diff --git a/packages/prompts/src/__snapshots__/index.test.ts.snap b/packages/prompts/src/__snapshots__/index.test.ts.snap index 21a236f8..52b0f26a 100644 --- a/packages/prompts/src/__snapshots__/index.test.ts.snap +++ b/packages/prompts/src/__snapshots__/index.test.ts.snap @@ -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", @@ -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", @@ -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", @@ -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", +] +`; diff --git a/packages/prompts/src/index.test.ts b/packages/prompts/src/index.test.ts index b2fbb5ba..e2ae6ecb 100644 --- a/packages/prompts/src/index.test.ts +++ b/packages/prompts/src/index.test.ts @@ -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(); + }); }); });