-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
76 lines (58 loc) · 1.84 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
/**
* The idea is to use a dummy writable stream, save whatever our program outputs
* to `process.stdout` and then finally compare if the results are different
* from each other, which means the loading animation is working properly.
**/
import Stream from 'stream';
import logUpdate from 'log-update';
import test from 'ava';
import loading from './';
/**
* Note that we share the same stream instance, that's why tests must run
* serially, otherwise output data will get messed up.
**/
let output = [];
const stream = new Stream.Duplex({
write(chunk, encoding, next) {
output.push(chunk);
next();
}
});
/**
* Little helper to hang the test for a given period of time through Promises.
**/
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const createLoadingIndicator = stream => {
return loading.start('loading', {
render: logUpdate.create(stream)
});
};
/**
* Reset dummy stream output data at each run.
**/
test.beforeEach(() => output.length = 0);
test('test for valid output', async t => {
/**
* Creates a new loading indicator passing a custom render fn.
* In this case, the function simply returns a new `logUpdate()` that writes
* to our dummy stream.
**/
const timer = createLoadingIndicator(stream);
await delay(1000);
loading.stop(timer);
t.not(output[0].toString(), output[1].toString());
t.not(output[1].toString(), output[2].toString());
t.not(output[2].toString(), output[3].toString());
});
test('test if timeout is properly disposed', t => {
const timer = createLoadingIndicator(stream);
loading.stop(timer);
t.not(timer['0']);
});
test('test if custom text is properly added', async t => {
const timer = createLoadingIndicator(stream);
await delay(1000);
loading.stop(timer);
const text = output[0].toString().slice(2).trim();
t.is(text, 'loading');
});