-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
107 lines (99 loc) · 2.02 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
import test from 'ava';
import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import pSettle from './index.js';
test('main', async t => {
t.deepEqual(
// eslint-disable-next-line prefer-promise-reject-errors
await pSettle([delay(100, {value: 1}), 2, Promise.reject(3)]),
[
{
status: 'fulfilled',
value: 1,
isFulfilled: true,
isRejected: false,
},
{
status: 'fulfilled',
value: 2,
isFulfilled: true,
isRejected: false,
},
{
status: 'rejected',
reason: 3,
isFulfilled: false,
isRejected: true,
},
],
);
});
test('concurrency option works', async t => {
const fixture = [
async () => {
await delay(300);
return 10;
},
async () => {
await delay(200);
return 20;
},
async () => {
await delay(100);
return 30;
},
];
const end = timeSpan();
t.deepEqual(
await pSettle(fixture, {concurrency: 1}),
[
{
status: 'fulfilled',
value: 10,
isFulfilled: true,
isRejected: false,
},
{
status: 'fulfilled',
value: 20,
isFulfilled: true,
isRejected: false,
},
{
status: 'fulfilled',
value: 30,
isFulfilled: true,
isRejected: false,
},
],
);
t.true(inRange(end(), {start: 590, end: 760}));
});
test('handles empty iterable', async t => {
t.deepEqual(await pSettle([]), []);
});
test('handles null and undefined', async t => {
t.deepEqual(
await pSettle([null, undefined]),
[
{
status: 'fulfilled',
value: null,
isFulfilled: true,
isRejected: false,
},
{
status: 'fulfilled',
value: undefined,
isFulfilled: true,
isRejected: false,
},
],
);
});
test('compatible with PromiseSettledResult', async t => {
const result = await pSettle([delay(100, {value: 1}), 2, Promise.reject(new Error('3'))], {concurrency: 2});
t.truthy(result.every(item => item.status === 'fulfilled' || item.status === 'rejected'));
t.truthy(result.every(item => (item.status === 'fulfilled' ? 'value' : 'reason') in item));
});