-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
test.js
58 lines (46 loc) · 1.45 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
import test from 'ava';
import stringLength from 'string-length';
import cliSpinners, {randomSpinner} from './index.js';
function mockMathRandom(fixedResult) {
unMockMathRandom();
const originalImplementation = Math.random;
Math.random = () => fixedResult;
Math.random.originalImplementation = originalImplementation;
}
function unMockMathRandom() {
if (Math.random.originalImplementation) {
Math.random = Math.random.originalImplementation;
}
}
console.log('Spinner count:', Object.keys(cliSpinners).length);
test('main', t => {
t.is(typeof cliSpinners, 'object');
t.is(cliSpinners.dots.interval, 80);
t.true(Array.isArray(cliSpinners.dots.frames));
});
test('constant width', t => {
for (const key of Object.keys(cliSpinners)) {
const {
[key]: {
frames,
frames: [
firstFrame,
],
},
} = cliSpinners;
const firstFrameLength = stringLength(firstFrame);
t.true(frames.every(frame => stringLength(frame) === firstFrameLength));
}
});
test('randomSpinner()', t => {
const spinnersList = Object.values(cliSpinners);
// Should always return an item from the spinners list.
t.true(spinnersList.includes(randomSpinner()));
// Should return the first spinner when `Math.random()` is the minimum value.
mockMathRandom(0);
t.is(randomSpinner(), spinnersList[0]);
mockMathRandom(0.99);
// Should return the last spinner when `Math.random()` is the maximum value.
t.is(randomSpinner(), spinnersList.at(-1));
unMockMathRandom();
});