Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Mar 21, 2022
1 parent dbc33df commit bcb3d82
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
11 changes: 5 additions & 6 deletions src/models/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ export interface AsyncAPIDocumentInterface extends BaseModel, ExtensionsMixinInt
info(): InfoInterface;
}

export function newAsyncAPIDocument(document: DetailedAsyncAPI): AsyncAPIDocumentInterface {
const majorVersion = document.semver.major;
switch (majorVersion) {
export function newAsyncAPIDocument(asyncapi: DetailedAsyncAPI): AsyncAPIDocumentInterface {
switch (asyncapi.semver.major) {
case 2:
return new AsyncAPIDocumentV2(document.parsed, { parent: null, asyncapi: document, pointer: '/' });
return new AsyncAPIDocumentV2(asyncapi.parsed, { parent: null, asyncapi, pointer: '/' });
case 3:
return new AsyncAPIDocumentV3(document.parsed, { parent: null, asyncapi: document, pointer: '/' });
return new AsyncAPIDocumentV3(asyncapi.parsed, { parent: null, asyncapi, pointer: '/' });
default:
throw new Error(`Unsupported AsyncAPI version: ${majorVersion}`);
throw new Error(`Unsupported AsyncAPI version: ${asyncapi.semver.version}`);
}
}
13 changes: 7 additions & 6 deletions src/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AsyncAPIDocumentInterface, newAsyncAPIDocument } from './models';

import { isAsyncAPIDocument, isParsedDocument, isStringifiedDocument } from './utils';
import { createDetailedAsyncAPI, isAsyncAPIDocument, isParsedDocument, isStringifiedDocument } from './utils';
import { xParserSpecStringified } from './constants';

export interface StringifyOptions {
Expand All @@ -26,25 +26,26 @@ export function stringify(document: unknown, options: StringifyOptions = {}): st
}

export function unstringify(document: unknown): AsyncAPIDocumentInterface | undefined {
let parsed: unknown = document;
if (typeof document === 'string') {
try {
document = JSON.parse(document);
parsed = JSON.parse(document);
} catch(_) {
return;
}
}

if (!isStringifiedDocument(document)) {
if (!isStringifiedDocument(parsed)) {
return;
}

// shall copy of whole JSON
document = { ...document };
parsed = { ...parsed };
// remove `x-parser-spec-stringified` extension
delete (<Record<string, any>>document)[String(xParserSpecStringified)];
delete (<Record<string, any>>parsed)[String(xParserSpecStringified)];

traverseStringifiedDoc(document, undefined, document, new Map(), new Map());
return newAsyncAPIDocument(<Record<string, any>>document);
return newAsyncAPIDocument(createDetailedAsyncAPI(document as string, parsed as Record<string, unknown>));
}

function refReplacer() {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function toAsyncAPIDocument(maybeDoc: unknown): AsyncAPIDocumentInterface
if (!isParsedDocument(maybeDoc)) {
return;
}
return unstringify(maybeDoc) || newAsyncAPIDocument(maybeDoc);
return unstringify(maybeDoc) || newAsyncAPIDocument(createDetailedAsyncAPI(maybeDoc, maybeDoc));
}

export function isAsyncAPIDocument(maybeDoc: unknown): maybeDoc is AsyncAPIDocumentInterface {
Expand Down
10 changes: 7 additions & 3 deletions test/models/v2/asyncapi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { newAsyncAPIDocument, AsyncAPIDocumentV2, InfoV2, AsyncAPIDocumentV3 } from '../../../src/models';
import { createDetailedAsyncAPI } from '../../../src/utils';

import {
assertExtensionsMixinInheritance,
Expand Down Expand Up @@ -35,20 +36,23 @@ describe('AsyncAPIDocument model', function() {
describe('AsyncAPIDocument factory', function() {
it('should create a valid document from v2.0.0', function() {
const doc = { asyncapi: "2.0.0" };
const d = newAsyncAPIDocument(doc)
const detailed = createDetailedAsyncAPI(doc, doc);
const d = newAsyncAPIDocument(detailed)
expect(d.version()).toEqual(doc.asyncapi);
expect(d).toBeInstanceOf(AsyncAPIDocumentV2);
});

it('should create a valid document from v3.0.0', function() {
const doc = { asyncapi: "3.0.0" };
const d = newAsyncAPIDocument(doc)
const detailed = createDetailedAsyncAPI(doc, doc);
const d = newAsyncAPIDocument(detailed)
expect(d.version()).toEqual(doc.asyncapi);
expect(d).toBeInstanceOf(AsyncAPIDocumentV3);
});

it('should fail trying to create a document from a non supported spec version', function() {
const doc = { asyncapi: "99.99.99" };
expect(() => newAsyncAPIDocument(doc)).toThrow("Unsupported version: 99.99.99");
const detailed = createDetailedAsyncAPI(doc, doc);
expect(() => newAsyncAPIDocument(detailed)).toThrow("Unsupported AsyncAPI version: 99.99.99");
});
});
5 changes: 4 additions & 1 deletion test/stringify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { xParserSpecParsed, xParserSpecStringified } from '../src/constants';
import { BaseModel, newAsyncAPIDocument } from '../src/models';
import { stringify, unstringify } from '../src/stringify';
import { createDetailedAsyncAPI } from '../src/utils';

describe('stringify & unstringify', function() {
class Model extends BaseModel {}
Expand Down Expand Up @@ -31,7 +32,9 @@ describe('stringify & unstringify', function() {
});

it('should stringify AsyncAPIDocument instance', function() {
expect(typeof stringify(newAsyncAPIDocument({ asyncapi: '2.0.0' }))).toEqual('string');
const doc = { asyncapi: '2.0.0' };
const detailed = createDetailedAsyncAPI(doc, doc);
expect(typeof stringify(newAsyncAPIDocument(detailed))).toEqual('string');
});
});

Expand Down
24 changes: 18 additions & 6 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ describe('utils', function() {
});

it('AsyncAPIDocument instance should return AsyncAPIDocument instance', function() {
expect(toAsyncAPIDocument(newAsyncAPIDocument({ asyncapi: '2.0.0' }))).toBeInstanceOf(AsyncAPIDocumentV2);
const doc = { asyncapi: '2.0.0' };
const detailed = createDetailedAsyncAPI(doc, doc);
expect(toAsyncAPIDocument(newAsyncAPIDocument(detailed))).toBeInstanceOf(AsyncAPIDocumentV2);
});

it('parsed document should return AsyncAPIDocument instance', function() {
Expand Down Expand Up @@ -74,7 +76,9 @@ describe('utils', function() {
});

it('AsyncAPIDocument instance should be AsyncAPI document', function() {
expect(isAsyncAPIDocument(newAsyncAPIDocument({ asyncapi: '2.0.0' }))).toEqual(true);
const doc = { asyncapi: '2.0.0' };
const detailed = createDetailedAsyncAPI(doc, doc);
expect(isAsyncAPIDocument(newAsyncAPIDocument(detailed))).toEqual(true);
});
});

Expand All @@ -96,11 +100,15 @@ describe('utils', function() {
});

it('AsyncAPIDocument instance should not be parsed document', function() {
expect(isParsedDocument(newAsyncAPIDocument({ asyncapi: '2.0.0' }))).toEqual(false);
const doc = { asyncapi: '2.0.0' };
const detailed = createDetailedAsyncAPI(doc, doc);
expect(isParsedDocument(newAsyncAPIDocument(detailed))).toEqual(false);
});

it('AsyncAPIDocument instance with proper extension should not be parsed document', function() {
expect(isParsedDocument(newAsyncAPIDocument({ asyncapi: '2.0.0', [xParserSpecParsed]: true }))).toEqual(false);
const doc = { asyncapi: '2.0.0', [xParserSpecParsed]: true };
const detailed = createDetailedAsyncAPI(doc, doc);
expect(isParsedDocument(newAsyncAPIDocument(detailed))).toEqual(false);
});

it('object with proper extension should be parsed document', function() {
Expand All @@ -126,11 +134,15 @@ describe('utils', function() {
});

it('AsyncAPIDocument instance should not be parsed document', function() {
expect(isStringifiedDocument(newAsyncAPIDocument({ asyncapi: '2.0.0' }))).toEqual(false);
const doc = { asyncapi: '2.0.0' };
const detailed = createDetailedAsyncAPI(doc, doc);
expect(isStringifiedDocument(newAsyncAPIDocument(detailed))).toEqual(false);
});

it('AsyncAPIDocument instance with proper extension should not be parsed document', function() {
expect(isStringifiedDocument(newAsyncAPIDocument({ asyncapi: '2.0.0', [xParserSpecParsed]: true, [xParserSpecStringified]: true }))).toEqual(false);
const doc = { asyncapi: '2.0.0', [xParserSpecParsed]: true, [xParserSpecStringified]: true };
const detailed = createDetailedAsyncAPI(doc, doc);
expect(isStringifiedDocument(newAsyncAPIDocument(detailed))).toEqual(false);
});

it('object with only stringified extension should not be parsed document', function() {
Expand Down

0 comments on commit bcb3d82

Please sign in to comment.