/
hooks.tsx
163 lines (137 loc) Β· 4.06 KB
/
hooks.tsx
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
import {serial as test} from 'ava';
import {spawn} from 'node-pty';
import stripAnsi from 'strip-ansi';
const term = (fixture: string, args: string[] = []) => {
let resolve: (value?: any) => void;
let reject: (error?: Error) => void;
// eslint-disable-next-line promise/param-names
const exitPromise = new Promise((resolve2, reject2) => {
resolve = resolve2;
reject = reject2;
});
const env = {...process.env};
delete env.CI;
const ps = spawn('ts-node', [`./fixtures/${fixture}.tsx`, ...args], {
name: 'xterm-color',
cols: 100,
cwd: __dirname,
env
});
const result = {
write: (input: string) => {
// Give TS and Ink time to start up and render UI
setTimeout(() => {
ps.write(input);
}, 1000);
},
output: '',
waitForExit: () => exitPromise
};
ps.on('data', data => {
result.output += data;
});
ps.on('exit', code => {
if (code === 0) {
resolve();
return;
}
reject(new Error(`Process exited with non-zero exit code: ${code}`));
});
return result;
};
test('useInput - handle lowercase character', async t => {
const ps = term('use-input', ['lowercase']);
ps.write('q');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle uppercase character', async t => {
const ps = term('use-input', ['uppercase']);
ps.write('Q');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle escape', async t => {
const ps = term('use-input', ['escape']);
ps.write('\u001B');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle ctrl', async t => {
const ps = term('use-input', ['ctrl']);
ps.write('\u0006');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle meta', async t => {
const ps = term('use-input', ['meta']);
ps.write('\u001Bm');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle up arrow', async t => {
const ps = term('use-input', ['upArrow']);
ps.write('\u001B[A');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle down arrow', async t => {
const ps = term('use-input', ['downArrow']);
ps.write('\u001B[B');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle left arrow', async t => {
const ps = term('use-input', ['leftArrow']);
ps.write('\u001B[D');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle right arrow', async t => {
const ps = term('use-input', ['rightArrow']);
ps.write('\u001B[C');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle Tab', async t => {
const ps = term('use-input', ['tab']);
ps.write('\t');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle Shift+Tab', async t => {
const ps = term('use-input', ['shiftTab']);
ps.write('\u001B[Z');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle backspace', async t => {
const ps = term('use-input', ['backspace']);
ps.write('\u0008');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - handle delete', async t => {
const ps = term('use-input', ['delete']);
ps.write('\u007F');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});
test('useInput - ignore input if not active', async t => {
const ps = term('use-input-multiple');
ps.write('x');
await ps.waitForExit();
t.false(ps.output.includes('xx'));
t.true(ps.output.includes('x'));
t.true(ps.output.includes('exited'));
});
test('useStdout - write to stdout', async t => {
const ps = term('use-stdout');
await ps.waitForExit();
const lines = stripAnsi(ps.output).split('\r\n').slice(1, -1);
t.deepEqual(lines, ['Hello from Ink to stdout', 'Hello World', 'exited']);
});
// `node-pty` doesn't support streaming stderr output, so I need to figure out
// how to test useStderr() hook. child_process.spawn() can't be used, because
// Ink fails with "raw mode unsupported" error.
test.todo('useStderr - write to stderr');