|
| 1 | +import { xParserSpecParsed, xParserSpecStringified } from '../src/constants'; |
| 2 | +import { AsyncAPIDocument, BaseModel } from '../src/models'; |
| 3 | +import { toAsyncAPIDocument, isAsyncAPIDocument, isParsedDocument, isStringifiedDocument } from '../src/utils'; |
| 4 | + |
| 5 | +describe('utils', function() { |
| 6 | + describe('toAsyncAPIDocument()', function() { |
| 7 | + it('normal object should not return AsyncAPIDocument instance', function() { |
| 8 | + expect(toAsyncAPIDocument({})).toEqual(undefined); |
| 9 | + }); |
| 10 | + |
| 11 | + it('null object should not return AsyncAPIDocument instance', function() { |
| 12 | + expect(toAsyncAPIDocument(null)).toEqual(undefined); |
| 13 | + }); |
| 14 | + |
| 15 | + it('primitive should not return AsyncAPIDocument instance', function() { |
| 16 | + expect(toAsyncAPIDocument('AsyncAPI rocks!')).toEqual(undefined); |
| 17 | + }); |
| 18 | + |
| 19 | + it('BaseModel instance should not return AsyncAPIDocument instance', function() { |
| 20 | + expect(toAsyncAPIDocument(new BaseModel({}))).toEqual(undefined); |
| 21 | + }); |
| 22 | + |
| 23 | + it('AsyncAPIDocument instance should return AsyncAPIDocument instance', function() { |
| 24 | + expect(toAsyncAPIDocument(new AsyncAPIDocument({ asyncapi: '2.0.0' }))).toBeInstanceOf(AsyncAPIDocument); |
| 25 | + }); |
| 26 | + |
| 27 | + it('parsed document should return AsyncAPIDocument instance', function() { |
| 28 | + expect(toAsyncAPIDocument({ [xParserSpecParsed]: true })).toBeInstanceOf(AsyncAPIDocument); |
| 29 | + }); |
| 30 | + |
| 31 | + it('stringified document should return AsyncAPIDocument instance', function() { |
| 32 | + expect(toAsyncAPIDocument({ [xParserSpecParsed]: true, [xParserSpecStringified]: true })).toBeInstanceOf(AsyncAPIDocument); |
| 33 | + }); |
| 34 | + |
| 35 | + it('stringified document (with missed parsed extension) should not return AsyncAPIDocument instance', function() { |
| 36 | + expect(toAsyncAPIDocument({ [xParserSpecStringified]: true })).toEqual(undefined); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('isAsyncAPIDocument()', function() { |
| 41 | + it('normal object should not be AsyncAPI document', function() { |
| 42 | + expect(isAsyncAPIDocument({})).toEqual(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('null object should not be AsyncAPI document', function() { |
| 46 | + expect(isAsyncAPIDocument(null)).toEqual(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('primitive should not be AsyncAPI document', function() { |
| 50 | + expect(isAsyncAPIDocument('AsyncAPI rocks!')).toEqual(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('BaseModel instance should not be AsyncAPI document', function() { |
| 54 | + expect(isAsyncAPIDocument(new BaseModel({}))).toEqual(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it('AsyncAPIDocument instance should be AsyncAPI document', function() { |
| 58 | + expect(isAsyncAPIDocument(new AsyncAPIDocument({ asyncapi: '2.0.0' }))).toEqual(true); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('isParsedDocument()', function() { |
| 63 | + it('normal object should not be parsed document', function() { |
| 64 | + expect(isParsedDocument({})).toEqual(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('null object should not be parsed document', function() { |
| 68 | + expect(isParsedDocument(null)).toEqual(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it('primitive should not be parsed document', function() { |
| 72 | + expect(isParsedDocument('AsyncAPI rocks!')).toEqual(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it('BaseModel instance should not be AsyncAPI document', function() { |
| 76 | + expect(isParsedDocument(new BaseModel({}))).toEqual(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it('AsyncAPIDocument instance should not be parsed document', function() { |
| 80 | + expect(isParsedDocument(new AsyncAPIDocument({ asyncapi: '2.0.0' }))).toEqual(false); |
| 81 | + }); |
| 82 | + |
| 83 | + it('AsyncAPIDocument instance with proper extension should not be parsed document', function() { |
| 84 | + expect(isParsedDocument(new AsyncAPIDocument({ asyncapi: '2.0.0', [xParserSpecParsed]: true }))).toEqual(false); |
| 85 | + }); |
| 86 | + |
| 87 | + it('object with proper extension should be parsed document', function() { |
| 88 | + expect(isParsedDocument({ [xParserSpecParsed]: true })).toEqual(true); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('isStringifiedDocument()', function() { |
| 93 | + it('normal object should not be parsed document', function() { |
| 94 | + expect(isStringifiedDocument({})).toEqual(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('null object should not be parsed document', function() { |
| 98 | + expect(isStringifiedDocument(null)).toEqual(false); |
| 99 | + }); |
| 100 | + |
| 101 | + it('primitive should not be parsed document', function() { |
| 102 | + expect(isStringifiedDocument('AsyncAPI rocks!')).toEqual(false); |
| 103 | + }); |
| 104 | + |
| 105 | + it('BaseModel instance should not be AsyncAPI document', function() { |
| 106 | + expect(isStringifiedDocument(new BaseModel({}))).toEqual(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it('AsyncAPIDocument instance should not be parsed document', function() { |
| 110 | + expect(isStringifiedDocument(new AsyncAPIDocument({ asyncapi: '2.0.0' }))).toEqual(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it('AsyncAPIDocument instance with proper extension should not be parsed document', function() { |
| 114 | + expect(isStringifiedDocument(new AsyncAPIDocument({ asyncapi: '2.0.0', [xParserSpecParsed]: true, [xParserSpecStringified]: true }))).toEqual(false); |
| 115 | + }); |
| 116 | + |
| 117 | + it('object with only stringified extension should not be parsed document', function() { |
| 118 | + expect(isStringifiedDocument({ [xParserSpecStringified]: true })).toEqual(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it('object with proper extensions should be parsed document', function() { |
| 122 | + expect(isStringifiedDocument({ [xParserSpecParsed]: true, [xParserSpecStringified]: true })).toEqual(true); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments