forked from sindresorhus/p-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
166 lines (140 loc) · 3.2 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
163
164
165
166
import test from 'ava';
import delay from 'delay';
import timeSpan from 'time-span';
import inRange from 'in-range';
import PProgress from '.';
const fixture = Symbol('fixture');
test('new PProgress()', async t => {
t.plan(45);
const p = new PProgress(async (resolve, reject, progress) => {
progress(0.1);
await delay(50);
progress(0.3);
await delay(100);
progress(0.4);
await delay(20);
progress(0.9);
await delay(50);
progress(1);
progress(1);
progress(1);
resolve(fixture);
});
t.true(p instanceof Promise);
p.onProgress(progress => {
t.is(progress, p.progress);
t.true(progress >= 0 && progress <= 1);
});
p.onProgress(progress => {
t.is(progress, p.progress);
t.true(progress >= 0 && progress <= 1);
});
// eslint-disable-next-line promise/prefer-await-to-then
p.then(result => [result, result]).then(results => {
t.true(Array.isArray(results));
results.forEach(result => t.is(result, fixture));
})
.onProgress(progress => {
t.is(progress, p.progress);
t.true(progress >= 0 && progress <= 1);
});
p.catch(() => {}).onProgress(progress => {
t.is(progress, p.progress);
t.true(progress >= 0 && progress <= 1);
});
t.is(await p, fixture);
await delay(1);
});
test('PProgress.fn()', async t => {
t.plan(4);
const fn = PProgress.fn(async (input, progress) => {
progress(0.1);
await delay(50);
progress(0.6);
await delay(100);
progress(1);
return input;
});
const p = fn(fixture);
p.onProgress(progress => {
t.true(progress >= 0 && progress <= 1);
});
t.is(await p, fixture);
});
test('PProgress.all()', async t => {
const fixtureFn = PProgress.fn(async (input, progress) => {
progress(0.16);
await delay(50);
progress(0.55);
await delay(100);
return input;
});
const fixtureFn2 = PProgress.fn(async (input, progress) => {
progress(0.14);
await delay(52);
progress(0.37);
await delay(104);
progress(0.41);
await delay(26);
progress(0.93);
await delay(55);
return input;
});
const p = PProgress.all([
delay(103),
fixtureFn(fixture),
delay(55),
fixtureFn2(fixture),
delay(14),
delay(209)
]);
p.onProgress(progress => {
t.true(progress >= 0 && progress <= 1);
});
t.deepEqual(await p, [
undefined,
fixture,
undefined,
fixture,
undefined,
undefined
]);
});
test('PProgress.all() with concurrency = 1', async t => {
const fixtureFn = PProgress.fn(async (input, progress) => {
progress(0.16);
await delay(50);
progress(0.55);
await delay(50);
return input;
});
const fixtureFn2 = PProgress.fn(async (input, progress) => {
progress(0.41);
await delay(50);
progress(0.93);
await delay(50);
return input;
});
// Should throw when first argument is array of promises instead of promise-returning functions
t.throws(() => PProgress.all([fixtureFn(fixture), fixtureFn2(fixture)], {
concurrency: 1
}), TypeError);
const end = timeSpan();
const p = PProgress.all([
() => fixtureFn(fixture),
() => fixtureFn2(fixture)
], {
concurrency: 1
});
p.onProgress(progress => {
t.true(progress >= 0 && progress <= 1);
});
t.deepEqual(await p, [
fixture,
fixture
]);
t.true(inRange(end(), {
start: 200, // 4 delays of 50ms each
end: 250 // Reasonable padding
}));
});