Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions packages/prompts/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ exports[`prompts (isCI = false) > text > can cancel 1`] = `
]
`;

exports[`prompts (isCI = false) > text > defaultValue sets the value but does not render 1`] = `
[
"[?25l",
"│
◆ foo
│ _
└
",
"",
"",
"",
"◇ foo
│ bar",
"
",
"[?25h",
]
`;

exports[`prompts (isCI = false) > text > renders cancelled value if one set 1`] = `
[
"[?25l",
Expand Down Expand Up @@ -220,6 +239,82 @@ exports[`prompts (isCI = false) > text > renders submitted value 1`] = `
]
`;

exports[`prompts (isCI = false) > text > validation errors render and clear (using Error) 1`] = `
[
"[?25l",
"│
◆ foo
│ _
└
",
"",
"",
"",
"│ x█",
"",
"",
"",
"",
"▲ foo
│ x█
└ should be xy
",
"",
"",
"",
"◆ foo
│ xy█
└
",
"",
"",
"",
"◇ foo
│ xy",
"
",
"[?25h",
]
`;

exports[`prompts (isCI = false) > text > validation errors render and clear 1`] = `
[
"[?25l",
"│
◆ foo
│ _
└
",
"",
"",
"",
"│ x█",
"",
"",
"",
"",
"▲ foo
│ x█
└ should be xy
",
"",
"",
"",
"◆ foo
│ xy█
└
",
"",
"",
"",
"◇ foo
│ xy",
"
",
"[?25h",
]
`;

exports[`prompts (isCI = true) > spinner > message > sets message for next frame 1`] = `
[
"[?25l",
Expand Down Expand Up @@ -344,6 +439,25 @@ exports[`prompts (isCI = true) > text > can cancel 1`] = `
]
`;

exports[`prompts (isCI = true) > text > defaultValue sets the value but does not render 1`] = `
[
"[?25l",
"│
◆ foo
│ _
└
",
"",
"",
"",
"◇ foo
│ bar",
"
",
"[?25h",
]
`;

exports[`prompts (isCI = true) > text > renders cancelled value if one set 1`] = `
[
"[?25l",
Expand Down Expand Up @@ -440,3 +554,79 @@ exports[`prompts (isCI = true) > text > renders submitted value 1`] = `
"[?25h",
]
`;

exports[`prompts (isCI = true) > text > validation errors render and clear (using Error) 1`] = `
[
"[?25l",
"│
◆ foo
│ _
└
",
"",
"",
"",
"│ x█",
"",
"",
"",
"",
"▲ foo
│ x█
└ should be xy
",
"",
"",
"",
"◆ foo
│ xy█
└
",
"",
"",
"",
"◇ foo
│ xy",
"
",
"[?25h",
]
`;

exports[`prompts (isCI = true) > text > validation errors render and clear 1`] = `
[
"[?25l",
"│
◆ foo
│ _
└
",
"",
"",
"",
"│ x█",
"",
"",
"",
"",
"▲ foo
│ x█
└ should be xy
",
"",
"",
"",
"◆ foo
│ xy█
└
",
"",
"",
"",
"◇ foo
│ xy",
"
",
"[?25h",
]
`;
54 changes: 54 additions & 0 deletions packages/prompts/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,59 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
expect(value).toBe('xy');
expect(output.buffer).toMatchSnapshot();
});

test('defaultValue sets the value but does not render', async () => {
const result = prompts.text({
message: 'foo',
defaultValue: 'bar',
input,
output,
});

input.emit('keypress', '', { name: 'return' });

const value = await result;

expect(value).toBe('bar');
expect(output.buffer).toMatchSnapshot();
});

test('validation errors render and clear', async () => {
const result = prompts.text({
message: 'foo',
validate: (val) => (val !== 'xy' ? 'should be xy' : undefined),
input,
output,
});

input.emit('keypress', 'x', { name: 'x' });
input.emit('keypress', '', { name: 'return' });
input.emit('keypress', 'y', { name: 'y' });
input.emit('keypress', '', { name: 'return' });

const value = await result;

expect(value).toBe('xy');
expect(output.buffer).toMatchSnapshot();
});

test('validation errors render and clear (using Error)', async () => {
const result = prompts.text({
message: 'foo',
validate: (val) => (val !== 'xy' ? new Error('should be xy') : undefined),
input,
output,
});

input.emit('keypress', 'x', { name: 'x' });
input.emit('keypress', '', { name: 'return' });
input.emit('keypress', 'y', { name: 'y' });
input.emit('keypress', '', { name: 'return' });

const value = await result;

expect(value).toBe('xy');
expect(output.buffer).toMatchSnapshot();
});
});
});