Skip to content

Commit

Permalink
Add tests for the input prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Apr 23, 2023
1 parent 392cb6f commit 972d180
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
[*test.mts, *.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion packages/input/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const answer = await input({ message: 'Enter your name' });
| ----------- | ----------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| message | `string` | yes | The question to ask |
| default | `string` | no | Default value if no answer is provided (clear it by pressing backspace) |
| transformer | `(string, { isFinal: boolean }) => string` | no | Transform/Format the raw value entered by the user. Once the prompt is completed, `isFinal` will be `true`. This function is purely visual; to modify the answer, use the `filter` option. |
| transformer | `(string, { isFinal: boolean }) => string` | no | Transform/Format the raw value entered by the user. Once the prompt is completed, `isFinal` will be `true`. This function is purely visual, modify the answer in your code if needed. |
| validate | `string => boolean \| string \| Promise<string \| boolean>` | no | On submit, validate the filtered answered content. When returning a string, it'll be used as the error message displayed to the user. Note: returning a rejected promise, we'll assume a code error happened and crash. |

# License
Expand Down
4 changes: 4 additions & 0 deletions packages/input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"@inquirer/type": "^0.1.0",
"chalk": "^5.2.0"
},
"devDependencies": {
"@inquirer/testing": "^0.1.0",
"@jest/globals": "^29.5.0"
},
"scripts": {
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
"clean": "rm -rf dist",
Expand Down
136 changes: 136 additions & 0 deletions packages/input/test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { render } from '@inquirer/testing';
import input from './src/index.mjs';

describe('input prompt', () => {
it('handle simple use case', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'What is your name',
});

expect(getScreen()).toMatchInlineSnapshot(`"? What is your name"`);

events.type('J');
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name J"`);

events.type('ohn');
events.keypress('enter');

await expect(answer).resolves.toEqual('John');
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name John"`);
});

it('handle transformer', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'What is your name',
transformer: (value, { isFinal }) => (isFinal ? 'Transformed' : `t+${value}`),
});

expect(getScreen()).toMatchInlineSnapshot(`"? What is your name t+"`);

events.type('John');
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name t+John"`);

events.keypress('enter');
await expect(answer).resolves.toEqual('John');
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name Transformed"`);
});

it('handle synchronous validation', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'Answer 2 ===',
validate: (answer) => answer === '2',
});

expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 ==="`);

events.type('1');
expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 === 1"`);

events.keypress('enter');
await Promise.resolve();
expect(getScreen()).toMatchInlineSnapshot(`
"? Answer 2 ===
> You must provide a valid value"
`);

events.type('2');
expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 === 2"`);

events.keypress('enter');
await expect(answer).resolves.toEqual('2');
});

it('handle asynchronous validation', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'Answer 2 ===',
validate(answer) {
return new Promise((resolve) => {
if (answer === '2') {
resolve(true);
} else {
resolve('Answer must be 2');
}
});
},
});

expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 ==="`);

events.type('1');
expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 === 1"`);

events.keypress('enter');
await Promise.resolve();
expect(getScreen()).toMatchInlineSnapshot(`
"? Answer 2 ===
> Answer must be 2"
`);

events.type('2');
expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 === 2"`);

events.keypress('enter');
await expect(answer).resolves.toEqual('2');
});

it('handle default option', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'What is your name',
default: 'Mike',
});

expect(getScreen()).toMatchInlineSnapshot(`"? What is your name (Mike)"`);

events.keypress('enter');
await expect(answer).resolves.toEqual('Mike');
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name Mike"`);
});

it('handle overwriting the default option', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'What is your name',
default: 'Mike',
});

expect(getScreen()).toMatchInlineSnapshot(`"? What is your name (Mike)"`);

events.type('John');
events.keypress('enter');
await expect(answer).resolves.toEqual('John');
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name John"`);
});

it('handle removing the default option', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'What is your name',
default: 'Mike',
});

expect(getScreen()).toMatchInlineSnapshot(`"? What is your name (Mike)"`);

events.keypress('backspace');
events.keypress('enter');
await expect(answer).resolves.toEqual('');
expect(getScreen()).toMatchInlineSnapshot(`"? What is your name"`);
});
});

0 comments on commit 972d180

Please sign in to comment.