Skip to content

Commit 7a6b817

Browse files
authored
test: add tests for labels/hints in select (#273)
1 parent e6fc1b6 commit 7a6b817

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed

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

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,31 @@ exports[`prompts (isCI = false) > multiselect > can cancel 1`] = `
169169
]
170170
`;
171171

172+
exports[`prompts (isCI = false) > multiselect > can render option hints 1`] = `
173+
[
174+
"[?25l",
175+
"│
176+
◆ foo
177+
│ ◻ opt0 (Hint 0)
178+
│ ◻ opt1
179+
└
180+
",
181+
"",
182+
"",
183+
"",
184+
"│ ◼ opt0 (Hint 0)",
185+
"",
186+
"",
187+
"",
188+
"",
189+
"◇ foo
190+
│ opt0",
191+
"
192+
",
193+
"[?25h",
194+
]
195+
`;
196+
172197
exports[`prompts (isCI = false) > multiselect > can set cursorAt to preselect an option 1`] = `
173198
[
174199
"[?25l",
@@ -194,6 +219,31 @@ exports[`prompts (isCI = false) > multiselect > can set cursorAt to preselect an
194219
]
195220
`;
196221

222+
exports[`prompts (isCI = false) > multiselect > can set custom labels 1`] = `
223+
[
224+
"[?25l",
225+
"│
226+
◆ foo
227+
│ ◻ Option 0
228+
│ ◻ Option 1
229+
└
230+
",
231+
"",
232+
"",
233+
"",
234+
"│ ◼ Option 0",
235+
"",
236+
"",
237+
"",
238+
"",
239+
"◇ foo
240+
│ Option 0",
241+
"
242+
",
243+
"[?25h",
244+
]
245+
`;
246+
197247
exports[`prompts (isCI = false) > multiselect > can set initial values 1`] = `
198248
[
199249
"[?25l",
@@ -349,6 +399,46 @@ exports[`prompts (isCI = false) > multiselect > renders message 1`] = `
349399
]
350400
`;
351401

402+
exports[`prompts (isCI = false) > multiselect > renders multiple cancelled values 1`] = `
403+
[
404+
"[?25l",
405+
"│
406+
◆ foo
407+
│ ◻ opt0
408+
│ ◻ opt1
409+
│ ◻ opt2
410+
└
411+
",
412+
"",
413+
"",
414+
"",
415+
"│ ◼ opt0",
416+
"",
417+
"",
418+
"",
419+
"",
420+
"│ ◼ opt0
421+
│ ◻ opt1
422+
│ ◻ opt2
423+
└
424+
",
425+
"",
426+
"",
427+
"",
428+
"│ ◼ opt1",
429+
"",
430+
"",
431+
"",
432+
"",
433+
"■ foo
434+
│ opt0, opt1
435+
│",
436+
"
437+
",
438+
"[?25h",
439+
]
440+
`;
441+
352442
exports[`prompts (isCI = false) > multiselect > renders multiple selected options 1`] = `
353443
[
354444
"[?25l",
@@ -1244,6 +1334,31 @@ exports[`prompts (isCI = true) > multiselect > can cancel 1`] = `
12441334
]
12451335
`;
12461336

1337+
exports[`prompts (isCI = true) > multiselect > can render option hints 1`] = `
1338+
[
1339+
"[?25l",
1340+
"│
1341+
◆ foo
1342+
│ ◻ opt0 (Hint 0)
1343+
│ ◻ opt1
1344+
└
1345+
",
1346+
"",
1347+
"",
1348+
"",
1349+
"│ ◼ opt0 (Hint 0)",
1350+
"",
1351+
"",
1352+
"",
1353+
"",
1354+
"◇ foo
1355+
│ opt0",
1356+
"
1357+
",
1358+
"[?25h",
1359+
]
1360+
`;
1361+
12471362
exports[`prompts (isCI = true) > multiselect > can set cursorAt to preselect an option 1`] = `
12481363
[
12491364
"[?25l",
@@ -1269,6 +1384,31 @@ exports[`prompts (isCI = true) > multiselect > can set cursorAt to preselect an
12691384
]
12701385
`;
12711386

1387+
exports[`prompts (isCI = true) > multiselect > can set custom labels 1`] = `
1388+
[
1389+
"[?25l",
1390+
"│
1391+
◆ foo
1392+
│ ◻ Option 0
1393+
│ ◻ Option 1
1394+
└
1395+
",
1396+
"",
1397+
"",
1398+
"",
1399+
"│ ◼ Option 0",
1400+
"",
1401+
"",
1402+
"",
1403+
"",
1404+
"◇ foo
1405+
│ Option 0",
1406+
"
1407+
",
1408+
"[?25h",
1409+
]
1410+
`;
1411+
12721412
exports[`prompts (isCI = true) > multiselect > can set initial values 1`] = `
12731413
[
12741414
"[?25l",
@@ -1424,6 +1564,46 @@ exports[`prompts (isCI = true) > multiselect > renders message 1`] = `
14241564
]
14251565
`;
14261566

1567+
exports[`prompts (isCI = true) > multiselect > renders multiple cancelled values 1`] = `
1568+
[
1569+
"[?25l",
1570+
"│
1571+
◆ foo
1572+
│ ◻ opt0
1573+
│ ◻ opt1
1574+
│ ◻ opt2
1575+
└
1576+
",
1577+
"",
1578+
"",
1579+
"",
1580+
"│ ◼ opt0",
1581+
"",
1582+
"",
1583+
"",
1584+
"",
1585+
"│ ◼ opt0
1586+
│ ◻ opt1
1587+
│ ◻ opt2
1588+
└
1589+
",
1590+
"",
1591+
"",
1592+
"",
1593+
"│ ◼ opt1",
1594+
"",
1595+
"",
1596+
"",
1597+
"",
1598+
"■ foo
1599+
│ opt0, opt1
1600+
│",
1601+
"
1602+
",
1603+
"[?25h",
1604+
]
1605+
`;
1606+
14271607
exports[`prompts (isCI = true) > multiselect > renders multiple selected options 1`] = `
14281608
[
14291609
"[?25l",

packages/prompts/src/index.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,5 +749,68 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
749749
expect(value).toEqual(['opt0']);
750750
expect(output.buffer).toMatchSnapshot();
751751
});
752+
753+
test('can set custom labels', async () => {
754+
const result = prompts.multiselect({
755+
message: 'foo',
756+
options: [
757+
{ value: 'opt0', label: 'Option 0' },
758+
{ value: 'opt1', label: 'Option 1' },
759+
],
760+
input,
761+
output,
762+
});
763+
764+
input.emit('keypress', '', { name: 'space' });
765+
input.emit('keypress', '', { name: 'return' });
766+
767+
const value = await result;
768+
769+
expect(value).toEqual(['opt0']);
770+
expect(output.buffer).toMatchSnapshot();
771+
});
772+
773+
test('can render option hints', async () => {
774+
const result = prompts.multiselect({
775+
message: 'foo',
776+
options: [
777+
{ value: 'opt0', hint: 'Hint 0' },
778+
{ value: 'opt1', hint: 'Hint 1' },
779+
],
780+
input,
781+
output,
782+
});
783+
784+
input.emit('keypress', '', { name: 'space' });
785+
input.emit('keypress', '', { name: 'return' });
786+
787+
const value = await result;
788+
789+
expect(value).toEqual(['opt0']);
790+
expect(output.buffer).toMatchSnapshot();
791+
});
792+
793+
test('renders multiple cancelled values', async () => {
794+
const result = prompts.multiselect({
795+
message: 'foo',
796+
options: [
797+
{ value: 'opt0' },
798+
{ value: 'opt1' },
799+
{ value: 'opt2' }
800+
],
801+
input,
802+
output,
803+
});
804+
805+
input.emit('keypress', '', { name: 'space' });
806+
input.emit('keypress', '', { name: 'down' });
807+
input.emit('keypress', '', { name: 'space' });
808+
input.emit('keypress', '', { name: 'escape' });
809+
810+
const value = await result;
811+
812+
expect(prompts.isCancel(value)).toBe(true);
813+
expect(output.buffer).toMatchSnapshot();
814+
});
752815
});
753816
});

0 commit comments

Comments
 (0)