Skip to content

Commit 33fad58

Browse files
smoyaderberg
authored andcommitted
refactor: add info and dependant models (#485)
1 parent 5be4fb5 commit 33fad58

File tree

9 files changed

+261
-1
lines changed

9 files changed

+261
-1
lines changed

src/models/asyncapi.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
import { BaseModel } from "./base";
2+
import { Info } from "./info";
23

3-
export class AsyncAPIDocument extends BaseModel {}
4+
export class AsyncAPIDocument extends BaseModel {
5+
version(): string {
6+
return this.json("asyncapi");
7+
}
8+
9+
info(): Info {
10+
return new Info(this.json("info"));
11+
}
12+
}

src/models/contact.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { BaseModel } from "./base";
2+
3+
export class Contact extends BaseModel {
4+
name(): string {
5+
return this.json("name");
6+
}
7+
8+
url(): string {
9+
return this.json("url");
10+
}
11+
12+
email(): string {
13+
return this.json("email");
14+
}
15+
}

src/models/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export * from './asyncapi';
22
export * from './base';
3+
export * from './contact';
4+
export * from './info';
5+
export * from './license';

src/models/info.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { BaseModel } from "./base";
2+
import { Contact } from "./contact";
3+
import { License } from "./license";
4+
5+
export class Info extends BaseModel {
6+
title(): string {
7+
return this.json("title");
8+
}
9+
10+
version(): string {
11+
return this.json("version");
12+
}
13+
14+
description(): string {
15+
return this.json("description");
16+
}
17+
18+
termsOfService(): string {
19+
return this.json("termsOfService");
20+
}
21+
22+
contact(): Contact | undefined {
23+
const doc = this.json("contact");
24+
return doc && new Contact(doc);
25+
}
26+
27+
license(): License | undefined {
28+
const doc = this.json("license");
29+
return doc && new License(doc);
30+
}
31+
}

src/models/license.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { BaseModel } from "./base";
2+
3+
export class License extends BaseModel {
4+
name(): string {
5+
return this.json("name");
6+
}
7+
8+
url(): string {
9+
return this.json("url");
10+
}
11+
}

test/models/asyncapi.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { AsyncAPIDocument } from '../../src/models/asyncapi';
2+
import { Info } from '../../src/models/info';
3+
4+
describe('AsyncAPIDocument model', function() {
5+
describe('.version()', function() {
6+
it('should return the value', function() {
7+
const doc = { asyncapi: "3.0.0" };
8+
const d = new AsyncAPIDocument(doc);
9+
expect(d.version()).toEqual(doc.asyncapi);
10+
});
11+
12+
it('should return undefined when there is no value', function() {
13+
const doc = { };
14+
const d = new AsyncAPIDocument(doc);
15+
expect(d.version()).toBeUndefined();
16+
});
17+
});
18+
19+
describe('.info()', function() {
20+
it('should return an Info object', function() {
21+
const doc = { info: { name: "LeChuck" } };
22+
const d = new AsyncAPIDocument(doc);
23+
expect(d.info() instanceof Info).toBeTruthy();
24+
});
25+
});
26+
});

test/models/contact.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Contact } from '../../src/models/contact';
2+
3+
describe('Contact model', function() {
4+
describe('.name()', function() {
5+
it('should return the value', function() {
6+
const doc = { name: "LeChuck" };
7+
const d = new Contact(doc);
8+
expect(d.name()).toEqual(doc.name);
9+
});
10+
11+
it('should return undefined when there is no value', function() {
12+
const doc = { };
13+
const d = new Contact(doc);
14+
expect(d.name()).toBeUndefined();
15+
});
16+
});
17+
18+
describe('.url()', function() {
19+
it('should return the value', function() {
20+
const doc = { url: "https://example.com" };
21+
const d = new Contact(doc);
22+
expect(d.url()).toEqual(doc.url);
23+
});
24+
25+
it('should return undefined when there is no value', function() {
26+
const doc = { };
27+
const d = new Contact(doc);
28+
expect(d.url()).toBeUndefined();
29+
});
30+
});
31+
32+
describe('.email()', function() {
33+
it('should return the value', function() {
34+
const doc = { email: "[email protected]" };
35+
const d = new Contact(doc);
36+
expect(d.email()).toEqual(doc.email);
37+
});
38+
39+
it('should return undefined when there is no value', function() {
40+
const doc = { };
41+
const d = new Contact(doc);
42+
expect(d.email()).toBeUndefined();
43+
});
44+
});
45+
});

