-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
81 lines (69 loc) · 2.09 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
const test = require('blue-tape')
const path = require('path')
const pipe = require(path.join(__dirname, process.env.PROMISED_PIPE_LIB_FILE || 'index.js'))
test('works with a sync function', t => {
return pipe(Math.abs)(-3).then(value => {
t.equals(value, 3)
return value
})
})
test('works with multiple arguments', t => {
return pipe(Math.pow)(2, 3).then(value => {
t.equals(value, 8)
return value
})
})
test('works with an array as an input', t => {
const sum = numbers => numbers.reduce((acc, cur) => acc + cur, 0)
return pipe(sum)([1, 5, 9]).then(value => {
t.equals(value, 15)
return value
})
})
test('works with a mix of functions', t => {
return pipe(
x => x + 1, // sync
asyncAbs, // async
x => x - 1 // sync
)(-3).then(value => {
t.equals(value, 1)
return value
})
})
test('fails when there is an error inside the chain', t => {
return t.shouldFail(pipe(
x => x + 1,
() => { throw Error('test') },
x => { return console.log('I should not be called') }
)(-3), Error)
})
test('fails when there is an error in the first function', t => {
return t.shouldFail(pipe(
() => { throw Error('test') }
)(), Error)
})
test('fails when there is a rejection inside the chain', t => {
return t.shouldFail(pipe(
x => x + 1,
() => { return Promise.reject(Error('test')) },
x => { return console.log('I should not be called') }
)(-3), Error)
})
test('fails when there is a rejection in the first function', t => {
return t.shouldFail(pipe(
() => { return Promise.reject(Error('test')) }
)(), Error)
})
test('fails when no arguments supplied', t => {
t.throws(() => {
pipe()
}, 'pipe requires at least one argument')
t.end()
})
test('fails when an argument is not a function', t => {
t.throws(() => {
pipe('not a function')
}, 'pipe requires each argument to be a function. Argument #1 is of type "string"')
t.end()
})
const asyncAbs = x => Promise.resolve(Math.abs(x))