Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/fury/lib/fury.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ class Fury {
return Promise.reject(error);
}

if (!api) {
// eslint-disable-next-line no-param-reassign
api = new this.minim.elements.Category();
}

const promise = adapter.serialize({ api, namespace: this.minim, mediaType });

if (done) {
Expand Down
23 changes: 18 additions & 5 deletions packages/fury/test/serialize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const assert = require('./assert');

describe('Serialize', () => {
describe('using callback', () => {
const JSONSerializeAdapter = {
name: 'json',
mediaTypes: ['application/json'],
serialize: ({ api, namespace }) => Promise.resolve(JSON.stringify(namespace.serialiser.serialise(api))),
};

it('errors with unknown mediaType', (done) => {
const fury = new Fury();
const api = new fury.minim.elements.Category();
Expand Down Expand Up @@ -34,11 +40,7 @@ describe('Serialize', () => {

it('can serialize with matching adapter', (done) => {
const fury = new Fury();
fury.use({
name: 'json',
mediaTypes: ['application/json'],
serialize: ({ api, namespace }) => Promise.resolve(JSON.stringify(namespace.serialiser.serialise(api))),
});
fury.use(JSONSerializeAdapter);

const api = new fury.minim.elements.Category();

Expand All @@ -48,6 +50,17 @@ describe('Serialize', () => {
done();
});
});

it('can serialize undefined `api` by creating default category', (done) => {
const fury = new Fury();
fury.use(JSONSerializeAdapter);

fury.serialize({ api: undefined, mediaType: 'application/json' }, (error, result) => {
expect(error).to.be.null;
expect(result).to.equal('{"element":"category"}');
done();
});
});
});

describe('using async/await', () => {
Expand Down