-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
65 lines (56 loc) · 1.57 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
/* eslint-disable unicorn/error-message */
import test from 'ava';
import delay from 'delay';
import PCancelable, {CancelError} from 'p-cancelable';
import pAny from './index.js';
test('returns the first fulfilled value', async t => {
const fixture = [
Promise.reject(new Error(1)),
Promise.resolve(2),
Promise.reject(new Error(3)),
Promise.resolve(4)
];
t.is(await pAny(fixture), 2);
});
test('returns the first fulfilled value #2', async t => {
const fixture = [
delay(100, {value: 1}),
delay(10, {value: 2}),
delay(50, {value: 3})
];
t.is(await pAny(fixture), 2);
});
test('returns the first fulfilled value that passes the filter function', async t => {
const fixture = [
Promise.resolve(1),
Promise.resolve('foo'),
Promise.resolve('unicorn')
];
t.is(await pAny(fixture, {filter: value => value === 'unicorn'}), 'unicorn');
});
test('returns a cancelable promise', t => {
const fixture = [Promise.resolve(1)];
t.true(pAny(fixture) instanceof PCancelable);
});
test('cancels all promises when returned promise is canceled', async t => {
const canceled = [false, false];
const fixture = [
new PCancelable((resolve, reject, onCancel) =>
onCancel(() => {
canceled[0] = true;
})
),
new PCancelable((resolve, reject, onCancel) =>
onCancel(() => {
canceled[1] = true;
})
)
];
const promise = pAny(fixture);
promise.cancel();
await t.throwsAsync(promise, {instanceOf: CancelError});
t.deepEqual(canceled, [true, true]);
});
test('rejects on empty iterable', async t => {
await t.throwsAsync(pAny([]), {instanceOf: RangeError});
});