-
Notifications
You must be signed in to change notification settings - Fork 2
/
extendTypes.test.js
146 lines (142 loc) · 4.2 KB
/
extendTypes.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
jest.unmock('../../src/features/extendTypes');
const { ExtendTypes } = require('../../src/features/extendTypes');
const { EVENT_NAMES } = require('../../src/constants');
describe('features:extendTypes', () => {
it('should register the listeners when instantiated', () => {
// Given
const events = {
on: jest.fn(),
};
let sut = null;
// When
sut = new ExtendTypes(events, EVENT_NAMES);
// Then
expect(sut).toBeInstanceOf(ExtendTypes);
expect(events.on).toHaveBeenCalledTimes(2);
expect(events.on).toHaveBeenCalledWith(EVENT_NAMES.newComment, expect.any(Function));
expect(events.on).toHaveBeenCalledWith(EVENT_NAMES.commentsReady, expect.any(Function));
});
it('should ignore comments that don\'t extend types nor use intersection', () => {
// Given
const comment = [
'/**',
' * @typedef {Daughter} Rosario',
' * @typedef {Daughter} Pilar',
' */',
].join('\n');
const source = `${comment} Something`;
const events = {
on: jest.fn(),
};
let sut = null;
let onComment = null;
let onCommentsReady = null;
let result = null;
// When
sut = new ExtendTypes(events, EVENT_NAMES);
[[, onComment], [, onCommentsReady]] = events.on.mock.calls;
onComment(comment);
result = onCommentsReady(source);
// Then
expect(sut).toBeInstanceOf(ExtendTypes); // to avoid `no-new`.
expect(result).toBe(source);
});
it('should transform an itersection into a union', () => {
// Given
const firstType = 'Object';
const secondType = 'SomeOtherType';
const definitionName = 'Child';
const comment = [
'/**',
` * @typedef {${firstType} & ${secondType}} ${definitionName}`,
' */',
].join('\n');
const content = ' Some other code';
const source = `${comment}${content}`;
const events = {
on: jest.fn(),
};
let sut = null;
let onComment = null;
let onCommentsReady = null;
let result = null;
const newComment = [
'/**',
` * @typedef {${firstType}|${secondType}} ${definitionName}`,
' */',
].join('\n');
// When
sut = new ExtendTypes(events, EVENT_NAMES);
[[, onComment], [, onCommentsReady]] = events.on.mock.calls;
onComment(comment);
result = onCommentsReady(source);
// Then
expect(sut).toBeInstanceOf(ExtendTypes); // to avoid `no-new`.
expect(result).toBe(`${newComment}${content}`);
});
it('should transform an intersection into an extension', () => {
// Given
const sharedLines = [
' * @memberof module:people',
];
const extendedProperiesLines = [
' * @property {number} name',
' * @property {number} age',
' * @property {number} height',
];
const extendedType = 'Human';
const baseType = 'Entity';
const comment = [
'/**',
` * @typedef {${baseType} & ${extendedType}Properties} ${extendedType}`,
...sharedLines,
' */',
].join('\n');
const propertiesLines = [
'/**',
` * @typedef {Object} ${extendedType}Properties`,
...extendedProperiesLines,
...sharedLines,
` * @augments ${extendedType}`,
' */',
];
const propertiesComment = propertiesLines.join('\n');
const content = 'Some other code';
const source = [
comment,
propertiesComment,
content,
].join('\n');
const events = {
on: jest.fn(),
};
let sut = null;
let onComment = null;
let onCommentsReady = null;
let result = null;
const newComment = [
'/**',
` * @typedef {${baseType}} ${extendedType}`,
...sharedLines,
...extendedProperiesLines,
' */',
].join('\n');
const emptyBlock = new Array(propertiesLines.length - extendedProperiesLines.length)
.fill('')
.join('\n');
const expectedResult = [
newComment,
emptyBlock,
content,
].join('\n');
// When
sut = new ExtendTypes(events, EVENT_NAMES);
[[, onComment], [, onCommentsReady]] = events.on.mock.calls;
onComment(comment);
onComment(propertiesComment);
result = onCommentsReady(source);
// Then
expect(sut).toBeInstanceOf(ExtendTypes); // to avoid `no-new`.
expect(result).toBe(expectedResult);
});
});