Skip to content

Commit 1bc6efd

Browse files
authored
chore: add wrapper class (#26)
* add wrapper class * add jsdoc comment * rename file to `asyncapidiff.ts` * rename variables
1 parent 85ee82e commit 1bc6efd

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"start": "tsc --watch",
99
"build": "tsc",
1010
"test": "jest",
11+
"test:coverage": "jest --coverage",
1112
"test:watch": "jest --watch",
1213
"lint": "eslint --max-warnings 0 --config .eslintrc .",
1314
"lint:fix": "eslint --max-warnings 0 --config .eslintrc . --fix",

src/asyncapidiff.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Output, DiffOutputItem } from './types';
2+
3+
/**
4+
* Implements functions to deal with the diff.
5+
* @class
6+
* @returns {AsyncAPIDiff}
7+
*/
8+
export default class AsyncAPIDiff {
9+
private output: Output;
10+
11+
constructor(output: string) {
12+
// output is a stringified JSON
13+
this.output= JSON.parse(output);
14+
}
15+
16+
/**
17+
* @returns All the breaking changes
18+
*/
19+
breaking(): DiffOutputItem[] {
20+
return this.output.changes.filter((diff) => diff.type === 'breaking');
21+
}
22+
23+
/**
24+
* @returns All the non-breaking changes
25+
*/
26+
nonBreaking(): DiffOutputItem[] {
27+
return this.output.changes.filter((diff) => diff.type === 'non-breaking');
28+
}
29+
30+
/**
31+
* @returns All the unclassified changes
32+
*/
33+
unclassified(): DiffOutputItem[] {
34+
return this.output.changes.filter((diff) => diff.type === 'unclassified');
35+
}
36+
37+
/**
38+
* @returns The JSON output
39+
*/
40+
getOutput(): Output {
41+
return this.output;
42+
}
43+
}

test/asycnapidiff.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import AsyncAPIDiff from '../src/asyncapidiff';
2+
3+
import {
4+
breakingChanges,
5+
inputDiff,
6+
nonbreakingChanges,
7+
unclassifiedChanges,
8+
} from './fixtures/asyncapidiff.fixtures';
9+
10+
describe('AsyncAPIDiff wrapper', () => {
11+
test('checks the instance', () => {
12+
expect(new AsyncAPIDiff(JSON.stringify(inputDiff))).toBeInstanceOf(
13+
AsyncAPIDiff
14+
);
15+
});
16+
17+
test('checks the original output', () => {
18+
const diff = new AsyncAPIDiff(JSON.stringify(inputDiff));
19+
expect(diff.getOutput()).toEqual(inputDiff);
20+
});
21+
22+
test('returns breaking changes', () => {
23+
const diff = new AsyncAPIDiff(JSON.stringify(inputDiff));
24+
expect(diff.breaking()).toEqual(breakingChanges);
25+
});
26+
27+
test('returns non-breaking changes', () => {
28+
const diff = new AsyncAPIDiff(JSON.stringify(inputDiff));
29+
expect(diff.nonBreaking()).toEqual(nonbreakingChanges);
30+
});
31+
32+
test('returns unclassified changes', () => {
33+
const diff = new AsyncAPIDiff(JSON.stringify(inputDiff));
34+
expect(diff.unclassified()).toEqual(unclassifiedChanges);
35+
});
36+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const inputDiff = {
2+
changes: [
3+
{
4+
type: 'breaking',
5+
path: '/servers',
6+
},
7+
{
8+
type: 'non-breaking',
9+
path: '/channels',
10+
},
11+
{
12+
type: 'unclassified',
13+
path: '/info',
14+
},
15+
],
16+
};
17+
18+
export const breakingChanges = [
19+
{
20+
type: 'breaking',
21+
path: '/servers',
22+
},
23+
];
24+
25+
export const nonbreakingChanges = [
26+
{
27+
type: 'non-breaking',
28+
path: '/channels',
29+
},
30+
];
31+
32+
export const unclassifiedChanges = [
33+
{
34+
type: 'unclassified',
35+
path: '/info',
36+
},
37+
];

0 commit comments

Comments
 (0)