-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
checkbox.test.js
356 lines (303 loc) · 10.6 KB
/
checkbox.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { vi, expect, beforeEach, describe, it } from 'vitest';
import ReadlineStub from '../../helpers/readline.js';
import fixtures from '../../helpers/fixtures.js';
import Checkbox from '../../../lib/prompts/checkbox.js';
describe('`checkbox` prompt', () => {
let fixture;
let rl;
let checkbox;
beforeEach(() => {
fixture = { ...fixtures.checkbox };
rl = new ReadlineStub();
checkbox = new Checkbox(fixture, rl);
});
it('should return a single selected choice in an array', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 1');
done();
});
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
it('should return multiples selected choices in an array', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer.length).toEqual(2);
expect(answer[0]).toEqual('choice 1');
expect(answer[1]).toEqual('choice 2');
done();
});
rl.input.emit('keypress', ' ', { name: 'space' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
it('should check defaults choices', () =>
new Promise((done) => {
fixture.choices = [
{ name: '1', checked: true },
{ name: '2', checked: false },
{ name: '3', checked: false },
];
checkbox = new Checkbox(fixture, rl);
checkbox.run().then((answer) => {
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('1');
done();
});
rl.emit('line');
}));
it('provide an array of checked choice to validate', () => {
fixture.choices = [
{ name: '1', checked: true },
{ name: '2', checked: 1 },
{ name: '3', checked: false },
];
fixture.validate = function (answer) {
expect(answer).toEqual(['1', '2']);
return true;
};
checkbox = new Checkbox(fixture, rl);
const promise = checkbox.run();
rl.emit('line');
return promise;
});
it('should check defaults choices if given as array of values', () =>
new Promise((done) => {
fixture.choices = [{ name: '1' }, { name: '2' }, { name: '3' }];
fixture.default = ['1', '3'];
checkbox = new Checkbox(fixture, rl);
checkbox.run().then((answer) => {
expect(answer.length).toEqual(2);
expect(answer[0]).toEqual('1');
expect(answer[1]).toEqual('3');
done();
});
rl.emit('line');
}));
it('should toggle choice when hitting space', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 1');
done();
});
rl.input.emit('keypress', ' ', { name: 'space' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
it('should allow for arrow navigation', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 2');
done();
});
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'up' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
it('should allow for vi-style navigation', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 2');
done();
});
rl.input.emit('keypress', 'j', { name: 'j' });
rl.input.emit('keypress', 'j', { name: 'j' });
rl.input.emit('keypress', 'k', { name: 'k' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
it('should allow for emacs-style navigation', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 2');
done();
});
rl.input.emit('keypress', 'n', { name: 'n', ctrl: true });
rl.input.emit('keypress', 'n', { name: 'n', ctrl: true });
rl.input.emit('keypress', 'p', { name: 'p', ctrl: true });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
it('should allow 1-9 shortcut key', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 2');
done();
});
rl.input.emit('keypress', '2');
rl.emit('line');
}));
it('should select all answers if <a> is pressed', () => {
const promise = checkbox.run();
rl.input.emit('keypress', 'a', { name: 'a' });
rl.emit('line');
return promise.then((answer) => {
expect(answer.length).toEqual(3);
});
});
it('should select no answers if <a> is pressed a second time', () => {
const promise = checkbox.run();
rl.input.emit('keypress', 'a', { name: 'a' });
rl.input.emit('keypress', 'a', { name: 'a' });
rl.emit('line');
return promise.then((answer) => {
expect(answer.length).toEqual(0);
});
});
it('should select the inverse of the current selection when <i> is pressed', () => {
const promise = checkbox.run();
rl.input.emit('keypress', 'i', { name: 'i' });
rl.emit('line');
return promise.then((answer) => {
expect(answer.length).toEqual(3);
});
});
it('pagination works with multiline choices', () =>
new Promise((done) => {
const multilineFixture = {
message: 'message',
name: 'name',
choices: ['a\n\n', 'b\n\n'],
};
const list = new Checkbox(multilineFixture, rl);
const spy = vi.spyOn(list.paginator, 'paginate');
list.run().then((answer) => {
const realIndexPosition1 = spy.mock.calls[0][1];
const realIndexPosition2 = spy.mock.calls[1][1];
// 'a\n\n': 0th index, but pagination at 2nd index position due to 2 extra newlines
expect(realIndexPosition1).toEqual(2);
// 'b\n\n': 1st index, but pagination at 5th index position due to 4 extra newlines
expect(realIndexPosition2).toEqual(5);
expect(answer[0]).toEqual('b\n\n');
done();
});
rl.input.emit('keypress', '', { name: 'down' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
describe('with disabled choices', () => {
beforeEach(() => {
fixture.choices.push(
{
name: 'dis1',
disabled: true,
},
{
name: 'dis2',
disabled: 'uh oh',
},
);
checkbox = new Checkbox(fixture, rl);
});
it('output disabled choices and custom messages', () => {
const promise = checkbox.run();
rl.emit('line');
return promise.then(() => {
expect(rl.output.__raw__).toContain('- dis1 (Disabled)');
expect(rl.output.__raw__).toContain('- dis2 (uh oh)');
});
});
it('skip disabled choices', () =>
new Promise((done) => {
checkbox.run().then((answer) => {
expect(answer[0]).toEqual('choice 1');
done();
});
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
}));
it("uncheck defaults choices who're disabled", () =>
new Promise((done) => {
fixture.choices = [{ name: '1', checked: true, disabled: true }, { name: '2' }];
checkbox = new Checkbox(fixture, rl);
checkbox.run().then((answer) => {
expect(answer.length).toEqual(0);
done();
});
rl.emit('line');
}));
it('disabled can be a function', () => {
fixture.choices = [
{
name: 'dis1',
disabled(answers) {
expect(answers.foo).toEqual('foo');
return true;
},
},
];
checkbox = new Checkbox(fixture, rl, { foo: 'foo' });
const promise = checkbox.run();
rl.emit('line');
promise.then(() => {
expect(rl.output.__raw__).toContain('- dis1 (Disabled)');
});
});
});
describe('going out of boundaries', () => {
describe('when loop undefined / true', () => {
it('loops to bottom when too far up', async () => {
const promise = checkbox.run();
rl.input.emit('keypress', null, { name: 'up' });
rl.input.emit('keypress', null, { name: 'up' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
const answer = await promise;
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 2');
});
it('loops to top when too far down', async () => {
const promise = checkbox.run();
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
const answer = await promise;
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 1');
});
});
describe('when loop: false', () => {
beforeEach(() => {
checkbox = new Checkbox(Object.assign(fixture, { loop: false }), rl);
});
it('stays at top when too far up', async () => {
const promise = checkbox.run();
rl.input.emit('keypress', null, { name: 'up' });
rl.input.emit('keypress', null, { name: 'up' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
const answer = await promise;
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 1');
});
it('stays at bottom when too far down', async () => {
const promise = checkbox.run();
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', null, { name: 'down' });
rl.input.emit('keypress', ' ', { name: 'space' });
rl.emit('line');
const answer = await promise;
expect(answer.length).toEqual(1);
expect(answer[0]).toEqual('choice 3');
});
});
});
});