Skip to content

Commit

Permalink
refactor: simplify operation traits based on parser-api (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Sep 5, 2022
1 parent cc46e8f commit bba5972
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
3 changes: 0 additions & 3 deletions src/models/operation-trait.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { BaseModel } from "./base";
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';
import type { OperationAction } from "./operation";
import type { SecuritySchemeInterface } from "./security-scheme";
import { SecurityRequirements } from "./v2/security-requirements";

export interface OperationTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
id(): string;
action(): OperationAction;
hasOperationId(): boolean;
operationId(): string | undefined;
hasSummary(): boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/models/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type { ServersInterface } from "./servers";
export type OperationAction = 'send' | 'receive' | 'publish' | 'subscribe';

export interface OperationInterface extends BaseModel, OperationTraitInterface {
action(): OperationAction;
isSend(): boolean;
isReceive(): boolean;
servers(): ServersInterface;
channels(): ChannelsInterface;
messages(): MessagesInterface;
Expand Down
8 changes: 8 additions & 0 deletions src/models/v2/operation-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export class OperationTrait<J extends v2.OperationTraitObject = v2.OperationTrai
return hasExternalDocs(this);
}

isSend(): boolean {
return this.action() === 'subscribe';
}

isReceive(): boolean {
return this.action() === 'publish';
}

externalDocs(): ExternalDocumentationInterface | undefined {
return externalDocs(this);
}
Expand Down
8 changes: 0 additions & 8 deletions test/models/v2/operation-trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ describe('OperationTrait model', function() {
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.action()).toEqual('publish');
});
});

describe('.hasOperationId()', function() {
it('should return true when there is a value', function() {
const doc = { operationId: '...' };
Expand Down
36 changes: 36 additions & 0 deletions test/models/v2/operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ describe('Operation model', function() {
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.action()).toEqual('publish');
});
});

describe('.isSend()', function() {
it('should return true when operation is subscribe', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isSend()).toBeTruthy();
});

it('should return false when operation is publish', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isSend()).toBeFalsy();
});
});

describe('.isReceive()', function() {
it('should return true when operation is publish', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isReceive()).toBeTruthy();
});

it('should return false when operation is subscribe', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isReceive()).toBeFalsy();
});
});

describe('.messages()', function() {
it('should return collection of messages - single message', function() {
const doc = { message: { messageId: '...' } };
Expand Down

0 comments on commit bba5972

Please sign in to comment.