forked from vadimdemedes/ink-text-input
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
243 lines (191 loc) · 5.46 KB
/
test.js
File metadata and controls
243 lines (191 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import React, {useState} from 'react';
import test from 'ava';
import chalk from 'chalk';
import {render} from 'ink-testing-library';
import sinon from 'sinon';
import delay from 'delay';
import TextInput, {UncontrolledTextInput} from '.';
const noop = () => {};
const CURSOR = chalk.inverse(' ');
const ENTER = '\r';
const ARROW_LEFT = '\u001B[D';
const ARROW_RIGHT = '\u001B[C';
const DELETE = '\u007F';
test('default state', t => {
const {lastFrame} = render(<TextInput value="" onChange={noop} />);
t.is(lastFrame(), CURSOR);
});
test('display value', t => {
const {lastFrame} = render(
<TextInput value="Hello" showCursor={false} onChange={noop} />
);
t.is(lastFrame(), 'Hello');
});
test('display value with cursor', t => {
const {lastFrame} = render(<TextInput value="Hello" onChange={noop} />);
t.is(lastFrame(), `Hello${CURSOR}`);
});
test('display placeholder', t => {
const {lastFrame} = render(
<TextInput value="" placeholder="Placeholder" onChange={noop} />
);
t.is(lastFrame(), chalk.inverse('P') + chalk.grey('laceholder'));
});
test('display placeholder when cursor is hidden', t => {
const {lastFrame} = render(
<TextInput
value=""
placeholder="Placeholder"
showCursor={false}
onChange={noop}
/>
);
t.is(lastFrame(), chalk.grey('Placeholder'));
});
test('display value with mask', t => {
const {lastFrame} = render(
<TextInput value="Hello" mask="*" onChange={noop} />
);
t.is(lastFrame(), `*****${chalk.inverse(' ')}`);
});
test('accept input (controlled)', async t => {
const StatefulTextInput = () => {
const [value, setValue] = useState('');
return <TextInput value={value} onChange={setValue} />;
};
const {stdin, lastFrame} = render(<StatefulTextInput />);
t.is(lastFrame(), CURSOR);
await delay(100);
stdin.write('X');
await delay(100);
t.is(lastFrame(), `X${CURSOR}`);
});
test('accept input (uncontrolled)', async t => {
const {stdin, lastFrame} = render(<UncontrolledTextInput />);
t.is(lastFrame(), CURSOR);
await delay(100);
stdin.write('X');
await delay(100);
t.is(lastFrame(), `X${CURSOR}`);
});
test('ignore input when not in focus', async t => {
const StatefulTextInput = () => {
const [value, setValue] = useState('');
return <TextInput focus={false} value={value} onChange={setValue} />;
};
const {stdin, frames, lastFrame} = render(<StatefulTextInput />);
t.is(lastFrame(), '');
await delay(100);
stdin.write('X');
await delay(100);
t.is(frames.length, 1);
});
test('ignore input for Tab and Shift+Tab keys', async t => {
const Test = () => {
const [value, setValue] = useState('');
return <TextInput value={value} onChange={setValue} />;
};
const {stdin, lastFrame} = render(<Test />);
await delay(100);
stdin.write('\t');
await delay(100);
t.is(lastFrame(), CURSOR);
stdin.write('\u001B[Z');
await delay(100);
t.is(lastFrame(), CURSOR);
});
test('onSubmit', async t => {
const onSubmit = sinon.spy();
const StatefulTextInput = () => {
const [value, setValue] = useState('');
return <TextInput value={value} onChange={setValue} onSubmit={onSubmit} />;
};
const {stdin, lastFrame} = render(<StatefulTextInput />);
t.is(lastFrame(), CURSOR);
await delay(100);
stdin.write('X');
await delay(100);
stdin.write(ENTER);
await delay(100);
t.is(lastFrame(), `X${CURSOR}`);
t.true(onSubmit.calledWith('X'));
t.true(onSubmit.calledOnce);
});
test('paste and move cursor', async t => {
const StatefulTextInput = () => {
const [value, setValue] = useState('');
return <TextInput highlightPastedText value={value} onChange={setValue} />;
};
const {stdin, lastFrame} = render(<StatefulTextInput />);
// Need this to invert each char separately
const inverse = string => {
return string
.split('')
.map(c => chalk.inverse(c))
.join('');
};
await delay(100);
stdin.write('A');
await delay(100);
stdin.write('B');
await delay(100);
t.is(lastFrame(), `AB${CURSOR}`);
stdin.write(ARROW_LEFT);
await delay(100);
t.is(lastFrame(), `A${chalk.inverse('B')}`);
stdin.write('Hello World');
await delay(100);
t.is(lastFrame(), `A${inverse('Hello WorldB')}`);
stdin.write(ARROW_RIGHT);
await delay(100);
t.is(lastFrame(), `AHello WorldB${CURSOR}`);
});
test('delete at the beginning of text', async t => {
const Test = () => {
const [value, setValue] = useState('');
return <TextInput value={value} onChange={setValue} />;
};
const {stdin, lastFrame} = render(<Test />);
await delay(100);
stdin.write('T');
await delay(100);
stdin.write('e');
await delay(100);
stdin.write('s');
await delay(100);
stdin.write('t');
stdin.write(ARROW_LEFT);
await delay(100);
stdin.write(ARROW_LEFT);
await delay(100);
stdin.write(ARROW_LEFT);
await delay(100);
stdin.write(ARROW_LEFT);
await delay(100);
stdin.write(DELETE);
await delay(100);
t.is(lastFrame(), `${chalk.inverse('T')}est`);
});
test('adjust cursor when text is shorter than last value', async t => {
const Test = () => {
const [value, setValue] = useState('');
const submit = () => setValue('');
return <TextInput value={value} onChange={setValue} onSubmit={submit} />;
};
const {stdin, lastFrame} = render(<Test />);
await delay(100);
stdin.write('A');
await delay(100);
stdin.write('B');
await delay(100);
t.is(lastFrame(), `AB${chalk.inverse(' ')}`);
stdin.write('\r');
await delay(100);
t.is(lastFrame(), chalk.inverse(' '));
stdin.write('A');
await delay(100);
t.is(lastFrame(), `A${chalk.inverse(' ')}`);
stdin.write('B');
await delay(100);
t.is(lastFrame(), `AB${chalk.inverse(' ')}`);
});