diff --git a/packages/prompts/src/__snapshots__/index.test.ts.snap b/packages/prompts/src/__snapshots__/index.test.ts.snap index 52b0f26a..ba296e65 100644 --- a/packages/prompts/src/__snapshots__/index.test.ts.snap +++ b/packages/prompts/src/__snapshots__/index.test.ts.snap @@ -1,5 +1,154 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`prompts (isCI = false) > confirm > can cancel 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "■ foo +│ No +│", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = false) > confirm > can set initialValue 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ○ Yes / ● No +└ +", + "", + "", + "", + "◇ foo +│ No", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = false) > confirm > left arrow moves to previous choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "│ ○ Yes / ● No", + "", + "", + "", + "", + "│ ● Yes / ○ No", + "", + "", + "", + "", + "◇ foo +│ Yes", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = false) > confirm > renders custom active choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● bleep / ○ No +└ +", + "", + "", + "", + "◇ foo +│ bleep", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = false) > confirm > renders custom inactive choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ bleep +└ +", + "", + "", + "", + "◇ foo +│ Yes", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = false) > confirm > renders message with choices 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "◇ foo +│ Yes", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = false) > confirm > right arrow moves to next choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "│ ○ Yes / ● No", + "", + "", + "", + "", + "◇ foo +│ No", + " +", + "[?25h", +] +`; + exports[`prompts (isCI = false) > spinner > message > sets message for next frame 1`] = ` [ "[?25l", @@ -315,6 +464,155 @@ exports[`prompts (isCI = false) > text > validation errors render and clear 1`] ] `; +exports[`prompts (isCI = true) > confirm > can cancel 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "■ foo +│ No +│", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = true) > confirm > can set initialValue 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ○ Yes / ● No +└ +", + "", + "", + "", + "◇ foo +│ No", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = true) > confirm > left arrow moves to previous choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "│ ○ Yes / ● No", + "", + "", + "", + "", + "│ ● Yes / ○ No", + "", + "", + "", + "", + "◇ foo +│ Yes", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = true) > confirm > renders custom active choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● bleep / ○ No +└ +", + "", + "", + "", + "◇ foo +│ bleep", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = true) > confirm > renders custom inactive choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ bleep +└ +", + "", + "", + "", + "◇ foo +│ Yes", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = true) > confirm > renders message with choices 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "◇ foo +│ Yes", + " +", + "[?25h", +] +`; + +exports[`prompts (isCI = true) > confirm > right arrow moves to next choice 1`] = ` +[ + "[?25l", + "│ +◆ foo +│ ● Yes / ○ No +└ +", + "", + "", + "", + "│ ○ Yes / ● No", + "", + "", + "", + "", + "◇ foo +│ No", + " +", + "[?25h", +] +`; + exports[`prompts (isCI = true) > spinner > message > sets message for next frame 1`] = ` [ "[?25l", diff --git a/packages/prompts/src/index.test.ts b/packages/prompts/src/index.test.ts index e2ae6ecb..cee860b4 100644 --- a/packages/prompts/src/index.test.ts +++ b/packages/prompts/src/index.test.ts @@ -336,4 +336,117 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => { expect(output.buffer).toMatchSnapshot(); }); }); + + describe('confirm', () => { + test('renders message with choices', async () => { + const result = prompts.confirm({ + message: 'foo', + input, + output, + }); + + input.emit('keypress', '', { name: 'return' }); + + const value = await result; + + expect(value).toBe(true); + expect(output.buffer).toMatchSnapshot(); + }); + + test('renders custom active choice', async () => { + const result = prompts.confirm({ + message: 'foo', + active: 'bleep', + input, + output, + }); + + input.emit('keypress', '', { name: 'return' }); + + const value = await result; + + expect(value).toBe(true); + expect(output.buffer).toMatchSnapshot(); + }); + + test('renders custom inactive choice', async () => { + const result = prompts.confirm({ + message: 'foo', + inactive: 'bleep', + input, + output, + }); + + input.emit('keypress', '', { name: 'return' }); + + const value = await result; + + expect(value).toBe(true); + expect(output.buffer).toMatchSnapshot(); + }); + + test('right arrow moves to next choice', async () => { + const result = prompts.confirm({ + message: 'foo', + input, + output, + }); + + input.emit('keypress', 'right', { name: 'right' }); + input.emit('keypress', '', { name: 'return' }); + + const value = await result; + + expect(value).toBe(false); + expect(output.buffer).toMatchSnapshot(); + }); + + test('left arrow moves to previous choice', async () => { + const result = prompts.confirm({ + message: 'foo', + input, + output, + }); + + input.emit('keypress', 'right', { name: 'right' }); + input.emit('keypress', 'left', { name: 'left' }); + input.emit('keypress', '', { name: 'return' }); + + const value = await result; + + expect(value).toBe(true); + expect(output.buffer).toMatchSnapshot(); + }); + + test('can cancel', async () => { + const result = prompts.confirm({ + message: 'foo', + input, + output, + }); + + input.emit('keypress', 'escape', { name: 'escape' }); + + const value = await result; + + expect(prompts.isCancel(value)).toBe(true); + expect(output.buffer).toMatchSnapshot(); + }); + + test('can set initialValue', async () => { + const result = prompts.confirm({ + message: 'foo', + initialValue: false, + input, + output, + }); + + input.emit('keypress', '', { name: 'return' }); + + const value = await result; + + expect(value).toBe(false); + expect(output.buffer).toMatchSnapshot(); + }); + }); });