test/models/info.spec.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Contact } from '../../src/models/contact';
2+
import { Info } from '../../src/models/info';
3+
import { License } from '../../src/models/license';
4+
5+
describe('Info model', function() {
6+
describe('.title()', function() {
7+
it('should return the value', function() {
8+
const doc = { title: "Example API" };
9+
const d = new Info(doc);
10+
expect(d.title()).toEqual(doc.title);
11+
});
12+
13+
it('should return undefined when there is no value', function() {
14+
const doc = { };
15+
const d = new Info(doc);
16+
expect(d.title()).toBeUndefined();
17+
});
18+
});
19+
20+
describe('.version()', function() {
21+
it('should return the value', function() {
22+
const doc = { version: "1.0.0" };
23+
const d = new Info(doc);
24+
expect(d.version()).toEqual(doc.version);
25+
});
26+
27+
it('should return undefined when there is no value', function() {
28+
const doc = { };
29+
const d = new Info(doc);
30+
expect(d.version()).toBeUndefined();
31+
});
32+
});
33+
34+
describe('.description()', function() {
35+
it('should return the value', function() {
36+
const doc = { description: "This is the API of Example" };
37+
const d = new Info(doc);
38+
expect(d.description()).toEqual(doc.description);
39+
});
40+
41+
it('should return undefined when there is no value', function() {
42+
const doc = { };
43+
const d = new Info(doc);
44+
expect(d.description()).toBeUndefined();
45+
});
46+
});
47+
48+
describe('.termsOfService()', function() {
49+
it('should return the value', function() {
50+
const doc = { termsOfService: "These are the terms of service" };
51+
const d = new Info(doc);
52+
expect(d.termsOfService()).toEqual(doc.termsOfService);
53+
});
54+
55+
it('should return undefined when there is no value', function() {
56+
const doc = { };
57+
const d = new Info(doc);
58+
expect(d.termsOfService()).toBeUndefined();
59+
});
60+
});
61+
62+
describe('.contact()', function() {
63+
it('should return a Contact object', function() {
64+
const doc = { contact: { name: "LeChuck" } };
65+
const d = new Info(doc);
66+
expect(d.contact() instanceof Contact).toBeTruthy();
67+
});
68+
69+
it('should return undefined when there is no value', function() {
70+
const doc = { };
71+
const d = new Info(doc);
72+
expect(d.contact()).toBeUndefined();
73+
});
74+
});
75+
76+
describe('.license()', function() {
77+
it('should return a License object', function() {
78+
const doc = { license: { name: "Apache 2.0" } };
79+
const d = new Info(doc);
80+
expect(d.license() instanceof License).toBeTruthy();
81+
});
82+
83+
it('should return undefined when there is no value', function() {
84+
const doc = { };
85+
const d = new Info(doc);
86+
expect(d.license()).toBeUndefined();
87+
});
88+
});
89+
});

test/models/license.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { License } from '../../src/models/license';
2+
3+
describe('License model', function() {
4+
describe('.name()', function() {
5+
it('should return the value', function() {
6+
const doc = { name: "Apache 2.0" };
7+
const d = new License(doc);
8+
expect(d.name()).toEqual(doc.name);
9+
});
10+
11+
it('should return undefined when there is no value', function() {
12+
const doc = { };
13+
const d = new License(doc);
14+
expect(d.name()).toBeUndefined();
15+
});
16+
});
17+
18+
describe('.url()', function() {
19+
it('should return the value', function() {
20+
const doc = { url: "https://www.apache.org/licenses/LICENSE-2.0.html" };
21+
const d = new License(doc);
22+
expect(d.url()).toEqual(doc.url);
23+
});
24+
25+
it('should return undefined when there is no value', function() {
26+
const doc = { };
27+
const d = new License(doc);
28+
expect(d.url()).toBeUndefined();
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)