forked from OpenAPITools/openapi-generator-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-manager.controller.spec.ts
More file actions
314 lines (272 loc) · 9.59 KB
/
version-manager.controller.spec.ts
File metadata and controls
314 lines (272 loc) · 9.59 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { Test } from '@nestjs/testing';
import { VersionManagerController } from './version-manager.controller';
import { CommandMock } from '../mocks/command.mock';
import { COMMANDER_PROGRAM, LOGGER } from '../constants';
import { UIService, VersionManagerService } from '../services';
import { of } from 'rxjs';
import chalk from 'chalk';
jest.mock('fs-extra');
describe('VersionManagerController', () => {
let commandMock: CommandMock;
const log = jest.fn();
const uiServiceMock = {
table: jest.fn(),
list: jest.fn(),
};
const versionManagerServiceMock = {
setSelectedVersion: jest.fn(),
isSelectedVersion: jest.fn(),
downloadIfNeeded: jest.fn(),
download: jest.fn(),
remove: jest.fn(),
search: jest.fn(),
};
beforeEach(async () => {
commandMock = new CommandMock();
[
log,
...Object.values(uiServiceMock),
...Object.values(versionManagerServiceMock),
].forEach((spy) => spy.mockReset());
await Test.createTestingModule({
controllers: [VersionManagerController],
providers: [
{ provide: UIService, useValue: uiServiceMock },
{ provide: VersionManagerService, useValue: versionManagerServiceMock },
{ provide: COMMANDER_PROGRAM, useValue: commandMock },
{ provide: LOGGER, useValue: { log } },
],
}).compile();
});
it('adds 3 commands, but 2 actions', () => {
expect(commandMock.action).toHaveBeenCalledTimes(2);
expect(commandMock.command).toHaveBeenCalledTimes(3);
expect(commandMock.description).toHaveBeenCalledTimes(3);
});
describe('commands', function () {
let cmd: (typeof commandMock.commands)[string];
describe('list', () => {
const versions = [
{
version: '1.2.3',
installed: true,
versionTags: ['latest', 'stable', '1'],
releaseDate: new Date('2020-09-24T14:30:15.189Z'),
},
{
version: '1.2.4',
installed: true,
versionTags: ['stable', '1'],
releaseDate: new Date('2020-09-21T14:30:15.189Z'),
},
{
version: '0.5.6',
installed: false,
versionTags: ['beta', '4.5'],
releaseDate: new Date('2020-09-22T14:30:15.189Z'),
},
];
beforeEach(() => {
versionManagerServiceMock.search.mockReturnValue(of(versions));
cmd = commandMock.commands['list [versionTags...]'];
});
it('has the correct description', () => {
expect(cmd.description).toEqual('lists all published versions');
});
it('has the options', () =>
expect(cmd.options).toEqual([
{
flags: '-j, --json',
description: 'print as json',
defaultValue: false,
},
]));
describe('the --json flag is set', () => {
beforeEach(async () => {
commandMock.refs['list [versionTags...]'].opts.mockReturnValue({
json: true,
});
await cmd.action(['tag1', 'tag2']);
});
it.each([
'isSelectedVersion',
'download',
'remove',
'setSelectedVersion',
])('does not call version manager %s', (fn) => {
expect(versionManagerServiceMock[fn]).toHaveBeenCalledTimes(0);
});
it('does not print a table', () => {
expect(uiServiceMock.table).toHaveBeenCalledTimes(0);
});
it('does not print a list', () => {
expect(uiServiceMock.list).toHaveBeenCalledTimes(0);
});
it('prints the result as json', () => {
expect(log).toHaveBeenNthCalledWith(
1,
JSON.stringify(versions, null, 2),
);
});
});
describe('the --json flag is not set', () => {
const whatsNextSpy = jest.fn();
beforeEach(async () => {
whatsNextSpy.mockReset();
uiServiceMock.table.mockReset().mockResolvedValue(versions[0]);
uiServiceMock.list.mockReset().mockResolvedValue(whatsNextSpy);
versionManagerServiceMock.isSelectedVersion.mockImplementationOnce(
(v) => v === '1.2.3',
);
commandMock.refs['list [versionTags...]'].opts
.mockReset()
.mockReturnValue({ json: false });
await cmd.action(['tag1', 'tag2']);
});
it('prints the table once', () => {
expect(uiServiceMock.table).toHaveBeenNthCalledWith(1, {
printColNum: false,
message: 'The following releases are available:',
rows: [
{
value: versions[0],
short: versions[0].version,
row: {
'☐': '☒',
releasedAt: '2020-09-24',
version: chalk.yellow(versions[0].version),
versionTags: `${chalk.green('latest')} stable 1`,
installed: chalk.green('yes'),
},
},
{
value: versions[1],
short: versions[1].version,
row: {
'☐': '☐',
releasedAt: '2020-09-21',
version: chalk.yellow(versions[1].version),
versionTags: `stable 1`,
installed: chalk.green('yes'),
},
},
{
value: versions[2],
short: versions[2].version,
row: {
'☐': '☐',
releasedAt: '2020-09-22',
version: chalk.gray(versions[2].version),
versionTags: `beta 4.5`,
installed: chalk.red('no'),
},
},
],
});
});
it('prints "No results for ..., if no versions are found', async () => {
versionManagerServiceMock.search.mockReturnValue(of([]));
await cmd.action(['tag1', 'tag2']);
expect(log).toHaveBeenNthCalledWith(
1,
chalk.red('No results for: tag1 tag2'),
);
});
describe('the selection is installed and in use', () => {
let choices: Array<{
name: Record<string, unknown>;
value: () => unknown;
}>;
beforeEach(async () => {
whatsNextSpy.mockReset();
uiServiceMock.list.mockClear().mockImplementation((opts) => {
choices = opts.choices;
return whatsNextSpy;
});
uiServiceMock.table.mockReset().mockResolvedValue(versions[0]);
versionManagerServiceMock.isSelectedVersion.mockReturnValue(true);
await cmd.action(['tag1', 'tag2']);
});
it('provides the correct choices', () => {
expect(choices).toHaveLength(1);
expect(choices[0].name).toEqual('exit');
});
});
describe('the selection is installed but not in use', () => {
let choices: Array<{
name: Record<string, unknown>;
value: () => unknown;
}>;
beforeEach(async () => {
whatsNextSpy.mockReset();
uiServiceMock.list.mockClear().mockImplementation((opts) => {
choices = opts.choices;
return whatsNextSpy;
});
uiServiceMock.table.mockReset().mockResolvedValue(versions[1]);
versionManagerServiceMock.isSelectedVersion.mockReturnValue(false);
await cmd.action(['tag1', 'tag2']);
});
it('provides the correct choices', () => {
expect(choices).toHaveLength(3);
expect(choices.map(({ name }) => name)).toEqual([
chalk.green('use'),
chalk.red('remove'),
'exit',
]);
});
});
describe('the selection is not installed and not in use', () => {
let choices: Array<{
name: Record<string, unknown>;
value: () => unknown;
}>;
beforeEach(async () => {
whatsNextSpy.mockReset();
uiServiceMock.list.mockClear().mockImplementation((opts) => {
choices = opts.choices;
return whatsNextSpy;
});
uiServiceMock.table.mockReset().mockResolvedValue(versions[2]);
versionManagerServiceMock.isSelectedVersion.mockReturnValue(false);
await cmd.action(['tag1', 'tag2']);
});
it('provides the correct choices', () => {
expect(choices).toHaveLength(3);
expect(choices.map(({ name }) => name)).toEqual([
chalk.green('use'),
chalk.yellow('download'),
'exit',
]);
});
});
});
});
describe('set', () => {
beforeEach(() => {
cmd = commandMock.commands['set <versionTags...>'];
});
it('sets version[0] from the list', async () => {
versionManagerServiceMock.search.mockReturnValue(
of([
{ version: '1.2.3' },
{ version: '1.2.4' },
{ version: '0.5.6' },
]),
);
await cmd.action(['tag1', 'tag2']);
expect(
versionManagerServiceMock.setSelectedVersion,
).toHaveBeenNthCalledWith(1, '1.2.3');
});
it('prints a message, if the search result is empty', async () => {
versionManagerServiceMock.search.mockReturnValue(of([]));
await cmd.action(['tag1', 'tag2']);
expect(log).toHaveBeenNthCalledWith(
1,
chalk.red('Unable to find version matching criteria "tag1 tag2"'),
);
});
});
});
});