From d2dc88207ff5979f52448436e6ad3da53b804ebf Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Fri, 6 May 2022 15:07:04 +0200 Subject: [PATCH 1/2] refactor: port channel, channel-parameter and schema models --- src/models/schema.ts | 56 +- src/models/v2/channel-parameter.ts | 41 ++ src/models/v2/channel-parameters.ts | 14 + src/models/v2/channel.ts | 78 +++ src/models/v2/channels.ts | 14 + src/models/v2/schema.ts | 255 ++++++- test/models/v2/channel-parameter.spec.ts | 70 ++ test/models/v2/channel-parameters.spec.ts | 45 ++ test/models/v2/channel.spec.ts | 114 +++ test/models/v2/channels.spec.ts | 45 ++ test/models/v2/schema.spec.ts | 803 ++++++++++++++++++++++ 11 files changed, 1532 insertions(+), 3 deletions(-) create mode 100644 src/models/v2/channel-parameter.ts create mode 100644 src/models/v2/channel-parameters.ts create mode 100644 src/models/v2/channel.ts create mode 100644 src/models/v2/channels.ts create mode 100644 test/models/v2/channel-parameter.spec.ts create mode 100644 test/models/v2/channel-parameters.spec.ts create mode 100644 test/models/v2/channel.spec.ts create mode 100644 test/models/v2/channels.spec.ts create mode 100644 test/models/v2/schema.spec.ts diff --git a/src/models/schema.ts b/src/models/schema.ts index c6b4384f1..ec48a7788 100644 --- a/src/models/schema.ts +++ b/src/models/schema.ts @@ -1,4 +1,56 @@ import type { BaseModel } from "./base"; -import type { ExtensionsMixinInterface } from "./mixins"; +import type { ExtensionsMixinInterface, ExternalDocumentationMixinInterface } from "./mixins"; -export interface SchemaInterface extends BaseModel, ExtensionsMixinInterface {} +export interface SchemaInterface extends BaseModel, ExtensionsMixinInterface, ExternalDocumentationMixinInterface { + uid(): string; + $comment(): string | undefined; + $id(): string | undefined; + $schema(): string; + additionalItems(): boolean | SchemaInterface; + additionalProperties(): boolean | SchemaInterface; + allOf(): Array | undefined; + anyOf(): Array | undefined; + const(): any; + contains(): SchemaInterface | undefined; + contentEncoding(): string | undefined; + contentMediaType(): string | undefined; + default(): any; + definitions(): Record | undefined; + description(): string | undefined; + dependencies(): Record> | undefined; + deprecated(): boolean; + discriminator(): string | undefined; + else(): SchemaInterface | undefined; + enum(): Array | undefined; + examples(): Array | undefined; + exclusiveMaximum(): number | undefined; + exclusiveMinimum(): number | undefined; + format(): string | undefined; + isBooleanSchema(): boolean; + if(): SchemaInterface | undefined; + isCircular(): boolean; + items(): SchemaInterface | Array | undefined; + maximum(): number | undefined; + maxItems(): number | undefined; + maxLength(): number | undefined; + maxProperties(): number | undefined; + minimum(): number | undefined; + minItems(): number | undefined; + minLength(): number | undefined; + minProperties(): number | undefined; + multipleOf(): number | undefined; + not(): SchemaInterface | undefined; + oneOf(): Array | undefined; + pattern(): string | undefined; + patternProperties(): Record | undefined; + properties(): Record | undefined; + property(key: string): SchemaInterface | undefined; + propertyNames(): SchemaInterface | undefined; + readOnly(): boolean | undefined; + required(): Array | undefined; + then(): SchemaInterface | undefined; + title(): string | undefined; + type(): string | Array | undefined; + uniqueItems(): boolean | undefined; + writeOnly(): boolean | undefined; +} diff --git a/src/models/v2/channel-parameter.ts b/src/models/v2/channel-parameter.ts new file mode 100644 index 000000000..8eee59a55 --- /dev/null +++ b/src/models/v2/channel-parameter.ts @@ -0,0 +1,41 @@ +import { BaseModel } from "../base"; +import { Schema } from "./schema"; + +import { Mixin } from '../utils'; +import { DescriptionMixin } from './mixins/description'; +import { ExtensionsMixin } from './mixins/extensions'; + +import type { ModelMetadata } from "../base"; +import type { ChannelParameterInterface } from "../channel-parameter"; +import type { SchemaInterface } from "../schema"; + +export class ChannelParameter extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements ChannelParameterInterface { + constructor( + private readonly _id: string, + _json: Record, + _meta: ModelMetadata = {} as any + ) { + super(_json, _meta); + } + + id(): string { + return this._id + } + + hasSchema(): boolean { + return !!this._json.schema; + } + + schema(): SchemaInterface | undefined { + if (!this._json.schema) return undefined; + return this.createModel(Schema, this._json.schema, { pointer: `${this._meta.pointer}/schema` }); + } + + hasLocation(): boolean { + return !!this._json.location; + } + + location(): string | undefined { + return this._json.location; + } +} diff --git a/src/models/v2/channel-parameters.ts b/src/models/v2/channel-parameters.ts new file mode 100644 index 000000000..486f0177a --- /dev/null +++ b/src/models/v2/channel-parameters.ts @@ -0,0 +1,14 @@ +import { Collection } from '../collection'; + +import type { ChannelParametersInterface } from '../channel-parameters'; +import type { ChannelParameterInterface } from '../channel-parameter'; + +export class ChannelParameters extends Collection implements ChannelParametersInterface { + override get(id: string): ChannelParameterInterface | undefined { + return this.collections.find(parameter => parameter.id() === id); + } + + override has(id: string): boolean { + return this.collections.some(parameter => parameter.id() === id); + } +} diff --git a/src/models/v2/channel.ts b/src/models/v2/channel.ts new file mode 100644 index 000000000..f57eee045 --- /dev/null +++ b/src/models/v2/channel.ts @@ -0,0 +1,78 @@ +import { BaseModel } from "../base"; +import { ChannelParameters } from './channel-parameters'; +import { ChannelParameter } from './channel-parameter'; +import { Messages } from './messages'; +import { Operations } from './operations'; +import { Operation } from './operation'; +import { Servers } from './servers'; +import { Server } from './server'; + +import { Mixin } from '../utils'; +import { BindingsMixin } from './mixins/bindings'; +import { DescriptionMixin } from './mixins/description'; +import { ExtensionsMixin } from './mixins/extensions'; + +import type { ModelMetadata } from "../base"; +import type { ChannelInterface } from "../channel"; +import type { ChannelParametersInterface } from "../channel-parameters"; +import type { MessagesInterface } from "../messages"; +import type { MessageInterface } from "../message"; +import type { OperationsInterface } from "../operations"; +import type { OperationInterface } from "../operation"; +import type { ServersInterface } from "../servers"; +import type { ServerInterface } from "../server"; + +export class Channel extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, ExtensionsMixin) implements ChannelInterface { + constructor( + private readonly _id: string, + _json: Record, + protected readonly _meta: ModelMetadata & { address: string } = {} as any + ) { + super(_json, _meta); + } + + id(): string { + return this._id + } + + address(): string { + return this._meta.address; + } + + servers(): ServersInterface { + const servers: ServerInterface[] = []; + const allowedServers: string[] = this._json.servers || []; + Object.entries(this._meta.asyncapi?.parsed.servers || {}).map(([serverName, server]) => { + if (allowedServers.length === 0 || allowedServers.includes(serverName)) { + servers.push(this.createModel(Server, server, { id: serverName, pointer: `/servers/${serverName}` })); + } + }); + return new Servers(servers); + } + + operations(): OperationsInterface { + const operations: OperationInterface[] = [] + if (this._json.publish) { + operations.push( + this.createModel(Operation, this._json.publish, { id: 'publish', action: 'publish', pointer: `${this._meta.pointer}/publish` }), + ); + } + if (this._json.subscribe) { + operations.push( + this.createModel(Operation, this._json.subscribe, { id: 'subscribe', action: 'subscribe', pointer: `${this._meta.pointer}/subscribe` }), + ); + } + return new Operations(operations); + } + + parameters(): ChannelParametersInterface { + return new ChannelParameters( + Object.entries(this._json.parameters || {}).map(([channelParameterName, channelParameter]) => { + return this.createModel(ChannelParameter, channelParameter, { + id: channelParameterName, + pointer: `${this._meta.pointer}/parameters/${channelParameterName}` + }) + }) + ); + } +} diff --git a/src/models/v2/channels.ts b/src/models/v2/channels.ts new file mode 100644 index 000000000..8a3520197 --- /dev/null +++ b/src/models/v2/channels.ts @@ -0,0 +1,14 @@ +import { Collection } from '../collection'; + +import type { ChannelsInterface } from '../channels'; +import type { ChannelInterface } from '../channel'; + +export class Channels extends Collection implements ChannelsInterface { + override get(id: string): ChannelInterface | undefined { + return this.collections.find(channel => channel.id() === id); + } + + override has(id: string): boolean { + return this.collections.some(channel => channel.id() === id); + } +} diff --git a/src/models/v2/schema.ts b/src/models/v2/schema.ts index 24eb9ea9a..dfc791f49 100644 --- a/src/models/v2/schema.ts +++ b/src/models/v2/schema.ts @@ -2,7 +2,260 @@ import { BaseModel } from "../base"; import { Mixin } from '../utils'; import { ExtensionsMixin } from './mixins/extensions'; +import { ExternalDocumentationMixin } from './mixins/external-docs'; +import type { ModelMetadata } from "../base"; import type { SchemaInterface } from "../schema"; -export class Schema extends Mixin(BaseModel, ExtensionsMixin) implements SchemaInterface {} +export class Schema extends Mixin(BaseModel, ExtensionsMixin, ExternalDocumentationMixin) implements SchemaInterface { + constructor( + private readonly _id: string, + _json: Record, + protected readonly _meta: ModelMetadata & { parent: Schema } = {} as any + ) { + super(_json, _meta); + } + + uid(): string { + return this._id; + } + + $comment(): string | undefined { + return this._json.$comment; + } + + $id(): string | undefined { + return this._json.$id; + } + + $schema(): string { + return this._json.$schema || 'http://json-schema.org/draft-07/schema#'; + } + + additionalItems(): boolean | SchemaInterface { + if (this._json.additionalItems === undefined) return true; + if (typeof this._json.additionalItems === 'boolean') return this._json.additionalItems; + return this.createModel(Schema, this._json.additionalItems, { pointer: `${this._meta.pointer}/additionalItems`, parent: this }); + } + + additionalProperties(): boolean | SchemaInterface { + if (this._json.additionalProperties === undefined) return true; + if (typeof this._json.additionalProperties === 'boolean') return this._json.additionalProperties; + return this.createModel(Schema, this._json.additionalProperties, { pointer: `${this._meta.pointer}/additionalProperties`, parent: this }); + } + + allOf(): Array | undefined { + if (!Array.isArray(this._json.allOf)) return undefined; + return this._json.allOf.map((s, index) => this.createModel(Schema, s, { pointer: `${this._meta.pointer}/allOf/${index}`, parent: this })); + } + + anyOf(): Array | undefined { + if (!Array.isArray(this._json.anyOf)) return undefined; + return this._json.anyOf.map((s, index) => this.createModel(Schema, s, { pointer: `${this._meta.pointer}/anyOf/${index}`, parent: this })); + } + + const(): any { + return this._json.const; + } + + contains(): SchemaInterface | undefined { + if (typeof this._json.contains !== 'object') return; + return this.createModel(Schema, this._json.contains, { pointer: `${this._meta.pointer}/contains`, parent: this }); + } + + contentEncoding(): string | undefined { + return this._json.contentEncoding; + } + + contentMediaType(): string | undefined { + return this._json.contentMediaType; + } + + default(): any { + return this._json.default; + } + + definitions(): Record | undefined { + if (typeof this._json.definitions !== 'object') return undefined; + return Object.entries(this._json.definitions).reduce((acc: Record, [key, s]: [string, any]) => { + acc[key] = this.createModel(Schema, s, { pointer: `${this._meta.pointer}/definitions/${key}`, parent: this }); + return acc; + }, {}); + } + + description(): string | undefined { + return this._json.description; + } + + dependencies(): Record> | undefined { + if (typeof this._json.dependencies !== 'object') return undefined; + return Object.entries(this._json.dependencies).reduce((acc: Record>, [key, s]: [string, any]) => { + acc[key] = Array.isArray(s) ? s : this.createModel(Schema, s, { pointer: `${this._meta.pointer}/dependencies/${key}`, parent: this }); + return acc; + }, {}); + } + + deprecated(): boolean { + return this._json.deprecated || false; + } + + discriminator(): string | undefined { + return this._json.discriminator; + } + + else(): SchemaInterface | undefined { + if (typeof this._json.else !== 'object') return; + return this.createModel(Schema, this._json.else, { pointer: `${this._meta.pointer}/else`, parent: this }); + } + + enum(): Array | undefined { + return this._json.enum; + } + + examples(): Array | undefined { + return this._json.examples; + } + + exclusiveMaximum(): number | undefined { + return this._json.exclusiveMaximum; + } + + exclusiveMinimum(): number | undefined { + return this._json.exclusiveMinimum; + } + + format(): string | undefined { + return this._json.format; + } + + isBooleanSchema(): boolean { + return typeof this._json === 'boolean'; + } + + if(): SchemaInterface | undefined { + if (typeof this._json.if !== 'object') return; + return this.createModel(Schema, this._json.if, { pointer: `${this._meta.pointer}/if`, parent: this }); + } + + isCircular(): boolean { + let parent = this._meta.parent; + while (parent) { + if (parent._json === this._json) return true; + parent = parent._meta.parent; + } + return false; + } + + items(): SchemaInterface | Array | undefined { + if (typeof this._json.items !== 'object') return; + if (Array.isArray(this._json.items)) { + return this._json.items.map((s, index) => this.createModel(Schema, s, { pointer: `${this._meta.pointer}/items/${index}`, parent: this })); + } + return this.createModel(Schema, this._json.items, { pointer: `${this._meta.pointer}/items`, parent: this }); + } + + maximum(): number | undefined { + return this._json.maximum; + } + + maxItems(): number | undefined { + return this._json.maxItems; + } + + maxLength(): number | undefined { + return this._json.maxLength; + } + + maxProperties(): number | undefined { + return this._json.maxProperties; + } + + minimum(): number | undefined { + return this._json.minimum; + } + + minItems(): number | undefined { + return this._json.minItems; + } + + minLength(): number | undefined { + return this._json.minLength; + } + + minProperties(): number | undefined { + return this._json.minProperties; + } + + multipleOf(): number | undefined { + return this._json.multipleOf; + } + + not(): SchemaInterface | undefined { + if (typeof this._json.not !== 'object') return; + return this.createModel(Schema, this._json.not, { pointer: `${this._meta.pointer}/not`, parent: this }); + } + + oneOf(): Array | undefined { + if (!Array.isArray(this._json.oneOf)) return undefined; + return this._json.oneOf.map((s, index) => this.createModel(Schema, s, { pointer: `${this._meta.pointer}/oneOf/${index}`, parent: this })); + } + + pattern(): string | undefined { + return this._json.pattern; + } + + patternProperties(): Record | undefined { + if (typeof this._json.patternProperties !== 'object') return undefined; + return Object.entries(this._json.patternProperties).reduce((acc: Record, [key, s]: [string, any]) => { + acc[key] = this.createModel(Schema, s, { pointer: `${this._meta.pointer}/patternProperties/${key}`, parent: this }); + return acc; + }, {}); + } + + properties(): Record | undefined { + if (typeof this._json.properties !== 'object') return undefined; + return Object.entries(this._json.properties).reduce((acc: Record, [key, s]: [string, any]) => { + acc[key] = this.createModel(Schema, s, { pointer: `${this._meta.pointer}/properties/${key}`, parent: this }); + return acc; + }, {}); + } + + property(key: string): SchemaInterface | undefined { + if (this._json.properties === undefined || this._json.properties[key] === undefined) return undefined; + return this.createModel(Schema, this._json.properties[key], { pointer: `${this._meta.pointer}/properties/${key}`, parent: this }); + } + + propertyNames(): SchemaInterface | undefined { + if (typeof this._json.propertyNames !== 'object') return; + return this.createModel(Schema, this._json.propertyNames, { pointer: `${this._meta.pointer}/propertyNames`, parent: this }); + } + + readOnly(): boolean | undefined { + return this._json.readOnly || false; + } + + required(): Array | undefined { + return this._json.required; + } + + then(): SchemaInterface | undefined { + if (typeof this._json.then !== 'object') return; + return this.createModel(Schema, this._json.then, { pointer: `${this._meta.pointer}/then`, parent: this }); + } + + title(): string | undefined { + return this._json.title; + } + + type(): string | Array | undefined { + return this._json.type; + } + + uniqueItems(): boolean | undefined { + return this._json.uniqueItems || false; + } + + writeOnly(): boolean | undefined { + return this._json.writeOnly || false; + } +} diff --git a/test/models/v2/channel-parameter.spec.ts b/test/models/v2/channel-parameter.spec.ts new file mode 100644 index 000000000..5a18d4863 --- /dev/null +++ b/test/models/v2/channel-parameter.spec.ts @@ -0,0 +1,70 @@ +import { ChannelParameter } from '../../../src/models/v2/channel-parameter'; +import { Schema } from '../../../src/models/v2/schema'; + +import { + assertDescriptionMixinInheritance, + assertExtensionsMixinInheritance, +} from './mixins/inheritance'; + +describe('ChannelParameter model', function() { + describe('.hasLocation()', function() { + it('should return true when there is a value', function() { + const doc = { location: "..." }; + const d = new ChannelParameter('parameter', doc); + expect(d.hasLocation()).toEqual(true); + }); + + it('should return false when there is no value', function() { + const doc = {}; + const d = new ChannelParameter('parameter', doc); + expect(d.hasLocation()).toEqual(false); + }); + }); + + describe('.location()', function() { + it('should return the value', function() { + const doc = { location: "..." }; + const d = new ChannelParameter('parameter', doc); + expect(d.location()).toEqual(doc.location); + }); + + it('should return undefined when there is no value', function() { + const doc = {}; + const d = new ChannelParameter('parameter', doc); + expect(d.location()).toBeUndefined(); + }); + }); + + describe('.hasSchema()', function() { + it('should return true when there is a value', function() { + const doc = { schema: {} }; + const d = new ChannelParameter('parameter', doc); + expect(d.hasSchema()).toEqual(true); + }); + + it('should return false when there is no value', function() { + const doc = {}; + const d = new ChannelParameter('parameter', doc); + expect(d.hasSchema()).toEqual(false); + }); + }); + + describe('.schema()', function() { + it('should return the value', function() { + const doc = { schema: {} }; + const d = new ChannelParameter('parameter', doc); + expect(d.schema()).toBeInstanceOf(Schema); + }); + + it('should return undefined when there is no value', function() { + const doc = {}; + const d = new ChannelParameter('parameter', doc); + expect(d.schema()).toBeUndefined(); + }); + }); + + describe('mixins inheritance', function() { + assertDescriptionMixinInheritance(ChannelParameter); + assertExtensionsMixinInheritance(ChannelParameter); + }); +}); diff --git a/test/models/v2/channel-parameters.spec.ts b/test/models/v2/channel-parameters.spec.ts new file mode 100644 index 000000000..d8c7723e4 --- /dev/null +++ b/test/models/v2/channel-parameters.spec.ts @@ -0,0 +1,45 @@ +import { ChannelParameters } from '../../../src/models/v2/channel-parameters'; +import { ChannelParameter } from '../../../src/models/v2/channel-parameter'; + +const channelParameter = { + location: '...', +}; +const channelParameterItem = new ChannelParameter('parameter', channelParameter); + +describe('ChannelParameters model', function () { + describe('.isEmpty()', function () { + it('should return true if collection is empty', function () { + const servers = new ChannelParameters([]); + expect(servers.isEmpty()).toEqual(true); + }); + + it('should return false if collection is not empty', function () { + const servers = new ChannelParameters([channelParameterItem]); + expect(servers.isEmpty()).toEqual(false); + }); + }); + + describe('.get(id)', function () { + it('should return a specific Channel Parameter if it is present', function () { + const servers = new ChannelParameters([channelParameterItem]); + expect(servers.get('parameter')).toBeTruthy(); + }); + + it('should return undefined if specific Channel Parameter is missing', function () { + const servers = new ChannelParameters([]); + expect(servers.get('parameter')).toBeUndefined(); + }); + }); + + describe('.has(id)', function () { + it('should return true if the said name is available', function () { + const servers = new ChannelParameters([channelParameterItem]); + expect(servers.has('parameter')).toEqual(true); + }) + + it('should return false if the Channel Parameter name is missing', function () { + const servers = new ChannelParameters([channelParameterItem]); + expect(servers.has('anotherName')).toEqual(false); + }) + }) +}) \ No newline at end of file diff --git a/test/models/v2/channel.spec.ts b/test/models/v2/channel.spec.ts new file mode 100644 index 000000000..c52c4506e --- /dev/null +++ b/test/models/v2/channel.spec.ts @@ -0,0 +1,114 @@ +import { Channel } from '../../../src/models/v2/channel'; +import { ChannelParameters } from '../../../src/models/v2/channel-parameters'; +import { ChannelParameter } from '../../../src/models/v2/channel-parameter'; +import { Operations } from '../../../src/models/v2/operations'; +import { Operation } from '../../../src/models/v2/operation'; +import { Servers } from '../../../src/models/v2/servers'; +import { Server } from '../../../src/models/v2/server'; + +import { + assertBindingsMixinInheritance, + assertDescriptionMixinInheritance, + assertExtensionsMixinInheritance, +} from './mixins/inheritance'; + +describe('Channel model', function() { + describe('.id()', function() { + it('should return id of model', function() { + const doc = {}; + const d = new Channel('channel', doc); + expect(d.id()).toEqual('channel'); + }); + }); + + describe('.address()', function() { + it('should return the value', function() { + const doc = { location: "..." }; + const d = new Channel('channel', doc, { asyncapi: {} as any, pointer: '', address: 'user/signup' }); + expect(d.address()).toEqual('user/signup'); + }); + }); + + describe('.servers()', function() { + it('should return collection of servers - available on all servers', function() { + const doc = {}; + const d = new Channel('parameter', doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', address: 'user/signup' }); + expect(d.servers()).toBeInstanceOf(Servers); + expect(d.servers().all()).toHaveLength(2); + expect(d.servers().all()[0]).toBeInstanceOf(Server); + expect(d.servers().all()[0].id()).toEqual('someServer1'); + expect(d.servers().all()[1]).toBeInstanceOf(Server); + expect(d.servers().all()[1].id()).toEqual('someServer2'); + }); + + it('should return collection of servers - available on all servers (empty servers array)', function() { + const doc = { servers: [] }; + const d = new Channel('parameter', doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', address: 'user/signup' }); + expect(d.servers()).toBeInstanceOf(Servers); + expect(d.servers().all()).toHaveLength(2); + expect(d.servers().all()[0]).toBeInstanceOf(Server); + expect(d.servers().all()[0].id()).toEqual('someServer1'); + expect(d.servers().all()[1]).toBeInstanceOf(Server); + expect(d.servers().all()[1].id()).toEqual('someServer2'); + }); + + it('should return collection of servers - available only on particular ones', function() { + const doc = { servers: ['someServer2'] }; + const d = new Channel('parameter', doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', address: 'user/signup' }); + expect(d.servers()).toBeInstanceOf(Servers); + expect(d.servers().all()).toHaveLength(1); + expect(d.servers().all()[0]).toBeInstanceOf(Server); + expect(d.servers().all()[0].id()).toEqual('someServer2'); + }); + }); + + describe('.operations()', function() { + it('should return collection of operations - publish operation', function() { + const doc = { publish: {} }; + const d = new Channel('parameter', doc); + expect(d.operations()).toBeInstanceOf(Operations); + expect(d.operations().all()).toHaveLength(1); + expect(d.operations().all()[0]).toBeInstanceOf(Operation); + expect(d.operations().all()[0].action()).toEqual('publish'); + }); + + it('should return collection of operations - subscribe operation', function() { + const doc = { subscribe: {} }; + const d = new Channel('parameter', doc); + expect(d.operations()).toBeInstanceOf(Operations); + expect(d.operations().all()).toHaveLength(1); + expect(d.operations().all()[0]).toBeInstanceOf(Operation); + expect(d.operations().all()[0].action()).toEqual('subscribe'); + }); + + it('should return collection of operations - both operations', function() { + const doc = { publish: {}, subscribe: {} }; + const d = new Channel('parameter', doc); + expect(d.operations()).toBeInstanceOf(Operations); + expect(d.operations().all()).toHaveLength(2); + expect(d.operations().all()[0]).toBeInstanceOf(Operation); + expect(d.operations().all()[0].action()).toEqual('publish'); + expect(d.operations().all()[1]).toBeInstanceOf(Operation); + expect(d.operations().all()[1].action()).toEqual('subscribe'); + }); + }); + + describe('.parameters()', function() { + it('should return collection of channel parameters', function() { + const doc = { parameters: { parameter1: {}, parameter2: {} } }; + const d = new Channel('parameter', doc); + expect(d.parameters()).toBeInstanceOf(ChannelParameters); + expect(d.parameters().all()).toHaveLength(2); + expect(d.parameters().all()[0]).toBeInstanceOf(ChannelParameter); + expect(d.parameters().all()[0].id()).toEqual('parameter1'); + expect(d.parameters().all()[1]).toBeInstanceOf(ChannelParameter); + expect(d.parameters().all()[1].id()).toEqual('parameter2'); + }); + }); + + describe('mixins inheritance', function() { + assertBindingsMixinInheritance(Channel); + assertDescriptionMixinInheritance(Channel); + assertExtensionsMixinInheritance(Channel); + }); +}); diff --git a/test/models/v2/channels.spec.ts b/test/models/v2/channels.spec.ts new file mode 100644 index 000000000..27f2ae390 --- /dev/null +++ b/test/models/v2/channels.spec.ts @@ -0,0 +1,45 @@ +import { Channels } from '../../../src/models/v2/channels'; +import { Channel } from '../../../src/models/v2/channel'; + +const channel = { + publish: {}, +}; +const channelItem = new Channel('channel', channel); + +describe('Channels model', function () { + describe('.isEmpty()', function () { + it('should return true if collection is empty', function () { + const servers = new Channels([]); + expect(servers.isEmpty()).toEqual(true); + }); + + it('should return false if collection is not empty', function () { + const servers = new Channels([channelItem]); + expect(servers.isEmpty()).toEqual(false); + }); + }); + + describe('.get(id)', function () { + it('should return a specific Channel if it is present', function () { + const servers = new Channels([channelItem]); + expect(servers.get('channel')).toBeTruthy(); + }); + + it('should return undefined if specific Channel is missing', function () { + const servers = new Channels([]); + expect(servers.get('channel')).toBeUndefined(); + }); + }); + + describe('.has(id)', function () { + it('should return true if the said name is available', function () { + const servers = new Channels([channelItem]); + expect(servers.has('channel')).toEqual(true); + }) + + it('should return false if the Channel name is missing', function () { + const servers = new Channels([channelItem]); + expect(servers.has('anotherName')).toEqual(false); + }) + }) +}) \ No newline at end of file diff --git a/test/models/v2/schema.spec.ts b/test/models/v2/schema.spec.ts new file mode 100644 index 000000000..9fce65b21 --- /dev/null +++ b/test/models/v2/schema.spec.ts @@ -0,0 +1,803 @@ +import { Schema } from '../../../src/models/v2/schema'; + +import { + assertExtensionsMixinInheritance, + assertExternalDocumentationMixinInheritance, +} from './mixins/inheritance'; + +describe('Channel model', function() { + describe('.id()', function() { + it('should return id of model', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.uid()).toEqual('schema'); + }); + }); + + describe('.$comment()', function() { + it('should return the value', function() { + const doc = { $comment: "..." }; + const d = new Schema('schema', doc); + expect(d.$comment()).toEqual(doc.$comment); + }); + + it('should return undefined when there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.$comment()).toBeUndefined(); + }); + }); + + describe('.$id()', function() { + it('should return the value', function() { + const doc = { $id: "..." }; + const d = new Schema('schema', doc); + expect(d.$id()).toEqual(doc.$id); + }); + + it('should return undefined when there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.$id()).toBeUndefined(); + }); + }); + + describe('.$schema()', function() { + it('should return the value', function() { + const doc = { $schema: "..." }; + const d = new Schema('schema', doc); + expect(d.$schema()).toEqual(doc.$schema); + }); + + it('should return fallback value when there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.$schema()).toEqual('http://json-schema.org/draft-07/schema#'); + }); + }); + + describe('.additionalItems()', function() { + it('should return the value schema object', function() { + const doc = { additionalItems: {} }; + const d = new Schema('schema', doc); + expect(d.additionalItems()).toBeInstanceOf(Schema); + }); + + it('should return the true when there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.additionalItems()).toEqual(true); + }); + + it('should return the false where there is false value', function() { + const doc = { additionalItems: false }; + const d = new Schema('schema', doc); + expect(d.additionalItems()).toEqual(false); + }); + + it('should return the false where there is true value', function() { + const doc = { additionalItems: true }; + const d = new Schema('schema', doc); + expect(d.additionalItems()).toEqual(true); + }); + }); + + describe('.additionalProperties()', function() { + it('should return the value schema object', function() { + const doc = { additionalProperties: {} }; + const d = new Schema('schema', doc); + expect(d.additionalProperties()).toBeInstanceOf(Schema); + }); + + it('should return the true when there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.additionalProperties()).toEqual(true); + }); + + it('should return the false where there is false value', function() { + const doc = { additionalProperties: false }; + const d = new Schema('schema', doc); + expect(d.additionalProperties()).toEqual(false); + }); + + it('should return the false where there is true value', function() { + const doc = { additionalProperties: true }; + const d = new Schema('schema', doc); + expect(d.additionalProperties()).toEqual(true); + }); + }); + + describe('.allOf()', function() { + it('should return collection of schemas', function() { + const doc = { allOf: [ {}, {} ] }; + const d = new Schema('schema', doc); + expect(Array.isArray(d.allOf())).toEqual(true); + expect(d.allOf()).toHaveLength(2); + expect((d.allOf() as any)[0]).toBeInstanceOf(Schema); + expect((d.allOf() as any)[1]).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.allOf()).toBeUndefined(); + }); + }); + + describe('.anyOf()', function() { + it('should return collection of schemas', function() { + const doc = { anyOf: [ {}, {} ] }; + const d = new Schema('schema', doc); + expect(Array.isArray(d.anyOf())).toEqual(true); + expect(d.anyOf()).toHaveLength(2); + expect((d.anyOf() as any)[0]).toBeInstanceOf(Schema); + expect((d.anyOf() as any)[1]).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.anyOf()).toBeUndefined(); + }); + }); + + describe('.const()', function() { + it('should return value', function() { + const doc = { const: '...' }; + const d = new Schema('schema', doc); + expect(d.const()).toEqual(doc.const); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.const()).toBeUndefined(); + }); + }); + + describe('.contains()', function() { + it('should return value', function() { + const doc = { contains: {} }; + const d = new Schema('schema', doc); + expect(d.contains()).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.contains()).toBeUndefined(); + }); + }); + + describe('.contentEncoding()', function() { + it('should return value', function() { + const doc = { contentEncoding: '...' }; + const d = new Schema('schema', doc); + expect(d.contentEncoding()).toEqual(doc.contentEncoding); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.contentEncoding()).toBeUndefined(); + }); + }); + + describe('.contentMediaType()', function() { + it('should return value', function() { + const doc = { contentMediaType: '...' }; + const d = new Schema('schema', doc); + expect(d.contentMediaType()).toEqual(doc.contentMediaType); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.contentMediaType()).toBeUndefined(); + }); + }); + + describe('.default()', function() { + it('should return value', function() { + const doc = { default: '...' }; + const d = new Schema('schema', doc); + expect(d.default()).toEqual(doc.default); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.default()).toBeUndefined(); + }); + }); + + describe('.definitions()', function() { + it('should return map of definitions', function() { + const doc = { definitions: { def: {} } }; + const d = new Schema('schema', doc); + expect(typeof d.definitions() === 'object').toEqual(true); + expect((d.definitions() as any)['def']).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.default()).toBeUndefined(); + }); + }); + + describe('.description()', function() { + it('should return value', function() { + const doc = { description: '...' }; + const d = new Schema('schema', doc); + expect(d.description()).toEqual(doc.description); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.description()).toBeUndefined(); + }); + }); + + describe('.dependencies()', function() { + it('should return map of dependencies (schema case)', function() { + const doc = { dependencies: { dep: {} } }; + const d = new Schema('schema', doc); + expect(typeof d.dependencies() === 'object').toEqual(true); + expect((d.dependencies() as any)['dep']).toBeInstanceOf(Schema); + }); + + it('should return map of dependencies (array case)', function() { + const doc = { dependencies: { array: [] } }; + const d = new Schema('schema', doc); + expect(typeof d.dependencies() === 'object').toEqual(true); + expect(Array.isArray((d.dependencies() as any)['array'])).toEqual(true); + }); + + it('should return map of dependencies (schema and array case)', function() { + const doc = { dependencies: { dep: {}, array: [] } }; + const d = new Schema('schema', doc); + expect(typeof d.dependencies() === 'object').toEqual(true); + expect((d.dependencies() as any)['dep']).toBeInstanceOf(Schema); + expect(Array.isArray((d.dependencies() as any)['array'])).toEqual(true); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.dependencies()).toBeUndefined(); + }); + }); + + describe('.deprecated()', function() { + it('should return value', function() { + const doc = { deprecated: true }; + const d = new Schema('schema', doc); + expect(d.deprecated()).toEqual(doc.deprecated); + }); + + it('should return false where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.deprecated()).toEqual(false); + }); + }); + + describe('.discriminator()', function() { + it('should return value', function() { + const doc = { discriminator: '...' }; + const d = new Schema('schema', doc); + expect(d.discriminator()).toEqual(doc.discriminator); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.discriminator()).toBeUndefined(); + }); + }); + + describe('.else()', function() { + it('should return value', function() { + const doc = { else: {} }; + const d = new Schema('schema', doc); + expect(d.else()).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.else()).toBeUndefined(); + }); + }); + + describe('.enum()', function() { + it('should return value', function() { + const doc = { enum: ['example'] }; + const d = new Schema('schema', doc); + expect(Array.isArray(d.enum())).toEqual(true); + expect((d.enum() as any)[0]).toEqual('example'); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.else()).toBeUndefined(); + }); + }); + + describe('.examples()', function() { + it('should return value', function() { + const doc = { examples: ['example'] }; + const d = new Schema('schema', doc); + expect(Array.isArray(d.examples())).toEqual(true); + expect((d.examples() as any)[0]).toEqual('example'); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.examples()).toBeUndefined(); + }); + }); + + describe('.exclusiveMaximum()', function() { + it('should return value', function() { + const doc = { exclusiveMaximum: 2137 }; + const d = new Schema('schema', doc); + expect(d.exclusiveMaximum()).toEqual(doc.exclusiveMaximum); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.exclusiveMaximum()).toBeUndefined(); + }); + }); + + describe('.exclusiveMinimum()', function() { + it('should return value', function() { + const doc = { exclusiveMinimum: 2137 }; + const d = new Schema('schema', doc); + expect(d.exclusiveMinimum()).toEqual(doc.exclusiveMinimum); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.exclusiveMinimum()).toBeUndefined(); + }); + }); + + describe('.format()', function() { + it('should return value', function() { + const doc = { format: '...' }; + const d = new Schema('schema', doc); + expect(d.format()).toEqual(doc.format); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.format()).toBeUndefined(); + }); + }); + + describe('.isBooleanSchema()', function() { + it('should return true where value is true boolean', function() { + const d = new Schema('schema', true as any); + expect(d.isBooleanSchema()).toEqual(true); + }); + + it('should return true where value is false boolean', function() { + const d = new Schema('schema', false as any); + expect(d.isBooleanSchema()).toEqual(true); + }); + + it('should return false where value is object', function() { + const d = new Schema('schema', {}); + expect(d.isBooleanSchema()).toEqual(false); + }); + }); + + describe('.if()', function() { + it('should return value', function() { + const doc = { if: {} }; + const d = new Schema('schema', doc); + expect(d.if()).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.if()).toBeUndefined(); + }); + }); + + describe.skip('.isCircular()', function() { + it('should return a true when schema has circular reference', function() { + const doc = { + properties: { + nonCircular: { + type: 'string', + }, + circular: {}, + } + }; + doc.properties.circular = doc; + const d = new Schema('schema', doc); + expect(d.isCircular()).toEqual(false); + expect((d.properties() as any)['nonCircular'].isCircular()).toEqual(false); + expect((d.properties() as any)['circular'].isCircular()).toEqual(true); + }); + }); + + describe('.items()', function() { + it('should return schema instance', function() { + const doc = { items: {} }; + const d = new Schema('schema', doc); + expect(d.items()).toBeInstanceOf(Schema); + }); + + it('should return collection of schemas', function() { + const doc = { items: [ {}, {} ] }; + const d = new Schema('schema', doc); + expect(Array.isArray(d.items())).toEqual(true); + expect(d.items()).toHaveLength(2); + expect((d.items() as any)[0]).toBeInstanceOf(Schema); + expect((d.items() as any)[1]).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.items()).toBeUndefined(); + }); + }); + + describe('.maximum()', function() { + it('should return value', function() { + const doc = { maximum: 2137 }; + const d = new Schema('schema', doc); + expect(d.maximum()).toEqual(doc.maximum); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.maximum()).toBeUndefined(); + }); + }); + + describe('.maxItems()', function() { + it('should return value', function() { + const doc = { maxItems: 2137 }; + const d = new Schema('schema', doc); + expect(d.maxItems()).toEqual(doc.maxItems); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.maxItems()).toBeUndefined(); + }); + }); + + describe('.maxLength()', function() { + it('should return value', function() { + const doc = { maxLength: 2137 }; + const d = new Schema('schema', doc); + expect(d.maxLength()).toEqual(doc.maxLength); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.maxLength()).toBeUndefined(); + }); + }); + + describe('.maxProperties()', function() { + it('should return value', function() { + const doc = { maxProperties: 2137 }; + const d = new Schema('schema', doc); + expect(d.maxProperties()).toEqual(doc.maxProperties); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.maxProperties()).toBeUndefined(); + }); + }); + + describe('.minimum()', function() { + it('should return value', function() { + const doc = { minimum: 2137 }; + const d = new Schema('schema', doc); + expect(d.minimum()).toEqual(doc.minimum); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.minimum()).toBeUndefined(); + }); + }); + + describe('.minItems()', function() { + it('should return value', function() { + const doc = { minItems: 2137 }; + const d = new Schema('schema', doc); + expect(d.minItems()).toEqual(doc.minItems); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.minItems()).toBeUndefined(); + }); + }); + + describe('.minLength()', function() { + it('should return value', function() { + const doc = { minLength: 2137 }; + const d = new Schema('schema', doc); + expect(d.minLength()).toEqual(doc.minLength); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.minLength()).toBeUndefined(); + }); + }); + + describe('.minProperties()', function() { + it('should return value', function() { + const doc = { minProperties: 2137 }; + const d = new Schema('schema', doc); + expect(d.minProperties()).toEqual(doc.minProperties); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.minProperties()).toBeUndefined(); + }); + }); + + describe('.multipleOf()', function() { + it('should return value', function() { + const doc = { multipleOf: 2137 }; + const d = new Schema('schema', doc); + expect(d.multipleOf()).toEqual(doc.multipleOf); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.multipleOf()).toBeUndefined(); + }); + }); + + describe('.not()', function() { + it('should return value', function() { + const doc = { not: {} }; + const d = new Schema('schema', doc); + expect(d.not()).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.not()).toBeUndefined(); + }); + }); + + describe('.oneOf()', function() { + it('should return collection of schemas', function() { + const doc = { oneOf: [ {}, {} ] }; + const d = new Schema('schema', doc); + expect(Array.isArray(d.oneOf())).toEqual(true); + expect(d.oneOf()).toHaveLength(2); + expect((d.oneOf() as any)[0]).toBeInstanceOf(Schema); + expect((d.oneOf() as any)[1]).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.oneOf()).toBeUndefined(); + }); + }); + + describe('.pattern()', function() { + it('should return value', function() { + const doc = { pattern: '...' }; + const d = new Schema('schema', doc); + expect(d.pattern()).toEqual(doc.pattern); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.pattern()).toBeUndefined(); + }); + }); + + describe('.patternProperties()', function() { + it('should return map of patternProperties', function() { + const doc = { patternProperties: { prop: {} } }; + const d = new Schema('schema', doc); + expect(typeof d.patternProperties() === 'object').toEqual(true); + expect((d.patternProperties() as any)['prop']).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.patternProperties()).toBeUndefined(); + }); + }); + + describe('.properties()', function() { + it('should return map of properties', function() { + const doc = { properties: { prop: {} } }; + const d = new Schema('schema', doc); + expect(typeof d.properties() === 'object').toEqual(true); + expect((d.properties() as any)['prop']).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.properties()).toBeUndefined(); + }); + }); + + describe('.property()', function() { + it('should return property', function() { + const doc = { properties: { prop: {} } }; + const d = new Schema('schema', doc); + expect(d.property('prop')).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no property', function() { + const doc = { properties: { another: {} } }; + const d = new Schema('schema', doc); + expect(d.property('prop')).toBeUndefined(); + }); + + it('should return undefined where there is no properties', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.property('prop')).toBeUndefined(); + }); + }); + + describe('.propertyNames()', function() { + it('should return value', function() { + const doc = { propertyNames: {} }; + const d = new Schema('schema', doc); + expect(d.propertyNames()).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.propertyNames()).toBeUndefined(); + }); + }); + + describe('.readOnly()', function() { + it('should return value', function() { + const doc = { readOnly: true }; + const d = new Schema('schema', doc); + expect(d.readOnly()).toEqual(doc.readOnly); + }); + + it('should return false where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.readOnly()).toEqual(false); + }); + }); + + describe('.required()', function() { + it('should return array of required properties', function() { + const doc = { required: ['prop1', 'prop2'] }; + const d = new Schema('schema', doc); + expect(d.required()).toEqual(doc.required); + }); + + it('should return false where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.required()).toBeUndefined(); + }); + }); + + describe('.then()', function() { + it('should return value', function() { + const doc = { then: {} }; + const d = new Schema('schema', doc); + expect(d.then()).toBeInstanceOf(Schema); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.then()).toBeUndefined(); + }); + }); + + describe('.title()', function() { + it('should return value', function() { + const doc = { title: '...' }; + const d = new Schema('schema', doc); + expect(d.title()).toEqual(doc.title); + }); + + it('should return undefined where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.title()).toBeUndefined(); + }); + }); + + describe('.type()', function() { + it('should return single type', function() { + const doc = { type: 'object' }; + const d = new Schema('schema', doc); + expect(d.type()).toEqual(doc.type); + }); + + it('should return array of type', function() { + const doc = { type: ['object', 'array'] }; + const d = new Schema('schema', doc); + expect(d.type()).toEqual(doc.type); + }); + + it('should return false where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.type()).toBeUndefined(); + }); + }); + + describe('.uniqueItems()', function() { + it('should return value', function() { + const doc = { uniqueItems: true }; + const d = new Schema('schema', doc); + expect(d.uniqueItems()).toEqual(doc.uniqueItems); + }); + + it('should return false where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.uniqueItems()).toEqual(false); + }); + }); + + describe('.writeOnly()', function() { + it('should return value', function() { + const doc = { writeOnly: true }; + const d = new Schema('schema', doc); + expect(d.writeOnly()).toEqual(doc.writeOnly); + }); + + it('should return false where there is no value', function() { + const doc = {}; + const d = new Schema('schema', doc); + expect(d.writeOnly()).toEqual(false); + }); + }); + + describe('mixins inheritance', function() { + assertExtensionsMixinInheritance(Schema); + assertExternalDocumentationMixinInheritance(Schema); + }); +}); From 93aab7d80f98a1d1895ac39988baae877b58465a Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Fri, 6 May 2022 16:01:29 +0200 Subject: [PATCH 2/2] fix tests --- src/models/base.ts | 8 +- src/models/extension.ts | 2 +- src/models/v2/channel-parameter.ts | 5 +- src/models/v2/channel.ts | 7 +- src/models/v2/message-trait.ts | 5 +- src/models/v2/mixins/bindings.ts | 5 +- src/models/v2/mixins/extensions.ts | 11 +- src/models/v2/mixins/external-docs.ts | 5 +- src/models/v2/mixins/tags.ts | 5 +- src/models/v2/operation-trait.ts | 5 +- src/models/v2/schema.ts | 5 +- src/models/v2/security-scheme.ts | 5 +- src/models/v2/server-variable.ts | 5 +- src/models/v2/server.ts | 5 +- src/models/v3/mixins/extensions.ts | 6 +- test/models/v2/channel-parameter.spec.ts | 24 ++- test/models/v2/channel-parameters.spec.ts | 2 +- test/models/v2/channel.spec.ts | 20 +- test/models/v2/channels.spec.ts | 2 +- test/models/v2/message-trait.spec.ts | 66 +++---- test/models/v2/message-traits.spec.ts | 2 +- test/models/v2/message.spec.ts | 16 +- test/models/v2/messages.spec.ts | 2 +- test/models/v2/operation-trait.spec.ts | 26 +-- test/models/v2/operation-traits.spec.ts | 2 +- test/models/v2/operation.spec.ts | 16 +- test/models/v2/operations.spec.ts | 4 +- test/models/v2/schema.spec.ts | 222 +++++++++++----------- test/models/v2/security-scheme.spec.ts | 4 +- test/models/v2/server-variable.spec.ts | 2 +- test/models/v2/server.spec.ts | 8 +- test/models/v2/servers.spec.ts | 2 +- 32 files changed, 245 insertions(+), 259 deletions(-) diff --git a/src/models/base.ts b/src/models/base.ts index ca3efcc85..748a56889 100644 --- a/src/models/base.ts +++ b/src/models/base.ts @@ -32,11 +32,7 @@ export abstract class BaseModel { return `${this._meta?.pointer}/${field}`; } - protected createModel(Model: Constructor, value: any, { id, parent, pointer, ...rest }: { id?: string, pointer: string | number, [key: string]: any }): T { - const meta = { ...rest, asyncapi: this._meta.asyncapi, pointer } as ModelMetadata; - if (id) { - return new Model(id, value, meta); - } - return new Model(value, meta); + protected createModel(Model: Constructor, value: any, { pointer, ...rest }: { pointer: string | number, [key: string]: any }): T { + return new Model(value, { ...rest, asyncapi: this._meta.asyncapi, pointer }); } } diff --git a/src/models/extension.ts b/src/models/extension.ts index a21d16024..8099c45a8 100644 --- a/src/models/extension.ts +++ b/src/models/extension.ts @@ -1,7 +1,7 @@ import type { BaseModel } from "./base"; export interface ExtensionInterface extends BaseModel { - id(): string; + name(): string; version(): string; value(): any; } diff --git a/src/models/v2/channel-parameter.ts b/src/models/v2/channel-parameter.ts index 8eee59a55..0609b0636 100644 --- a/src/models/v2/channel-parameter.ts +++ b/src/models/v2/channel-parameter.ts @@ -11,15 +11,14 @@ import type { SchemaInterface } from "../schema"; export class ChannelParameter extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements ChannelParameterInterface { constructor( - private readonly _id: string, _json: Record, - _meta: ModelMetadata = {} as any + protected readonly _meta: ModelMetadata & { id: string } = {} as any ) { super(_json, _meta); } id(): string { - return this._id + return this._meta.id; } hasSchema(): boolean { diff --git a/src/models/v2/channel.ts b/src/models/v2/channel.ts index f57eee045..cc4b3cb47 100644 --- a/src/models/v2/channel.ts +++ b/src/models/v2/channel.ts @@ -15,8 +15,6 @@ import { ExtensionsMixin } from './mixins/extensions'; import type { ModelMetadata } from "../base"; import type { ChannelInterface } from "../channel"; import type { ChannelParametersInterface } from "../channel-parameters"; -import type { MessagesInterface } from "../messages"; -import type { MessageInterface } from "../message"; import type { OperationsInterface } from "../operations"; import type { OperationInterface } from "../operation"; import type { ServersInterface } from "../servers"; @@ -24,15 +22,14 @@ import type { ServerInterface } from "../server"; export class Channel extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, ExtensionsMixin) implements ChannelInterface { constructor( - private readonly _id: string, _json: Record, - protected readonly _meta: ModelMetadata & { address: string } = {} as any + protected readonly _meta: ModelMetadata & { id: string, address: string } = {} as any ) { super(_json, _meta); } id(): string { - return this._id + return this._meta.id; } address(): string { diff --git a/src/models/v2/message-trait.ts b/src/models/v2/message-trait.ts index a55c9c5c8..a796ae07d 100644 --- a/src/models/v2/message-trait.ts +++ b/src/models/v2/message-trait.ts @@ -19,15 +19,14 @@ import type { SchemaInterface } from "../schema"; export class MessageTrait extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, ExtensionsMixin, ExternalDocumentationMixin, TagsMixin) implements MessageTraitInterface { constructor( - private readonly _id: string, _json: Record, - _meta: ModelMetadata = {} as any + protected readonly _meta: ModelMetadata & { id: string } = {} as any ) { super(_json, _meta); } id(): string { - return this.messageId() || this._id; + return this.messageId() || this._meta.id; } hasMessageId(): boolean { diff --git a/src/models/v2/mixins/bindings.ts b/src/models/v2/mixins/bindings.ts index b1dfe9b2e..10135f31f 100644 --- a/src/models/v2/mixins/bindings.ts +++ b/src/models/v2/mixins/bindings.ts @@ -11,15 +11,14 @@ import type { BindingInterface } from "../../binding"; export class Binding extends Mixin(BaseModel, ExtensionsMixin) implements BindingInterface { constructor( - private readonly _protocol: string, _json: Record, - _meta: ModelMetadata = {} as any, + protected readonly _meta: ModelMetadata & { protocol: string } = {} as any, ) { super(_json, _meta); } protocol(): string { - return this._protocol; + return this._meta.protocol; } version(): string { diff --git a/src/models/v2/mixins/extensions.ts b/src/models/v2/mixins/extensions.ts index 727bb6598..cfee5c5af 100644 --- a/src/models/v2/mixins/extensions.ts +++ b/src/models/v2/mixins/extensions.ts @@ -10,15 +10,14 @@ import { EXTENSION_REGEX } from '../../../constants'; export class Extension extends BaseModel implements ExtensionInterface { constructor( - private readonly _id: string, _json: Record, - _meta: ModelMetadata = {} as any, + protected readonly _meta: ModelMetadata & { name: string } = {} as any, ) { super(_json, _meta); } - id(): string { - return this._id; + name(): string { + return this._meta.name; } version(): string { @@ -33,12 +32,12 @@ export class Extension extends BaseModel implements ExtensionInterface { export class Extensions extends Collection implements ExtensionsInterface { override get(name: string): ExtensionInterface | undefined { name = name.startsWith('x-') ? name : `x-${name}`; - return this.collections.find(ext => ext.id() === name); + return this.collections.find(ext => ext.name() === name); }; override has(name: string): boolean { name = name.startsWith('x-') ? name : `x-${name}`; - return this.collections.some(ext => ext.id() === name); + return this.collections.some(ext => ext.name() === name); }; } diff --git a/src/models/v2/mixins/external-docs.ts b/src/models/v2/mixins/external-docs.ts index d400201ca..92906fb0e 100644 --- a/src/models/v2/mixins/external-docs.ts +++ b/src/models/v2/mixins/external-docs.ts @@ -7,10 +7,7 @@ import { ExtensionsMixin } from './extensions'; import type { ExternalDocumentationInterface } from '../../external-docs'; import type { ExternalDocumentationMixinInterface } from "../../mixins"; -export class ExternalDocumentation - extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) - implements ExternalDocumentationInterface { - +export class ExternalDocumentation extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements ExternalDocumentationInterface { url(): string { return this._json.url; } diff --git a/src/models/v2/mixins/tags.ts b/src/models/v2/mixins/tags.ts index 0134183e5..4cc0a4fa0 100644 --- a/src/models/v2/mixins/tags.ts +++ b/src/models/v2/mixins/tags.ts @@ -10,10 +10,7 @@ import type { TagsMixinInterface } from "../../mixins"; import type { TagsInterface } from "../../tags"; import type { TagInterface } from "../../tag"; -export class Tag - extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin, ExternalDocumentationMixin) - implements TagInterface { - +export class Tag extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin, ExternalDocumentationMixin) implements TagInterface { name(): string { return this._json.name; } diff --git a/src/models/v2/operation-trait.ts b/src/models/v2/operation-trait.ts index aa3ccea40..9f9960558 100644 --- a/src/models/v2/operation-trait.ts +++ b/src/models/v2/operation-trait.ts @@ -15,15 +15,14 @@ import type { SecuritySchemeInterface } from "../security-scheme"; export class OperationTrait extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, ExtensionsMixin, ExternalDocumentationMixin, TagsMixin) implements OperationTraitInterface { constructor( - private readonly _id: string, _json: Record, - public readonly _meta: ModelMetadata & { action: OperationAction } = {} as any, + protected readonly _meta: ModelMetadata & { id: string, action: OperationAction } = {} as any, ) { super(_json, _meta); } id(): string { - return this.operationId() || this._id; + return this.operationId() || this._meta.id; } action(): OperationAction { diff --git a/src/models/v2/schema.ts b/src/models/v2/schema.ts index dfc791f49..0029a3658 100644 --- a/src/models/v2/schema.ts +++ b/src/models/v2/schema.ts @@ -9,15 +9,14 @@ import type { SchemaInterface } from "../schema"; export class Schema extends Mixin(BaseModel, ExtensionsMixin, ExternalDocumentationMixin) implements SchemaInterface { constructor( - private readonly _id: string, _json: Record, - protected readonly _meta: ModelMetadata & { parent: Schema } = {} as any + protected readonly _meta: ModelMetadata & { id: string, parent: Schema | null } = {} as any ) { super(_json, _meta); } uid(): string { - return this._id; + return this._meta.id; } $comment(): string | undefined { diff --git a/src/models/v2/security-scheme.ts b/src/models/v2/security-scheme.ts index bedeb610c..cf00444f9 100644 --- a/src/models/v2/security-scheme.ts +++ b/src/models/v2/security-scheme.ts @@ -11,15 +11,14 @@ import type { OAuthFlowsInterface } from '../oauth-flows'; export class SecurityScheme extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements SecuritySchemeInterface { constructor( - private readonly _id: string, _json: Record, - _meta: ModelMetadata = {} as any + protected readonly _meta: ModelMetadata & { id: string } = {} as any ) { super(_json, _meta); } id(): string { - return this._id; + return this._meta.id; } hasBearerFormat(): boolean { diff --git a/src/models/v2/server-variable.ts b/src/models/v2/server-variable.ts index e471973e7..99134ce91 100644 --- a/src/models/v2/server-variable.ts +++ b/src/models/v2/server-variable.ts @@ -9,15 +9,14 @@ import type { ServerVariableInterface } from '../server-variable'; export class ServerVariable extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements ServerVariableInterface { constructor( - private readonly _id: string, _json: Record, - _meta: ModelMetadata = {} as any + protected readonly _meta: ModelMetadata & { id: string } = {} as any ) { super(_json, _meta); } id(): string { - return this._id; + return this._meta.id; } hasDefaultValue(): boolean { diff --git a/src/models/v2/server.ts b/src/models/v2/server.ts index d19154582..bed21e88d 100644 --- a/src/models/v2/server.ts +++ b/src/models/v2/server.ts @@ -15,15 +15,14 @@ import type { SecuritySchemeInterface } from '../security-scheme'; export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, ExtensionsMixin) implements ServerInterface { constructor( - private readonly _id: string, _json: Record, - _meta: ModelMetadata = {} as any, + protected readonly _meta: ModelMetadata & { id: string } = {} as any, ) { super(_json, _meta); } id(): string { - return this._id; + return this._meta.id; } url(): string { diff --git a/src/models/v3/mixins/extensions.ts b/src/models/v3/mixins/extensions.ts index 9e5cb45dc..df5032f5b 100644 --- a/src/models/v3/mixins/extensions.ts +++ b/src/models/v3/mixins/extensions.ts @@ -17,7 +17,7 @@ export class Extension extends BaseModel implements ExtensionInterface { super(_json, _meta); } - id(): string { + name(): string { return this._id; } @@ -32,11 +32,11 @@ export class Extension extends BaseModel implements ExtensionInterface { export class Extensions extends Collection implements ExtensionsInterface { override get(name: string): ExtensionInterface | undefined { - return this.collections.find(ext => ext.id() === name); + return this.collections.find(ext => ext.name() === name); }; override has(name: string): boolean { - return this.collections.some(ext => ext.id() === name); + return this.collections.some(ext => ext.name() === name); }; } diff --git a/test/models/v2/channel-parameter.spec.ts b/test/models/v2/channel-parameter.spec.ts index 5a18d4863..1c1a4a074 100644 --- a/test/models/v2/channel-parameter.spec.ts +++ b/test/models/v2/channel-parameter.spec.ts @@ -7,16 +7,24 @@ import { } from './mixins/inheritance'; describe('ChannelParameter model', function() { + describe('.id()', function() { + it('should return id of model', function() { + const doc = {}; + const d = new ChannelParameter(doc, { asyncapi: {} as any, pointer: '', id: 'parameter' }); + expect(d.id()).toEqual('parameter'); + }); + }); + describe('.hasLocation()', function() { it('should return true when there is a value', function() { const doc = { location: "..." }; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.hasLocation()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.hasLocation()).toEqual(false); }); }); @@ -24,13 +32,13 @@ describe('ChannelParameter model', function() { describe('.location()', function() { it('should return the value', function() { const doc = { location: "..." }; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.location()).toEqual(doc.location); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.location()).toBeUndefined(); }); }); @@ -38,13 +46,13 @@ describe('ChannelParameter model', function() { describe('.hasSchema()', function() { it('should return true when there is a value', function() { const doc = { schema: {} }; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.hasSchema()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.hasSchema()).toEqual(false); }); }); @@ -52,13 +60,13 @@ describe('ChannelParameter model', function() { describe('.schema()', function() { it('should return the value', function() { const doc = { schema: {} }; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.schema()).toBeInstanceOf(Schema); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new ChannelParameter('parameter', doc); + const d = new ChannelParameter(doc); expect(d.schema()).toBeUndefined(); }); }); diff --git a/test/models/v2/channel-parameters.spec.ts b/test/models/v2/channel-parameters.spec.ts index d8c7723e4..703e2b556 100644 --- a/test/models/v2/channel-parameters.spec.ts +++ b/test/models/v2/channel-parameters.spec.ts @@ -4,7 +4,7 @@ import { ChannelParameter } from '../../../src/models/v2/channel-parameter'; const channelParameter = { location: '...', }; -const channelParameterItem = new ChannelParameter('parameter', channelParameter); +const channelParameterItem = new ChannelParameter(channelParameter, { asyncapi: {} as any, pointer: '', id: 'parameter' }); describe('ChannelParameters model', function () { describe('.isEmpty()', function () { diff --git a/test/models/v2/channel.spec.ts b/test/models/v2/channel.spec.ts index c52c4506e..f3435a380 100644 --- a/test/models/v2/channel.spec.ts +++ b/test/models/v2/channel.spec.ts @@ -16,15 +16,15 @@ describe('Channel model', function() { describe('.id()', function() { it('should return id of model', function() { const doc = {}; - const d = new Channel('channel', doc); + const d = new Channel(doc, { asyncapi: {} as any, pointer: '', id: 'channel', address: '' }); expect(d.id()).toEqual('channel'); }); }); describe('.address()', function() { it('should return the value', function() { - const doc = { location: "..." }; - const d = new Channel('channel', doc, { asyncapi: {} as any, pointer: '', address: 'user/signup' }); + const doc = {}; + const d = new Channel(doc, { asyncapi: {} as any, pointer: '', id: 'channel', address: 'user/signup' }); expect(d.address()).toEqual('user/signup'); }); }); @@ -32,7 +32,7 @@ describe('Channel model', function() { describe('.servers()', function() { it('should return collection of servers - available on all servers', function() { const doc = {}; - const d = new Channel('parameter', doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', address: 'user/signup' }); + const d = new Channel(doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', id: 'channel', address: 'user/signup' }); expect(d.servers()).toBeInstanceOf(Servers); expect(d.servers().all()).toHaveLength(2); expect(d.servers().all()[0]).toBeInstanceOf(Server); @@ -43,7 +43,7 @@ describe('Channel model', function() { it('should return collection of servers - available on all servers (empty servers array)', function() { const doc = { servers: [] }; - const d = new Channel('parameter', doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', address: 'user/signup' }); + const d = new Channel(doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', id: 'channel', address: 'user/signup' }); expect(d.servers()).toBeInstanceOf(Servers); expect(d.servers().all()).toHaveLength(2); expect(d.servers().all()[0]).toBeInstanceOf(Server); @@ -54,7 +54,7 @@ describe('Channel model', function() { it('should return collection of servers - available only on particular ones', function() { const doc = { servers: ['someServer2'] }; - const d = new Channel('parameter', doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', address: 'user/signup' }); + const d = new Channel(doc, { asyncapi: { parsed: { servers: { someServer1: {}, someServer2: {}, } } } as any, pointer: '', id: 'channel', address: 'user/signup' }); expect(d.servers()).toBeInstanceOf(Servers); expect(d.servers().all()).toHaveLength(1); expect(d.servers().all()[0]).toBeInstanceOf(Server); @@ -65,7 +65,7 @@ describe('Channel model', function() { describe('.operations()', function() { it('should return collection of operations - publish operation', function() { const doc = { publish: {} }; - const d = new Channel('parameter', doc); + const d = new Channel(doc); expect(d.operations()).toBeInstanceOf(Operations); expect(d.operations().all()).toHaveLength(1); expect(d.operations().all()[0]).toBeInstanceOf(Operation); @@ -74,7 +74,7 @@ describe('Channel model', function() { it('should return collection of operations - subscribe operation', function() { const doc = { subscribe: {} }; - const d = new Channel('parameter', doc); + const d = new Channel(doc); expect(d.operations()).toBeInstanceOf(Operations); expect(d.operations().all()).toHaveLength(1); expect(d.operations().all()[0]).toBeInstanceOf(Operation); @@ -83,7 +83,7 @@ describe('Channel model', function() { it('should return collection of operations - both operations', function() { const doc = { publish: {}, subscribe: {} }; - const d = new Channel('parameter', doc); + const d = new Channel(doc); expect(d.operations()).toBeInstanceOf(Operations); expect(d.operations().all()).toHaveLength(2); expect(d.operations().all()[0]).toBeInstanceOf(Operation); @@ -96,7 +96,7 @@ describe('Channel model', function() { describe('.parameters()', function() { it('should return collection of channel parameters', function() { const doc = { parameters: { parameter1: {}, parameter2: {} } }; - const d = new Channel('parameter', doc); + const d = new Channel(doc); expect(d.parameters()).toBeInstanceOf(ChannelParameters); expect(d.parameters().all()).toHaveLength(2); expect(d.parameters().all()[0]).toBeInstanceOf(ChannelParameter); diff --git a/test/models/v2/channels.spec.ts b/test/models/v2/channels.spec.ts index 27f2ae390..fa525b3ee 100644 --- a/test/models/v2/channels.spec.ts +++ b/test/models/v2/channels.spec.ts @@ -4,7 +4,7 @@ import { Channel } from '../../../src/models/v2/channel'; const channel = { publish: {}, }; -const channelItem = new Channel('channel', channel); +const channelItem = new Channel(channel, { asyncapi: {} as any, pointer: '', id: 'channel', address: '' }); describe('Channels model', function () { describe('.isEmpty()', function () { diff --git a/test/models/v2/message-trait.spec.ts b/test/models/v2/message-trait.spec.ts index 4ad51f934..1061eac17 100644 --- a/test/models/v2/message-trait.spec.ts +++ b/test/models/v2/message-trait.spec.ts @@ -16,13 +16,13 @@ describe('MessageTrait model', function() { describe('.id()', function() { it('should return id of model', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait' }); expect(d.id()).toEqual('trait'); }); it('should reuse messageId', function() { const doc = { messageId: '...' }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.id()).toEqual(doc.messageId); }); }); @@ -30,13 +30,13 @@ describe('MessageTrait model', function() { describe('.hasMessageId()', function() { it('should return true when there is a value', function() { const doc = { messageId: '...' }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasMessageId()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasMessageId()).toEqual(false); }); }); @@ -44,13 +44,13 @@ describe('MessageTrait model', function() { describe('.messageId()', function() { it('should return the value', function() { const doc = { messageId: '...' }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.messageId()).toEqual(doc.messageId); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.messageId()).toBeUndefined(); }); }); @@ -58,13 +58,13 @@ describe('MessageTrait model', function() { describe('.hasCorrelationId()', function() { it('should return true when there is a value', function() { const doc = { correlationId: {} }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasCorrelationId()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasCorrelationId()).toEqual(false); }); }); @@ -72,13 +72,13 @@ describe('MessageTrait model', function() { describe('.correlationId()', function() { it('should return the value', function() { const doc = { correlationId: {} }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.correlationId()).toBeInstanceOf(CorrelationId); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.correlationId()).toBeUndefined(); }); }); @@ -86,13 +86,13 @@ describe('MessageTrait model', function() { describe('.hasContentType()', function() { it('should return true when there is a value', function() { const doc = { contentType: '...' }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasContentType()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasContentType()).toEqual(false); }); }); @@ -100,19 +100,19 @@ describe('MessageTrait model', function() { describe('.contentType()', function() { it('should return the value', function() { const doc = { contentType: '...' }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.contentType()).toEqual(doc.contentType); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.contentType()).toBeUndefined(); }); it('should reuse defaultContentType value', function() { const doc = {}; - const d = new MessageTrait('trait', doc, { asyncapi: { parsed: { defaultContentType: '...' } } } as any); + const d = new MessageTrait(doc, { asyncapi: { parsed: { defaultContentType: '...' } } } as any); expect(d.contentType()).toEqual('...'); }); }); @@ -120,13 +120,13 @@ describe('MessageTrait model', function() { describe('.hasHeaders()', function() { it('should return true when there is a value', function() { const doc = { headers: {} }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasHeaders()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasHeaders()).toEqual(false); }); }); @@ -134,13 +134,13 @@ describe('MessageTrait model', function() { describe('.headers()', function() { it('should return the value', function() { const doc = { headers: {} }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.headers()).toBeInstanceOf(Schema); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.headers()).toBeUndefined(); }); }); @@ -148,13 +148,13 @@ describe('MessageTrait model', function() { describe('.hasName()', function() { it('should return true when there is a value', function() { const doc = { name: "..." }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasName()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasName()).toEqual(false); }); }); @@ -162,13 +162,13 @@ describe('MessageTrait model', function() { describe('.name()', function() { it('should return the value', function() { const doc = { name: "..." }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.name()).toEqual(doc.name); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.name()).toBeUndefined(); }); }); @@ -176,13 +176,13 @@ describe('MessageTrait model', function() { describe('.hasTitle()', function() { it('should return true when there is a value', function() { const doc = { title: "..." }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasTitle()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasTitle()).toEqual(false); }); }); @@ -190,13 +190,13 @@ describe('MessageTrait model', function() { describe('.title()', function() { it('should return the value', function() { const doc = { title: "..." }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.title()).toEqual(doc.title); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.title()).toBeUndefined(); }); }); @@ -204,13 +204,13 @@ describe('MessageTrait model', function() { describe('.hasSummary()', function() { it('should return true when there is a value', function() { const doc = { summary: "..." }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasSummary()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.hasSummary()).toEqual(false); }); }); @@ -218,13 +218,13 @@ describe('MessageTrait model', function() { describe('.summary()', function() { it('should return the value', function() { const doc = { summary: "..." }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.summary()).toEqual(doc.summary); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.summary()).toBeUndefined(); }); }); @@ -232,7 +232,7 @@ describe('MessageTrait model', function() { describe('.examples()', function() { it('should return collection of examples', function() { const doc = { examples: [ { name: '...' } ] }; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.examples()).toBeInstanceOf(MessageExamples); expect(d.examples().all()).toHaveLength(1); expect(d.examples().all()[0]).toBeInstanceOf(MessageExample); @@ -240,7 +240,7 @@ describe('MessageTrait model', function() { it('should return collection of examples when value is undefined', function() { const doc = {}; - const d = new MessageTrait('trait', doc); + const d = new MessageTrait(doc); expect(d.examples()).toBeInstanceOf(MessageExamples); expect(d.examples().all()).toHaveLength(0); }); diff --git a/test/models/v2/message-traits.spec.ts b/test/models/v2/message-traits.spec.ts index 9343c715d..6d1617cb0 100644 --- a/test/models/v2/message-traits.spec.ts +++ b/test/models/v2/message-traits.spec.ts @@ -4,7 +4,7 @@ import { MessageTrait } from '../../../src/models/v2/message-trait'; const messageTrait = { messageId: 'test', }; -const messageTraitItem = new MessageTrait('test', messageTrait); +const messageTraitItem = new MessageTrait(messageTrait, { asyncapi: {} as any, pointer: '', id: 'test' }); describe('MessageTraits model', function () { describe('.isEmpty()', function () { diff --git a/test/models/v2/message.spec.ts b/test/models/v2/message.spec.ts index 625191eca..d697624cd 100644 --- a/test/models/v2/message.spec.ts +++ b/test/models/v2/message.spec.ts @@ -15,13 +15,13 @@ describe('Message model', function() { describe('.id()', function() { it('should return id of model', function() { const doc = {}; - const d = new Message('message', doc); + const d = new Message(doc, { asyncapi: {} as any, pointer: '', id: 'message' }); expect(d.id()).toEqual('message'); }); it('should reuse messageId', function() { const doc = { messageId: '...' }; - const d = new Message('message', doc); + const d = new Message(doc); expect(d.id()).toEqual(doc.messageId); }); }); @@ -29,13 +29,13 @@ describe('Message model', function() { describe('.hasPayload()', function() { it('should return true when there is a value', function() { const doc = { payload: {} }; - const d = new Message('message', doc); + const d = new Message(doc); expect(d.hasPayload()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new Message('message', doc); + const d = new Message(doc); expect(d.hasPayload()).toEqual(false); }); }); @@ -43,13 +43,13 @@ describe('Message model', function() { describe('.payload()', function() { it('should return the value', function() { const doc = { payload: {} }; - const d = new Message('message', doc); + const d = new Message(doc); expect(d.payload()).toBeInstanceOf(Schema); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new Message('message', doc); + const d = new Message(doc); expect(d.payload()).toBeUndefined(); }); }); @@ -57,7 +57,7 @@ describe('Message model', function() { describe('.traits()', function() { it('should return collection of traits', function() { const doc = { traits: [ { messageId: '...' } ] }; - const d = new Message('message', doc); + const d = new Message(doc); expect(d.traits()).toBeInstanceOf(MessageTraits); expect(d.traits().all()).toHaveLength(1); expect(d.traits().all()[0]).toBeInstanceOf(MessageTrait); @@ -65,7 +65,7 @@ describe('Message model', function() { it('should return collection of traits when value is undefined', function() { const doc = {}; - const d = new Message('message', doc); + const d = new Message(doc); expect(d.traits()).toBeInstanceOf(MessageTraits); expect(d.traits().all()).toHaveLength(0); }); diff --git a/test/models/v2/messages.spec.ts b/test/models/v2/messages.spec.ts index 394c632f9..95cb35f77 100644 --- a/test/models/v2/messages.spec.ts +++ b/test/models/v2/messages.spec.ts @@ -4,7 +4,7 @@ import { Message } from '../../../src/models/v2/message'; const message = { messageId: 'test', }; -const messageItem = new Message('test', message); +const messageItem = new Message(message, { asyncapi: {} as any, pointer: '', id: 'test' }); describe('Messages model', function () { describe('.isEmpty()', function () { diff --git a/test/models/v2/operation-trait.spec.ts b/test/models/v2/operation-trait.spec.ts index 9704ae019..b30e1ebe1 100644 --- a/test/models/v2/operation-trait.spec.ts +++ b/test/models/v2/operation-trait.spec.ts @@ -13,13 +13,13 @@ describe('OperationTrait model', function() { describe('.id()', function() { it('should return id of model', function() { const doc = {}; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' }); expect(d.id()).toEqual('trait'); }); it('should reuse operationId', function() { const doc = { operationId: '...' }; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.id()).toEqual(doc.operationId); }); }); @@ -27,7 +27,7 @@ describe('OperationTrait model', function() { describe('.action()', function() { it('should return kind/action of operation', function() { const doc = {}; - const d = new OperationTrait('trait', doc, { asyncapi: {} as any, pointer: '', action: 'publish' }); + const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' }); expect(d.action()).toEqual('publish'); }); }); @@ -35,13 +35,13 @@ describe('OperationTrait model', function() { describe('.hasOperationId()', function() { it('should return true when there is a value', function() { const doc = { operationId: '...' }; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.hasOperationId()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.hasOperationId()).toEqual(false); }); }); @@ -49,13 +49,13 @@ describe('OperationTrait model', function() { describe('.operationId()', function() { it('should return the value', function() { const doc = { operationId: '...' }; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.operationId()).toEqual(doc.operationId); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.operationId()).toBeUndefined(); }); }); @@ -63,13 +63,13 @@ describe('OperationTrait model', function() { describe('.hasSummary()', function() { it('should return true when there is a value', function() { const doc = { summary: "..." }; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.hasSummary()).toEqual(true); }); it('should return false when there is no value', function() { const doc = {}; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.hasSummary()).toEqual(false); }); }); @@ -77,13 +77,13 @@ describe('OperationTrait model', function() { describe('.summary()', function() { it('should return the value', function() { const doc = { summary: "..." }; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.summary()).toEqual(doc.summary); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(d.summary()).toBeUndefined(); }); }); @@ -91,7 +91,7 @@ describe('OperationTrait model', function() { describe('.security()', function() { it('should return collection of security requirements', function() { const doc = { security: [ { requirement: [] } ] }; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(Array.isArray(d.security())).toEqual(true); expect(d.security()).toHaveLength(1); expect(typeof d.security()[0]).toEqual('object'); @@ -101,7 +101,7 @@ describe('OperationTrait model', function() { it('should return collection of security requirements when value is undefined', function() { const doc = {}; - const d = new OperationTrait('trait', doc); + const d = new OperationTrait(doc); expect(Array.isArray(d.security())).toEqual(true); expect(d.security()).toHaveLength(0); }); diff --git a/test/models/v2/operation-traits.spec.ts b/test/models/v2/operation-traits.spec.ts index 6f8bb0c2a..edfdd91c0 100644 --- a/test/models/v2/operation-traits.spec.ts +++ b/test/models/v2/operation-traits.spec.ts @@ -4,7 +4,7 @@ import { OperationTrait } from '../../../src/models/v2/operation-trait'; const operationTrait = { operationId: 'test', }; -const operationTraitItem = new OperationTrait('test', operationTrait); +const operationTraitItem = new OperationTrait(operationTrait, { asyncapi: {} as any, pointer: '', id: 'test', action: 'publish' }); describe('OperationTraits model', function () { describe('.isEmpty()', function () { diff --git a/test/models/v2/operation.spec.ts b/test/models/v2/operation.spec.ts index 8ff8d0a77..4ea92c60f 100644 --- a/test/models/v2/operation.spec.ts +++ b/test/models/v2/operation.spec.ts @@ -16,13 +16,13 @@ describe('Operation model', function() { describe('.id()', function() { it('should return id of model', function() { const doc = {}; - const d = new Operation('message', doc); - expect(d.id()).toEqual('message'); + const d = new Operation(doc, { asyncapi: {} as any, pointer: '', id: 'operation', action: 'publish' }); + expect(d.id()).toEqual('operation'); }); it('should reuse operationId', function() { const doc = { operationId: '...' }; - const d = new Operation('message', doc); + const d = new Operation(doc); expect(d.id()).toEqual(doc.operationId); }); }); @@ -30,7 +30,7 @@ describe('Operation model', function() { describe('.messages()', function() { it('should return collection of messages - single message', function() { const doc = { message: { messageId: '...' } }; - const d = new Operation('message', doc); + const d = new Operation(doc); expect(d.messages()).toBeInstanceOf(Messages); expect(d.messages().all()).toHaveLength(1); expect(d.messages().all()[0]).toBeInstanceOf(Message); @@ -38,7 +38,7 @@ describe('Operation model', function() { it('should return collection of messages - oneOf messages', function() { const doc = { message: { oneOf: [ { messageId: '...' }, { messageId: '...' } ] } }; - const d = new Operation('message', doc); + const d = new Operation(doc); expect(d.messages()).toBeInstanceOf(Messages); expect(d.messages().all()).toHaveLength(2); expect(d.messages().all()[0]).toBeInstanceOf(Message); @@ -47,7 +47,7 @@ describe('Operation model', function() { it('should return undefined when there is no value', function() { const doc = {}; - const d = new Operation('message', doc); + const d = new Operation(doc); expect(d.messages()).toBeInstanceOf(Messages); expect(d.messages().all()).toHaveLength(0); }); @@ -56,7 +56,7 @@ describe('Operation model', function() { describe('.traits()', function() { it('should return collection of traits', function() { const doc = { traits: [ { operationId: '...' } ] }; - const d = new Operation('message', doc); + const d = new Operation(doc); expect(d.traits()).toBeInstanceOf(OperationTraits); expect(d.traits().all()).toHaveLength(1); expect(d.traits().all()[0]).toBeInstanceOf(OperationTrait); @@ -64,7 +64,7 @@ describe('Operation model', function() { it('should return collection of traits when value is undefined', function() { const doc = {}; - const d = new Operation('message', doc); + const d = new Operation(doc); expect(d.traits()).toBeInstanceOf(OperationTraits); expect(d.traits().all()).toHaveLength(0); }); diff --git a/test/models/v2/operations.spec.ts b/test/models/v2/operations.spec.ts index ca0a87243..34ea61b5a 100644 --- a/test/models/v2/operations.spec.ts +++ b/test/models/v2/operations.spec.ts @@ -2,9 +2,9 @@ import { Operations } from '../../../src/models/v2/operations'; import { Operation } from '../../../src/models/v2/operation'; const operation = { - messageId: 'test', + operationId: 'test', }; -const operationItem = new Operation('test', operation); +const operationItem = new Operation(operation, { asyncapi: {} as any, pointer: '', id: 'test', action: 'publish' }); describe('Operations model', function () { describe('.isEmpty()', function () { diff --git a/test/models/v2/schema.spec.ts b/test/models/v2/schema.spec.ts index 9fce65b21..8444c5490 100644 --- a/test/models/v2/schema.spec.ts +++ b/test/models/v2/schema.spec.ts @@ -9,7 +9,7 @@ describe('Channel model', function() { describe('.id()', function() { it('should return id of model', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc, { asyncapi: {} as any, pointer: '', id: 'schema', parent: null }); expect(d.uid()).toEqual('schema'); }); }); @@ -17,13 +17,13 @@ describe('Channel model', function() { describe('.$comment()', function() { it('should return the value', function() { const doc = { $comment: "..." }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.$comment()).toEqual(doc.$comment); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.$comment()).toBeUndefined(); }); }); @@ -31,13 +31,13 @@ describe('Channel model', function() { describe('.$id()', function() { it('should return the value', function() { const doc = { $id: "..." }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.$id()).toEqual(doc.$id); }); it('should return undefined when there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.$id()).toBeUndefined(); }); }); @@ -45,13 +45,13 @@ describe('Channel model', function() { describe('.$schema()', function() { it('should return the value', function() { const doc = { $schema: "..." }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.$schema()).toEqual(doc.$schema); }); it('should return fallback value when there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.$schema()).toEqual('http://json-schema.org/draft-07/schema#'); }); }); @@ -59,25 +59,25 @@ describe('Channel model', function() { describe('.additionalItems()', function() { it('should return the value schema object', function() { const doc = { additionalItems: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalItems()).toBeInstanceOf(Schema); }); it('should return the true when there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalItems()).toEqual(true); }); it('should return the false where there is false value', function() { const doc = { additionalItems: false }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalItems()).toEqual(false); }); it('should return the false where there is true value', function() { const doc = { additionalItems: true }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalItems()).toEqual(true); }); }); @@ -85,25 +85,25 @@ describe('Channel model', function() { describe('.additionalProperties()', function() { it('should return the value schema object', function() { const doc = { additionalProperties: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalProperties()).toBeInstanceOf(Schema); }); it('should return the true when there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalProperties()).toEqual(true); }); it('should return the false where there is false value', function() { const doc = { additionalProperties: false }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalProperties()).toEqual(false); }); it('should return the false where there is true value', function() { const doc = { additionalProperties: true }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.additionalProperties()).toEqual(true); }); }); @@ -111,7 +111,7 @@ describe('Channel model', function() { describe('.allOf()', function() { it('should return collection of schemas', function() { const doc = { allOf: [ {}, {} ] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(Array.isArray(d.allOf())).toEqual(true); expect(d.allOf()).toHaveLength(2); expect((d.allOf() as any)[0]).toBeInstanceOf(Schema); @@ -120,7 +120,7 @@ describe('Channel model', function() { it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.allOf()).toBeUndefined(); }); }); @@ -128,7 +128,7 @@ describe('Channel model', function() { describe('.anyOf()', function() { it('should return collection of schemas', function() { const doc = { anyOf: [ {}, {} ] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(Array.isArray(d.anyOf())).toEqual(true); expect(d.anyOf()).toHaveLength(2); expect((d.anyOf() as any)[0]).toBeInstanceOf(Schema); @@ -137,7 +137,7 @@ describe('Channel model', function() { it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.anyOf()).toBeUndefined(); }); }); @@ -145,13 +145,13 @@ describe('Channel model', function() { describe('.const()', function() { it('should return value', function() { const doc = { const: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.const()).toEqual(doc.const); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.const()).toBeUndefined(); }); }); @@ -159,13 +159,13 @@ describe('Channel model', function() { describe('.contains()', function() { it('should return value', function() { const doc = { contains: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.contains()).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.contains()).toBeUndefined(); }); }); @@ -173,13 +173,13 @@ describe('Channel model', function() { describe('.contentEncoding()', function() { it('should return value', function() { const doc = { contentEncoding: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.contentEncoding()).toEqual(doc.contentEncoding); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.contentEncoding()).toBeUndefined(); }); }); @@ -187,13 +187,13 @@ describe('Channel model', function() { describe('.contentMediaType()', function() { it('should return value', function() { const doc = { contentMediaType: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.contentMediaType()).toEqual(doc.contentMediaType); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.contentMediaType()).toBeUndefined(); }); }); @@ -201,13 +201,13 @@ describe('Channel model', function() { describe('.default()', function() { it('should return value', function() { const doc = { default: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.default()).toEqual(doc.default); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.default()).toBeUndefined(); }); }); @@ -215,14 +215,14 @@ describe('Channel model', function() { describe('.definitions()', function() { it('should return map of definitions', function() { const doc = { definitions: { def: {} } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(typeof d.definitions() === 'object').toEqual(true); expect((d.definitions() as any)['def']).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.default()).toBeUndefined(); }); }); @@ -230,13 +230,13 @@ describe('Channel model', function() { describe('.description()', function() { it('should return value', function() { const doc = { description: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.description()).toEqual(doc.description); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.description()).toBeUndefined(); }); }); @@ -244,21 +244,21 @@ describe('Channel model', function() { describe('.dependencies()', function() { it('should return map of dependencies (schema case)', function() { const doc = { dependencies: { dep: {} } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(typeof d.dependencies() === 'object').toEqual(true); expect((d.dependencies() as any)['dep']).toBeInstanceOf(Schema); }); it('should return map of dependencies (array case)', function() { const doc = { dependencies: { array: [] } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(typeof d.dependencies() === 'object').toEqual(true); expect(Array.isArray((d.dependencies() as any)['array'])).toEqual(true); }); it('should return map of dependencies (schema and array case)', function() { const doc = { dependencies: { dep: {}, array: [] } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(typeof d.dependencies() === 'object').toEqual(true); expect((d.dependencies() as any)['dep']).toBeInstanceOf(Schema); expect(Array.isArray((d.dependencies() as any)['array'])).toEqual(true); @@ -266,7 +266,7 @@ describe('Channel model', function() { it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.dependencies()).toBeUndefined(); }); }); @@ -274,13 +274,13 @@ describe('Channel model', function() { describe('.deprecated()', function() { it('should return value', function() { const doc = { deprecated: true }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.deprecated()).toEqual(doc.deprecated); }); it('should return false where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.deprecated()).toEqual(false); }); }); @@ -288,13 +288,13 @@ describe('Channel model', function() { describe('.discriminator()', function() { it('should return value', function() { const doc = { discriminator: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.discriminator()).toEqual(doc.discriminator); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.discriminator()).toBeUndefined(); }); }); @@ -302,13 +302,13 @@ describe('Channel model', function() { describe('.else()', function() { it('should return value', function() { const doc = { else: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.else()).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.else()).toBeUndefined(); }); }); @@ -316,14 +316,14 @@ describe('Channel model', function() { describe('.enum()', function() { it('should return value', function() { const doc = { enum: ['example'] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(Array.isArray(d.enum())).toEqual(true); expect((d.enum() as any)[0]).toEqual('example'); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.else()).toBeUndefined(); }); }); @@ -331,14 +331,14 @@ describe('Channel model', function() { describe('.examples()', function() { it('should return value', function() { const doc = { examples: ['example'] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(Array.isArray(d.examples())).toEqual(true); expect((d.examples() as any)[0]).toEqual('example'); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.examples()).toBeUndefined(); }); }); @@ -346,13 +346,13 @@ describe('Channel model', function() { describe('.exclusiveMaximum()', function() { it('should return value', function() { const doc = { exclusiveMaximum: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.exclusiveMaximum()).toEqual(doc.exclusiveMaximum); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.exclusiveMaximum()).toBeUndefined(); }); }); @@ -360,13 +360,13 @@ describe('Channel model', function() { describe('.exclusiveMinimum()', function() { it('should return value', function() { const doc = { exclusiveMinimum: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.exclusiveMinimum()).toEqual(doc.exclusiveMinimum); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.exclusiveMinimum()).toBeUndefined(); }); }); @@ -374,30 +374,30 @@ describe('Channel model', function() { describe('.format()', function() { it('should return value', function() { const doc = { format: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.format()).toEqual(doc.format); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.format()).toBeUndefined(); }); }); describe('.isBooleanSchema()', function() { it('should return true where value is true boolean', function() { - const d = new Schema('schema', true as any); + const d = new Schema(true as any); expect(d.isBooleanSchema()).toEqual(true); }); it('should return true where value is false boolean', function() { - const d = new Schema('schema', false as any); + const d = new Schema(false as any); expect(d.isBooleanSchema()).toEqual(true); }); it('should return false where value is object', function() { - const d = new Schema('schema', {}); + const d = new Schema({}); expect(d.isBooleanSchema()).toEqual(false); }); }); @@ -405,18 +405,18 @@ describe('Channel model', function() { describe('.if()', function() { it('should return value', function() { const doc = { if: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.if()).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.if()).toBeUndefined(); }); }); - describe.skip('.isCircular()', function() { + describe('.isCircular()', function() { it('should return a true when schema has circular reference', function() { const doc = { properties: { @@ -427,7 +427,7 @@ describe('Channel model', function() { } }; doc.properties.circular = doc; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.isCircular()).toEqual(false); expect((d.properties() as any)['nonCircular'].isCircular()).toEqual(false); expect((d.properties() as any)['circular'].isCircular()).toEqual(true); @@ -437,13 +437,13 @@ describe('Channel model', function() { describe('.items()', function() { it('should return schema instance', function() { const doc = { items: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.items()).toBeInstanceOf(Schema); }); it('should return collection of schemas', function() { const doc = { items: [ {}, {} ] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(Array.isArray(d.items())).toEqual(true); expect(d.items()).toHaveLength(2); expect((d.items() as any)[0]).toBeInstanceOf(Schema); @@ -452,7 +452,7 @@ describe('Channel model', function() { it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.items()).toBeUndefined(); }); }); @@ -460,13 +460,13 @@ describe('Channel model', function() { describe('.maximum()', function() { it('should return value', function() { const doc = { maximum: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maximum()).toEqual(doc.maximum); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maximum()).toBeUndefined(); }); }); @@ -474,13 +474,13 @@ describe('Channel model', function() { describe('.maxItems()', function() { it('should return value', function() { const doc = { maxItems: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maxItems()).toEqual(doc.maxItems); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maxItems()).toBeUndefined(); }); }); @@ -488,13 +488,13 @@ describe('Channel model', function() { describe('.maxLength()', function() { it('should return value', function() { const doc = { maxLength: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maxLength()).toEqual(doc.maxLength); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maxLength()).toBeUndefined(); }); }); @@ -502,13 +502,13 @@ describe('Channel model', function() { describe('.maxProperties()', function() { it('should return value', function() { const doc = { maxProperties: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maxProperties()).toEqual(doc.maxProperties); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.maxProperties()).toBeUndefined(); }); }); @@ -516,13 +516,13 @@ describe('Channel model', function() { describe('.minimum()', function() { it('should return value', function() { const doc = { minimum: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minimum()).toEqual(doc.minimum); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minimum()).toBeUndefined(); }); }); @@ -530,13 +530,13 @@ describe('Channel model', function() { describe('.minItems()', function() { it('should return value', function() { const doc = { minItems: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minItems()).toEqual(doc.minItems); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minItems()).toBeUndefined(); }); }); @@ -544,13 +544,13 @@ describe('Channel model', function() { describe('.minLength()', function() { it('should return value', function() { const doc = { minLength: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minLength()).toEqual(doc.minLength); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minLength()).toBeUndefined(); }); }); @@ -558,13 +558,13 @@ describe('Channel model', function() { describe('.minProperties()', function() { it('should return value', function() { const doc = { minProperties: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minProperties()).toEqual(doc.minProperties); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.minProperties()).toBeUndefined(); }); }); @@ -572,13 +572,13 @@ describe('Channel model', function() { describe('.multipleOf()', function() { it('should return value', function() { const doc = { multipleOf: 2137 }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.multipleOf()).toEqual(doc.multipleOf); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.multipleOf()).toBeUndefined(); }); }); @@ -586,13 +586,13 @@ describe('Channel model', function() { describe('.not()', function() { it('should return value', function() { const doc = { not: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.not()).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.not()).toBeUndefined(); }); }); @@ -600,7 +600,7 @@ describe('Channel model', function() { describe('.oneOf()', function() { it('should return collection of schemas', function() { const doc = { oneOf: [ {}, {} ] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(Array.isArray(d.oneOf())).toEqual(true); expect(d.oneOf()).toHaveLength(2); expect((d.oneOf() as any)[0]).toBeInstanceOf(Schema); @@ -609,7 +609,7 @@ describe('Channel model', function() { it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.oneOf()).toBeUndefined(); }); }); @@ -617,13 +617,13 @@ describe('Channel model', function() { describe('.pattern()', function() { it('should return value', function() { const doc = { pattern: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.pattern()).toEqual(doc.pattern); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.pattern()).toBeUndefined(); }); }); @@ -631,14 +631,14 @@ describe('Channel model', function() { describe('.patternProperties()', function() { it('should return map of patternProperties', function() { const doc = { patternProperties: { prop: {} } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(typeof d.patternProperties() === 'object').toEqual(true); expect((d.patternProperties() as any)['prop']).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.patternProperties()).toBeUndefined(); }); }); @@ -646,14 +646,14 @@ describe('Channel model', function() { describe('.properties()', function() { it('should return map of properties', function() { const doc = { properties: { prop: {} } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(typeof d.properties() === 'object').toEqual(true); expect((d.properties() as any)['prop']).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.properties()).toBeUndefined(); }); }); @@ -661,19 +661,19 @@ describe('Channel model', function() { describe('.property()', function() { it('should return property', function() { const doc = { properties: { prop: {} } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.property('prop')).toBeInstanceOf(Schema); }); it('should return undefined where there is no property', function() { const doc = { properties: { another: {} } }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.property('prop')).toBeUndefined(); }); it('should return undefined where there is no properties', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.property('prop')).toBeUndefined(); }); }); @@ -681,13 +681,13 @@ describe('Channel model', function() { describe('.propertyNames()', function() { it('should return value', function() { const doc = { propertyNames: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.propertyNames()).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.propertyNames()).toBeUndefined(); }); }); @@ -695,13 +695,13 @@ describe('Channel model', function() { describe('.readOnly()', function() { it('should return value', function() { const doc = { readOnly: true }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.readOnly()).toEqual(doc.readOnly); }); it('should return false where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.readOnly()).toEqual(false); }); }); @@ -709,13 +709,13 @@ describe('Channel model', function() { describe('.required()', function() { it('should return array of required properties', function() { const doc = { required: ['prop1', 'prop2'] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.required()).toEqual(doc.required); }); it('should return false where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.required()).toBeUndefined(); }); }); @@ -723,13 +723,13 @@ describe('Channel model', function() { describe('.then()', function() { it('should return value', function() { const doc = { then: {} }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.then()).toBeInstanceOf(Schema); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.then()).toBeUndefined(); }); }); @@ -737,13 +737,13 @@ describe('Channel model', function() { describe('.title()', function() { it('should return value', function() { const doc = { title: '...' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.title()).toEqual(doc.title); }); it('should return undefined where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.title()).toBeUndefined(); }); }); @@ -751,19 +751,19 @@ describe('Channel model', function() { describe('.type()', function() { it('should return single type', function() { const doc = { type: 'object' }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.type()).toEqual(doc.type); }); it('should return array of type', function() { const doc = { type: ['object', 'array'] }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.type()).toEqual(doc.type); }); it('should return false where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.type()).toBeUndefined(); }); }); @@ -771,13 +771,13 @@ describe('Channel model', function() { describe('.uniqueItems()', function() { it('should return value', function() { const doc = { uniqueItems: true }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.uniqueItems()).toEqual(doc.uniqueItems); }); it('should return false where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.uniqueItems()).toEqual(false); }); }); @@ -785,13 +785,13 @@ describe('Channel model', function() { describe('.writeOnly()', function() { it('should return value', function() { const doc = { writeOnly: true }; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.writeOnly()).toEqual(doc.writeOnly); }); it('should return false where there is no value', function() { const doc = {}; - const d = new Schema('schema', doc); + const d = new Schema(doc); expect(d.writeOnly()).toEqual(false); }); }); diff --git a/test/models/v2/security-scheme.spec.ts b/test/models/v2/security-scheme.spec.ts index e8e8ae590..9ccf6c230 100644 --- a/test/models/v2/security-scheme.spec.ts +++ b/test/models/v2/security-scheme.spec.ts @@ -18,8 +18,8 @@ const doc1 = { } } -const sc1 = new SecurityScheme('api_key', doc1); -const emptyItem = new SecurityScheme('', {}); +const sc1 = new SecurityScheme(doc1, { asyncapi: {} as any, pointer: '', id: 'api_key' }); +const emptyItem = new SecurityScheme({}); describe('Security Scheme', function () { describe('.id()', function () { diff --git a/test/models/v2/server-variable.spec.ts b/test/models/v2/server-variable.spec.ts index 42d420d43..0b19fd5fd 100644 --- a/test/models/v2/server-variable.spec.ts +++ b/test/models/v2/server-variable.spec.ts @@ -6,7 +6,7 @@ const doc = { enum: ['1883', '8883'] } -const sv = new ServerVariable('doc', doc); +const sv = new ServerVariable(doc, { asyncapi: {} as any, pointer: '', id: 'doc' }); describe('Server Variable ', function() { describe('.id()', function() { diff --git a/test/models/v2/server.spec.ts b/test/models/v2/server.spec.ts index f724b0276..861e6df3f 100644 --- a/test/models/v2/server.spec.ts +++ b/test/models/v2/server.spec.ts @@ -21,8 +21,8 @@ const doc = { } } }; -const docItem = new Server('development', doc.development); -const emptyItem = new Server('', {}); +const docItem = new Server(doc.development, { asyncapi: {} as any, pointer: '', id: 'development' }); +const emptyItem = new Server({}, { asyncapi: {} as any, pointer: '', id: '' }); describe('Server Model', function () { describe('.id()', function () { @@ -72,7 +72,7 @@ describe('Server Model', function () { describe('.security()', function() { it('should return collection of security requirements', function() { const doc = { security: [ { requirement: [] } ] }; - const d = new Server('trait', doc); + const d = new Server(doc); expect(Array.isArray(d.security())).toEqual(true); expect(d.security()).toHaveLength(1); expect(typeof d.security()[0]).toEqual('object'); @@ -82,7 +82,7 @@ describe('Server Model', function () { it('should return collection of security requirements when value is undefined', function() { const doc = {}; - const d = new Server('trait', doc); + const d = new Server(doc); expect(Array.isArray(d.security())).toEqual(true); expect(d.security()).toHaveLength(0); }); diff --git a/test/models/v2/servers.spec.ts b/test/models/v2/servers.spec.ts index 122ad7d09..e329bc0c3 100644 --- a/test/models/v2/servers.spec.ts +++ b/test/models/v2/servers.spec.ts @@ -8,7 +8,7 @@ const doc = { url: 'development.gigantic-server.com' } }; -const docItem = new Server('development', doc.development); +const docItem = new Server(doc.development, { asyncapi: {} as any, pointer: '', id: 'development' }); describe('Servers model', function () { describe('.isEmpty()', function () {