-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.test.js
62 lines (53 loc) · 1.77 KB
/
index.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
const catchify = require('./index');
const test = require('ava');
test('catchify - promise with success', async t => {
const [error, value] = await catchify(Promise.resolve(1));
t.is(error, null);
t.is(value, 1);
});
test('catchify.resolve - promise with success', async t => {
const [error, value] = await catchify.resolve(Promise.resolve(1));
t.is(error, null);
t.is(value, 1);
});
test('catchify.reject - value', async t => {
const [error, value] = await catchify.reject(1);
t.is(error, 1);
t.is(value, null);
});
test('catchify.race - two values', async t => {
const [error, value] = await catchify.race([1, 2]);
t.is(error, null);
t.is(value, 1);
});
test('catchify.all - two values resolved', async t => {
const [error, [value1, value2]] = await catchify.all([1, 2]);
t.is(error, null);
t.is(value1, 1);
t.is(value2, 2);
});
test('catchify.some - resolve and reject', async t => {
const [[error1, error2], [value1, value2]] = await catchify.some([
Promise.resolve(1),
Promise.reject(new Error('2'))
]);
t.is(error1, null);
t.is(error2.message, '2');
t.is(value1, 1);
t.is(value2, null);
});
test('catchify.limit - three promises, last one rejects', async t => {
const [errors, values] = await catchify.limit([
new Promise((resolve, reject) => setTimeout(() => resolve(1), 5)),
new Promise((resolve, reject) => setTimeout(() => resolve(2), 5)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error('3')), 5))
]);
t.deepEqual(errors, [null, null, new Error('3')]);
t.deepEqual(values, [1, 2, null]);
});
test('catchify.newPromiseHandle returns a promise handle object', t => {
const handle = catchify.newPromiseHandle();
t.truthy(handle.promise);
t.truthy(handle.resolve);
t.truthy(handle.reject);
});