-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathprompt.test.ts
More file actions
336 lines (271 loc) · 7.48 KB
/
prompt.test.ts
File metadata and controls
336 lines (271 loc) · 7.48 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { cursor } from 'sisteransi';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { default as Prompt } from '../../src/prompts/prompt.js';
import { isCancel } from '../../src/utils/index.js';
import { MockReadable } from '../mock-readable.js';
import { MockWritable } from '../mock-writable.js';
describe('Prompt', () => {
let input: MockReadable;
let output: MockWritable;
beforeEach(() => {
input = new MockReadable();
output = new MockWritable();
});
afterEach(() => {
vi.restoreAllMocks();
});
test('renders render() result', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
// leave the promise hanging since we don't want to submit in this test
instance.prompt();
expect(output.buffer).to.deep.equal([cursor.hide, 'foo']);
});
test('submits on return key', async () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
const resultPromise = instance.prompt();
input.emit('keypress', '', { name: 'return' });
const result = await resultPromise;
expect(result).to.equal(undefined);
expect(isCancel(result)).to.equal(false);
expect(instance.state).to.equal('submit');
expect(output.buffer).to.deep.equal([cursor.hide, 'foo', '\n', cursor.show]);
});
test('cancels on ctrl-c', async () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
const resultPromise = instance.prompt();
input.emit('keypress', '\x03', { name: 'c' });
const result = await resultPromise;
expect(isCancel(result)).to.equal(true);
expect(instance.state).to.equal('cancel');
expect(output.buffer).to.deep.equal([cursor.hide, 'foo', '\n', cursor.show]);
});
test('writes initialValue to value', () => {
const eventSpy = vi.fn();
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'bananas',
});
instance.on('value', eventSpy);
instance.prompt();
expect(instance.value).to.equal('bananas');
expect(eventSpy).toHaveBeenCalled();
});
test('re-renders on resize', () => {
const renderFn = vi.fn().mockImplementation(() => 'foo');
const instance = new Prompt({
input,
output,
render: renderFn,
});
instance.prompt();
expect(renderFn).toHaveBeenCalledTimes(1);
output.emit('resize');
expect(renderFn).toHaveBeenCalledTimes(2);
});
test('state is active after first render', async () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
expect(instance.state).to.equal('initial');
instance.prompt();
expect(instance.state).to.equal('active');
});
test('emits truthy confirm on y press', () => {
const eventFn = vi.fn();
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
instance.on('confirm', eventFn);
instance.prompt();
input.emit('keypress', 'y', { name: 'y' });
expect(eventFn).toBeCalledWith(true);
});
test('emits falsey confirm on n press', () => {
const eventFn = vi.fn();
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
instance.on('confirm', eventFn);
instance.prompt();
input.emit('keypress', 'n', { name: 'n' });
expect(eventFn).toBeCalledWith(false);
});
test('sets value as placeholder on tab if one is set', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
placeholder: 'piwa',
});
instance.prompt();
input.emit('keypress', '\t', { name: 'tab' });
expect(instance.value).to.equal('piwa');
});
test('does not set placeholder value on tab if value already set', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
placeholder: 'piwa',
initialValue: 'trzy',
});
instance.prompt();
input.emit('keypress', '\t', { name: 'tab' });
expect(instance.value).to.equal('trzy');
});
test('emits key event for unknown chars', () => {
const eventSpy = vi.fn();
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
instance.on('key', eventSpy);
instance.prompt();
input.emit('keypress', 'z', { name: 'z' });
expect(eventSpy).toBeCalledWith('z', { name: 'z' });
});
test('emits cursor events for movement keys', () => {
const keys = ['up', 'down', 'left', 'right'];
const eventSpy = vi.fn();
const instance = new Prompt({
input,
output,
render: () => 'foo',
});
instance.on('cursor', eventSpy);
instance.prompt();
for (const key of keys) {
input.emit('keypress', key, { name: key });
expect(eventSpy).toBeCalledWith(key);
}
});
test('emits cursor events for movement key aliases when not tracking', () => {
const keys = [
['k', 'up'],
['j', 'down'],
['h', 'left'],
['l', 'right'],
];
const eventSpy = vi.fn();
const instance = new Prompt(
{
input,
output,
render: () => 'foo',
},
false
);
instance.on('cursor', eventSpy);
instance.prompt();
for (const [alias, key] of keys) {
input.emit('keypress', alias, { name: alias });
expect(eventSpy).toBeCalledWith(key);
}
});
test('aborts on abort signal', () => {
const abortController = new AbortController();
const instance = new Prompt({
input,
output,
render: () => 'foo',
signal: abortController.signal,
});
instance.prompt();
expect(instance.state).to.equal('active');
abortController.abort();
expect(instance.state).to.equal('cancel');
});
test('returns immediately if signal is already aborted', () => {
const abortController = new AbortController();
abortController.abort();
const instance = new Prompt({
input,
output,
render: () => 'foo',
signal: abortController.signal,
});
instance.prompt();
expect(instance.state).to.equal('cancel');
});
test('validates initial value on prompt start', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'invalid',
validate: (value) => value === 'valid' ? undefined : 'must be valid',
});
instance.prompt();
expect(instance.state).to.equal('error');
expect(instance.error).to.equal('must be valid');
});
test('accepts valid initial value', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'valid',
validate: (value) => value === 'valid' ? undefined : 'must be valid',
});
instance.prompt();
expect(instance.state).to.equal('active');
expect(instance.error).to.equal('');
});
test('validates initial value with Error object', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'invalid',
validate: (value) => value === 'valid' ? undefined : new Error('must be valid'),
});
instance.prompt();
expect(instance.state).to.equal('error');
expect(instance.error).to.equal('must be valid');
});
test('validates initial value with regex validation', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'Invalid Value $$$',
validate: (value) => /^[A-Z]+$/.test(value) ? undefined : 'Invalid value',
});
instance.prompt();
expect(instance.state).to.equal('error');
expect(instance.error).to.equal('Invalid value');
});
test('accepts valid initial value with regex validation', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'VALID',
validate: (value) => /^[A-Z]+$/.test(value) ? undefined : 'Invalid value',
});
instance.prompt();
expect(instance.state).to.equal('active');
expect(instance.error).to.equal('');
});
});