Skip to content

Commit eed2780

Browse files
authored
test: add tests for ConfirmPrompt (#259)
1 parent a87628c commit eed2780

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import color from 'picocolors';
2+
import { cursor } from 'sisteransi';
3+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
4+
import { default as ConfirmPrompt } from '../../src/prompts/confirm.js';
5+
import { MockReadable } from '../mock-readable.js';
6+
import { MockWritable } from '../mock-writable.js';
7+
8+
describe('ConfirmPrompt', () => {
9+
let input: MockReadable;
10+
let output: MockWritable;
11+
12+
beforeEach(() => {
13+
input = new MockReadable();
14+
output = new MockWritable();
15+
});
16+
17+
afterEach(() => {
18+
vi.restoreAllMocks();
19+
});
20+
21+
test('renders render() result', () => {
22+
const instance = new ConfirmPrompt({
23+
input,
24+
output,
25+
render: () => 'foo',
26+
active: 'yes',
27+
inactive: 'no',
28+
});
29+
instance.prompt();
30+
expect(output.buffer).to.deep.equal([cursor.hide, 'foo']);
31+
});
32+
33+
test('sets value and submits on confirm (y)', () => {
34+
const instance = new ConfirmPrompt({
35+
input,
36+
output,
37+
render: () => 'foo',
38+
active: 'yes',
39+
inactive: 'no',
40+
initialValue: true,
41+
});
42+
43+
instance.prompt();
44+
input.emit('keypress', 'y', { name: 'y' });
45+
46+
expect(instance.value).to.equal(true);
47+
expect(instance.state).to.equal('submit');
48+
});
49+
50+
test('sets value and submits on confirm (n)', () => {
51+
const instance = new ConfirmPrompt({
52+
input,
53+
output,
54+
render: () => 'foo',
55+
active: 'yes',
56+
inactive: 'no',
57+
initialValue: true,
58+
});
59+
60+
instance.prompt();
61+
input.emit('keypress', 'n', { name: 'n' });
62+
63+
expect(instance.value).to.equal(false);
64+
expect(instance.state).to.equal('submit');
65+
});
66+
67+
describe('cursor', () => {
68+
test('cursor is 1 when inactive', () => {
69+
const instance = new ConfirmPrompt({
70+
input,
71+
output,
72+
render: () => 'foo',
73+
active: 'yes',
74+
inactive: 'no',
75+
initialValue: false,
76+
});
77+
78+
instance.prompt();
79+
input.emit('keypress', '', { name: 'return' });
80+
expect(instance.cursor).to.equal(1);
81+
});
82+
83+
test('cursor is 0 when active', () => {
84+
const instance = new ConfirmPrompt({
85+
input,
86+
output,
87+
render: () => 'foo',
88+
active: 'yes',
89+
inactive: 'no',
90+
initialValue: true,
91+
});
92+
93+
instance.prompt();
94+
input.emit('keypress', '', { name: 'return' });
95+
expect(instance.cursor).to.equal(0);
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)