Skip to content

Commit 68bbf74

Browse files
authored
test: add tests for validation errors (#269)
1 parent 7a556ad commit 68bbf74

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

packages/prompts/src/__snapshots__/index.test.ts.snap

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ exports[`prompts (isCI = false) > text > can cancel 1`] = `
123123
]
124124
`;
125125

126+
exports[`prompts (isCI = false) > text > defaultValue sets the value but does not render 1`] = `
127+
[
128+
"[?25l",
129+
"│
130+
◆ foo
131+
│ _
132+
└
133+
",
134+
"",
135+
"",
136+
"",
137+
"◇ foo
138+
│ bar",
139+
"
140+
",
141+
"[?25h",
142+
]
143+
`;
144+
126145
exports[`prompts (isCI = false) > text > renders cancelled value if one set 1`] = `
127146
[
128147
"[?25l",
@@ -220,6 +239,82 @@ exports[`prompts (isCI = false) > text > renders submitted value 1`] = `
220239
]
221240
`;
222241

242+
exports[`prompts (isCI = false) > text > validation errors render and clear (using Error) 1`] = `
243+
[
244+
"[?25l",
245+
"│
246+
◆ foo
247+
│ _
248+
└
249+
",
250+
"",
251+
"",
252+
"",
253+
"│ x█",
254+
"",
255+
"",
256+
"",
257+
"",
258+
"▲ foo
259+
│ x█
260+
└ should be xy
261+
",
262+
"",
263+
"",
264+
"",
265+
"◆ foo
266+
│ xy█
267+
└
268+
",
269+
"",
270+
"",
271+
"",
272+
"◇ foo
273+
│ xy",
274+
"
275+
",
276+
"[?25h",
277+
]
278+
`;
279+
280+
exports[`prompts (isCI = false) > text > validation errors render and clear 1`] = `
281+
[
282+
"[?25l",
283+
"│
284+
◆ foo
285+
│ _
286+
└
287+
",
288+
"",
289+
"",
290+
"",
291+
"│ x█",
292+
"",
293+
"",
294+
"",
295+
"",
296+
"▲ foo
297+
│ x█
298+
└ should be xy
299+
",
300+
"",
301+
"",
302+
"",
303+
"◆ foo
304+
│ xy█
305+
└
306+
",
307+
"",
308+
"",
309+
"",
310+
"◇ foo
311+
│ xy",
312+
"
313+
",
314+
"[?25h",
315+
]
316+
`;
317+
223318
exports[`prompts (isCI = true) > spinner > message > sets message for next frame 1`] = `
224319
[
225320
"[?25l",
@@ -344,6 +439,25 @@ exports[`prompts (isCI = true) > text > can cancel 1`] = `
344439
]
345440
`;
346441

442+
exports[`prompts (isCI = true) > text > defaultValue sets the value but does not render 1`] = `
443+
[
444+
"[?25l",
445+
"│
446+
◆ foo
447+
│ _
448+
└
449+
",
450+
"",
451+
"",
452+
"",
453+
"◇ foo
454+
│ bar",
455+
"
456+
",
457+
"[?25h",
458+
]
459+
`;
460+
347461
exports[`prompts (isCI = true) > text > renders cancelled value if one set 1`] = `
348462
[
349463
"[?25l",
@@ -440,3 +554,79 @@ exports[`prompts (isCI = true) > text > renders submitted value 1`] = `
440554
"[?25h",
441555
]
442556
`;
557+
558+
exports[`prompts (isCI = true) > text > validation errors render and clear (using Error) 1`] = `
559+
[
560+
"[?25l",
561+
"│
562+
◆ foo
563+
│ _
564+
└
565+
",
566+
"",
567+
"",
568+
"",
569+
"│ x█",
570+
"",
571+
"",
572+
"",
573+
"",
574+
"▲ foo
575+
│ x█
576+
└ should be xy
577+
",
578+
"",
579+
"",
580+
"",
581+
"◆ foo
582+
│ xy█
583+
└
584+
",
585+
"",
586+
"",
587+
"",
588+
"◇ foo
589+
│ xy",
590+
"
591+
",
592+
"[?25h",
593+
]
594+
`;
595+
596+
exports[`prompts (isCI = true) > text > validation errors render and clear 1`] = `
597+
[
598+
"[?25l",
599+
"│
600+
◆ foo
601+
│ _
602+
└
603+
",
604+
"",
605+
"",
606+
"",
607+
"│ x█",
608+
"",
609+
"",
610+
"",
611+
"",
612+
"▲ foo
613+
│ x█
614+
└ should be xy
615+
",
616+
"",
617+
"",
618+
"",
619+
"◆ foo
620+
│ xy█
621+
└
622+
",
623+
"",
624+
"",
625+
"",
626+
"◇ foo
627+
│ xy",
628+
"
629+
",
630+
"[?25h",
631+
]
632+
`;

packages/prompts/src/index.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,59 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
281281
expect(value).toBe('xy');
282282
expect(output.buffer).toMatchSnapshot();
283283
});
284+
285+
test('defaultValue sets the value but does not render', async () => {
286+
const result = prompts.text({
287+
message: 'foo',
288+
defaultValue: 'bar',
289+
input,
290+
output,
291+
});
292+
293+
input.emit('keypress', '', { name: 'return' });
294+
295+
const value = await result;
296+
297+
expect(value).toBe('bar');
298+
expect(output.buffer).toMatchSnapshot();
299+
});
300+
301+
test('validation errors render and clear', async () => {
302+
const result = prompts.text({
303+
message: 'foo',
304+
validate: (val) => (val !== 'xy' ? 'should be xy' : undefined),
305+
input,
306+
output,
307+
});
308+
309+
input.emit('keypress', 'x', { name: 'x' });
310+
input.emit('keypress', '', { name: 'return' });
311+
input.emit('keypress', 'y', { name: 'y' });
312+
input.emit('keypress', '', { name: 'return' });
313+
314+
const value = await result;
315+
316+
expect(value).toBe('xy');
317+
expect(output.buffer).toMatchSnapshot();
318+
});
319+
320+
test('validation errors render and clear (using Error)', async () => {
321+
const result = prompts.text({
322+
message: 'foo',
323+
validate: (val) => (val !== 'xy' ? new Error('should be xy') : undefined),
324+
input,
325+
output,
326+
});
327+
328+
input.emit('keypress', 'x', { name: 'x' });
329+
input.emit('keypress', '', { name: 'return' });
330+
input.emit('keypress', 'y', { name: 'y' });
331+
input.emit('keypress', '', { name: 'return' });
332+
333+
const value = await result;
334+
335+
expect(value).toBe('xy');
336+
expect(output.buffer).toMatchSnapshot();
337+
});
284338
});
285339
});

0 commit comments

Comments
 (0)