From bcb3d82e1b2e9c2d937be264002c9517b4b1c6c8 Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Wed, 16 Mar 2022 13:27:43 +0100 Subject: [PATCH] fix tests --- src/models/asyncapi.ts | 11 +++++------ src/stringify.ts | 13 +++++++------ src/utils.ts | 2 +- test/models/v2/asyncapi.spec.ts | 10 +++++++--- test/stringify.spec.ts | 5 ++++- test/utils.spec.ts | 24 ++++++++++++++++++------ 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/models/asyncapi.ts b/src/models/asyncapi.ts index 273e62785..3e093e2d9 100644 --- a/src/models/asyncapi.ts +++ b/src/models/asyncapi.ts @@ -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}`); } } diff --git a/src/stringify.ts b/src/stringify.ts index b49e4ed61..32240adee 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -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 { @@ -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 (>document)[String(xParserSpecStringified)]; + delete (>parsed)[String(xParserSpecStringified)]; traverseStringifiedDoc(document, undefined, document, new Map(), new Map()); - return newAsyncAPIDocument(>document); + return newAsyncAPIDocument(createDetailedAsyncAPI(document as string, parsed as Record)); } function refReplacer() { diff --git a/src/utils.ts b/src/utils.ts index 6c48e1048..da88db8ad 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { diff --git a/test/models/v2/asyncapi.spec.ts b/test/models/v2/asyncapi.spec.ts index 24c98cbc9..bd926257a 100644 --- a/test/models/v2/asyncapi.spec.ts +++ b/test/models/v2/asyncapi.spec.ts @@ -1,4 +1,5 @@ import { newAsyncAPIDocument, AsyncAPIDocumentV2, InfoV2, AsyncAPIDocumentV3 } from '../../../src/models'; +import { createDetailedAsyncAPI } from '../../../src/utils'; import { assertExtensionsMixinInheritance, @@ -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"); }); }); diff --git a/test/stringify.spec.ts b/test/stringify.spec.ts index 6d702920f..14c79eee9 100644 --- a/test/stringify.spec.ts +++ b/test/stringify.spec.ts @@ -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 {} @@ -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'); }); }); diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 13d9be02d..e5ebfdca3 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -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() { @@ -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); }); }); @@ -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() { @@ -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() {