Skip to content

Commit

Permalink
refactor: add info and dependant models (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Mar 9, 2022
1 parent e40fafc commit d3d0a3e
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/models/asyncapi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { BaseModel } from "./base";
import { Info } from "./info";

export class AsyncAPIDocument extends BaseModel {}
export class AsyncAPIDocument extends BaseModel {
version(): string {
return this.json("asyncapi");
}

info(): Info {
return new Info(this.json("info"));
}
}
15 changes: 15 additions & 0 deletions src/models/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BaseModel } from "./base";

export class Contact extends BaseModel {
name(): string {
return this.json("name");
}

url(): string {
return this.json("url");
}

email(): string {
return this.json("email");
}
}
3 changes: 3 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './asyncapi';
export * from './base';
export * from './contact';
export * from './info';
export * from './license';
31 changes: 31 additions & 0 deletions src/models/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { BaseModel } from "./base";
import { Contact } from "./contact";
import { License } from "./license";

export class Info extends BaseModel {
title(): string {
return this.json("title");
}

version(): string {
return this.json("version");
}

description(): string {
return this.json("description");
}

termsOfService(): string {
return this.json("termsOfService");
}

contact(): Contact | undefined {
const doc = this.json("contact");
return doc && new Contact(doc);
}

license(): License | undefined {
const doc = this.json("license");
return doc && new License(doc);
}
}
11 changes: 11 additions & 0 deletions src/models/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BaseModel } from "./base";

export class License extends BaseModel {
name(): string {
return this.json("name");
}

url(): string {
return this.json("url");
}
}
26 changes: 26 additions & 0 deletions test/models/asyncapi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AsyncAPIDocument } from '../../src/models/asyncapi';
import { Info } from '../../src/models/info';

describe('AsyncAPIDocument model', function() {
describe('.version()', function() {
it('should return the value', function() {
const doc = { asyncapi: "3.0.0" };
const d = new AsyncAPIDocument(doc);
expect(d.version()).toEqual(doc.asyncapi);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new AsyncAPIDocument(doc);
expect(d.version()).toBeUndefined();
});
});

describe('.info()', function() {
it('should return an Info object', function() {
const doc = { info: { name: "LeChuck" } };
const d = new AsyncAPIDocument(doc);
expect(d.info() instanceof Info).toBeTruthy();
});
});
});
45 changes: 45 additions & 0 deletions test/models/contact.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Contact } from '../../src/models/contact';

describe('Contact model', function() {
describe('.name()', function() {
it('should return the value', function() {
const doc = { name: "LeChuck" };
const d = new Contact(doc);
expect(d.name()).toEqual(doc.name);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Contact(doc);
expect(d.name()).toBeUndefined();
});
});

describe('.url()', function() {
it('should return the value', function() {
const doc = { url: "https://example.com" };
const d = new Contact(doc);
expect(d.url()).toEqual(doc.url);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Contact(doc);
expect(d.url()).toBeUndefined();
});
});

describe('.email()', function() {
it('should return the value', function() {
const doc = { email: "[email protected]" };
const d = new Contact(doc);
expect(d.email()).toEqual(doc.email);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Contact(doc);
expect(d.email()).toBeUndefined();
});
});
});
89 changes: 89 additions & 0 deletions test/models/info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Contact } from '../../src/models/contact';
import { Info } from '../../src/models/info';
import { License } from '../../src/models/license';

describe('Info model', function() {
describe('.title()', function() {
it('should return the value', function() {
const doc = { title: "Example API" };
const d = new Info(doc);
expect(d.title()).toEqual(doc.title);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Info(doc);
expect(d.title()).toBeUndefined();
});
});

describe('.version()', function() {
it('should return the value', function() {
const doc = { version: "1.0.0" };
const d = new Info(doc);
expect(d.version()).toEqual(doc.version);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Info(doc);
expect(d.version()).toBeUndefined();
});
});

describe('.description()', function() {
it('should return the value', function() {
const doc = { description: "This is the API of Example" };
const d = new Info(doc);
expect(d.description()).toEqual(doc.description);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Info(doc);
expect(d.description()).toBeUndefined();
});
});

describe('.termsOfService()', function() {
it('should return the value', function() {
const doc = { termsOfService: "These are the terms of service" };
const d = new Info(doc);
expect(d.termsOfService()).toEqual(doc.termsOfService);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Info(doc);
expect(d.termsOfService()).toBeUndefined();
});
});

describe('.contact()', function() {
it('should return a Contact object', function() {
const doc = { contact: { name: "LeChuck" } };
const d = new Info(doc);
expect(d.contact() instanceof Contact).toBeTruthy();
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Info(doc);
expect(d.contact()).toBeUndefined();
});
});

describe('.license()', function() {
it('should return a License object', function() {
const doc = { license: { name: "Apache 2.0" } };
const d = new Info(doc);
expect(d.license() instanceof License).toBeTruthy();
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new Info(doc);
expect(d.license()).toBeUndefined();
});
});
});
31 changes: 31 additions & 0 deletions test/models/license.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { License } from '../../src/models/license';

describe('License model', function() {
describe('.name()', function() {
it('should return the value', function() {
const doc = { name: "Apache 2.0" };
const d = new License(doc);
expect(d.name()).toEqual(doc.name);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new License(doc);
expect(d.name()).toBeUndefined();
});
});

describe('.url()', function() {
it('should return the value', function() {
const doc = { url: "https://www.apache.org/licenses/LICENSE-2.0.html" };
const d = new License(doc);
expect(d.url()).toEqual(doc.url);
});

it('should return undefined when there is no value', function() {
const doc = { };
const d = new License(doc);
expect(d.url()).toBeUndefined();
});
});
});

0 comments on commit d3d0a3e

Please sign in to comment.