Skip to content

Commit c40a558

Browse files
committed
docs: add how to test
1 parent ffafc75 commit c40a558

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

packages/prompts/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ console.log(group.name, group.age, group.color);
162162
Execute multiple tasks in spinners.
163163

164164
```js
165+
import * as p from '@clack/prompts';
166+
165167
await p.tasks([
166168
{
167169
title: 'Installing via npm',
@@ -172,3 +174,52 @@ await p.tasks([
172174
},
173175
]);
174176
```
177+
178+
## How to Test
179+
180+
```js
181+
import { mockPrompt } from '@clack/core';
182+
import * as p from '@clack/prompts';
183+
import { setTimeout as sleep } from 'node:timers/promises';
184+
185+
const cli = async () => {
186+
return p.group({
187+
name: () => p.text({ message: 'What is your name?' }),
188+
age: () => p.text({ message: 'What is your age?' }),
189+
color: ({ results }) =>
190+
p.multiselect({
191+
message: `What is your favorite color ${results.name}?`,
192+
options: [
193+
{ value: 'red', label: 'Red' },
194+
{ value: 'green', label: 'Green' },
195+
{ value: 'blue', label: 'Blue' },
196+
],
197+
}),
198+
});
199+
};
200+
201+
describe('cli', () => {
202+
const prompt = mockPrompt();
203+
204+
afterEach(() => {
205+
prompt.close();
206+
});
207+
208+
it('should simulate user interaction', async () => {
209+
// Dot not use `await` or `.then()` before `prompt.submit()` or `prompt.cancel()`
210+
const promise = cli();
211+
212+
prompt.submit('John');
213+
await sleep(100);
214+
prompt.submit('22');
215+
await sleep(100);
216+
prompt.submit(['green']);
217+
218+
await expect(promise).resolves.toStrictEqual({
219+
name: 'John',
220+
age: '22',
221+
color: ['green'],
222+
});
223+
});
224+
});
225+
```

0 commit comments

Comments
 (0)