Skip to content

Commit 070c251

Browse files
committed
docs: add how to test
1 parent 897a89a commit 070c251

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',
@@ -187,3 +189,52 @@ log.message('Hello, World', { symbol: color.cyan('~') });
187189
```
188190

189191
[clack-log-prompts](https://github.com/natemoo-re/clack/blob/main/.github/assets/clack-logs.png)
192+
193+
## How to Test
194+
195+
```js
196+
import { mockPrompt } from '@clack/core';
197+
import * as p from '@clack/prompts';
198+
import { setTimeout as sleep } from 'node:timers/promises';
199+
200+
const cli = async () => {
201+
return p.group({
202+
name: () => p.text({ message: 'What is your name?' }),
203+
age: () => p.text({ message: 'What is your age?' }),
204+
color: ({ results }) =>
205+
p.multiselect({
206+
message: `What is your favorite color ${results.name}?`,
207+
options: [
208+
{ value: 'red', label: 'Red' },
209+
{ value: 'green', label: 'Green' },
210+
{ value: 'blue', label: 'Blue' },
211+
],
212+
}),
213+
});
214+
};
215+
216+
describe('cli', () => {
217+
const prompt = mockPrompt();
218+
219+
afterEach(() => {
220+
prompt.close();
221+
});
222+
223+
it('should simulate user interaction', async () => {
224+
// Dot not use `await` or `.then()` before `prompt.submit()` or `prompt.cancel()`
225+
const promise = cli();
226+
227+
prompt.submit('John');
228+
await sleep(100);
229+
prompt.submit('22');
230+
await sleep(100);
231+
prompt.submit(['green']);
232+
233+
await expect(promise).resolves.toStrictEqual({
234+
name: 'John',
235+
age: '22',
236+
color: ['green'],
237+
});
238+
});
239+
});
240+
```

0 commit comments

Comments
 (0)