/
test.js
58 lines (42 loc) · 1.24 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
import test from 'ava'
import sp from './lib'
let asyncFunctionBuilder = (success) => (value, timeouts = 1000) => {
return new Promise((resolve, reject) => {
setTimeout(function () {
if (success) {
resolve(value)
} else {
reject(new TypeError(value))
}
}, timeouts)
})
}
test('Async function transform', t => {
const syncFunc = sp(asyncFunctionBuilder(true))
const expectedReturn = 5
const returnValue = syncFunc(expectedReturn)
t.is(returnValue, expectedReturn)
t.pass()
})
test('it should resolve undefined promise result', t => {
const syncFunc = sp(asyncFunctionBuilder(true))
const expectedReturn = undefined
const returnValue = syncFunc(expectedReturn)
t.is(returnValue, expectedReturn)
t.pass()
})
test('it would throw promise rejection', t => {
const expectedReturn = '🦄'
const syncFunc = sp(asyncFunctionBuilder(false))
const error = t.throws(() => {
syncFunc(expectedReturn)
}, TypeError)
t.is(error.message, expectedReturn);
})
test('Get timeout error', t => {
const timeoutFunc = sp(asyncFunctionBuilder(true), { timeouts: 100 })
const error = t.throws(() => {
timeoutFunc(undefined, 1000)
}, Error)
t.truthy(error.message.match('called timeout'))
})