-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
162 lines (135 loc) · 3.91 KB
/
test.js
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
import process from 'node:process';
import {PassThrough} from 'node:stream';
import getStream from 'get-stream';
import test from 'ava';
import stripAnsi from 'strip-ansi';
import yoctocolors from 'yoctocolors';
import yoctoSpinner from './index.js';
delete process.env.CI;
const getPassThroughStream = () => {
const stream = new PassThrough();
stream.clearLine = () => {};
stream.cursorTo = () => {};
stream.moveCursor = () => {};
return stream;
};
const runSpinner = async (function_, options = {}, testOptions = {}) => {
const stream = testOptions.stream ?? getPassThroughStream();
const output = getStream(stream);
const spinner = yoctoSpinner({
stream,
text: testOptions.text ?? 'foo',
spinner: {
frames: ['-'],
interval: 10_000,
},
...options,
});
spinner.start();
function_(spinner);
stream.end();
return stripAnsi(await output);
};
test('start and stop spinner', async t => {
const output = await runSpinner(spinner => spinner.stop());
t.is(output, '- foo\n');
});
test('spinner.success()', async t => {
const output = await runSpinner(spinner => spinner.success());
t.regex(output, /✔ foo\n$/);
});
test('spinner.error()', async t => {
const output = await runSpinner(spinner => spinner.error());
t.regex(output, /✖️ foo\n$/);
});
test('spinner.warning()', async t => {
const output = await runSpinner(spinner => spinner.warning());
t.regex(output, /⚠ foo\n$/);
});
test('spinner.info()', async t => {
const output = await runSpinner(spinner => spinner.info());
t.regex(output, /ℹ foo\n$/);
});
test('spinner changes text', async t => {
const output = await runSpinner(spinner => {
spinner.text = 'bar';
spinner.stop();
});
t.is(output, '- foo\n- bar\n');
});
test('spinner stops with final text', async t => {
const output = await runSpinner(spinner => spinner.stop('final'));
t.regex(output, /final\n$/);
});
test('spinner with non-TTY stream', t => {
const stream = getPassThroughStream();
stream.isTTY = false;
const spinner = yoctoSpinner({stream, text: 'foo'});
spinner.start();
spinner.stop('final');
t.pass();
});
test('spinner starts with custom text', async t => {
const output = await runSpinner(spinner => spinner.stop(), {text: 'custom'});
t.is(output, '- custom\n');
});
test('spinner starts and changes text multiple times', async t => {
const output = await runSpinner(spinner => {
spinner.text = 'bar';
spinner.text = 'baz';
spinner.stop();
});
t.is(output, '- foo\n- bar\n- baz\n');
});
test('spinner handles multiple start/stop cycles', async t => {
const output = await runSpinner(spinner => {
spinner.stop();
spinner.start('bar');
spinner.stop();
spinner.start('baz');
spinner.stop();
});
t.is(output, '- foo\n- bar\n- baz\n');
});
test('spinner stops with success symbol and final text', async t => {
const output = await runSpinner(spinner => spinner.success('done'));
t.regex(output, /✔ done\n$/);
});
test('spinner stops with error symbol and final text', async t => {
const output = await runSpinner(spinner => spinner.error('failed'));
t.regex(output, /✖️ failed\n$/);
});
test('spinner accounts for ANSI escape codes when computing line breaks', async t => {
const scenarios = [
// 1 symbol + 1 space + 78 chars = 80 chars, max for one line
{
textLength: 78,
clearLineCount: 1,
},
// 1 symbol + 1 space + 79 chars = 81 chars, split on two lines
{
textLength: 79,
clearLineCount: 2,
},
];
for (const scenario of scenarios) {
let clearLineCount = 0;
const stream = new PassThrough();
stream.clearLine = () => {
clearLineCount += 1;
};
stream.cursorTo = () => {};
stream.moveCursor = () => {};
stream.isTTY = true;
let text = '';
for (let i = 0; i < scenario.textLength; i++) {
text += yoctocolors.blue('a');
}
// eslint-disable-next-line no-await-in-loop
await runSpinner(spinner => spinner.stop(), {}, {
stream,
text,
});
t.is(clearLineCount, scenario.clearLineCount);
}
});