-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.js
56 lines (40 loc) · 1.31 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
import test from 'ava';
import pIf from './index.js';
const fixture = Symbol('fixture');
test('if', async t => {
const value = await Promise.resolve(fixture)
.then(pIf(true, () => 'if', () => 'else'));
t.is(value, 'if');
});
test('else', async t => {
const value = await Promise.resolve(fixture)
.then(pIf(false, () => 'if', () => 'else'));
t.is(value, 'else');
});
test('passthrough', async t => {
const value = await Promise.resolve(fixture)
.then(pIf(false, () => 'if'));
t.is(value, fixture);
});
test('composability', async t => {
const value = await Promise.resolve(fixture)
.then(pIf(true, pIf(false, () => 'if', () => 'else')));
t.is(value, 'else');
});
test('condition can be a function', async t => {
const isEmpty = array => array.length === 0;
const valueA = await Promise.resolve([])
.then(pIf(isEmpty, array => [...array, 42]));
const valueB = await Promise.resolve([1])
.then(pIf(isEmpty, array => [...array, 42]));
t.deepEqual(valueA, [42]);
t.deepEqual(valueB, [1]);
});
test('condition can be an async function', async t => {
const valueA = await Promise.resolve([])
.then(pIf(async () => true, array => [...array, 42]));
const valueB = await Promise.resolve([1])
.then(pIf(async () => false, array => [...array, 42]));
t.deepEqual(valueA, [42]);
t.deepEqual(valueB, [1]);
});