-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
80 lines (67 loc) · 2.08 KB
/
test.js
File metadata and controls
80 lines (67 loc) · 2.08 KB
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
import test from 'tape'
import join from './index.js'
test(async (t) => {
const a = Promise.resolve(1)
const b = Promise.resolve(2)
const c = Promise.resolve(4)
await join(a, b, c, (...args) => t.deepEqual(args, [1, 2, 4]))
})
test('a promise is returned', async (t) => {
const p = join(1, () => {})
t.ok(p instanceof Promise)
await p
})
test('a promise is returned', async (t) => {
await join(1, () => 2).then((r) => t.equal(r, 2))
})
test('no arguments', async (t) => {
await rejects(t, join(), 'At least two arguments must be passed')
})
test('one non-function argument', async (t) => {
await rejects(t, join('abc'), 'At least two arguments must be passed')
})
test('two non-function arguments', async (t) => {
await rejects(t, join('abc', 'cde'), 'Missing expected function argument')
})
test('one function argument', async (t) => {
await rejects(
t,
join(() => {}),
'At least two arguments must be passed',
)
})
test('mixed value types (promise and not)', async (t) => {
await join(Promise.resolve(1), 2, (...args) => t.deepEqual(args, [1, 2]))
})
test('mixed value types (settled and unsettled)', async (t) => {
const {promise: a, resolve: resolveA} = Promise.withResolvers()
const {promise: b, resolve: resolveB} = Promise.withResolvers()
resolveA(1)
const args = join(a, b, (...args) => args)
resolveB(2)
t.deepEqual(await args, [1, 2])
})
test('single rejection', async (t) => {
const err = new Error()
await join(Promise.reject(err), () => t.fail()).catch((_err) =>
t.equal(_err, err),
)
})
test('multiple rejections', async (t) => {
const errA = new Error()
const errB = new Error()
await join(Promise.reject(errA), Promise.reject(errB), () =>
t.fail(),
).catch((_err) => t.equal(_err, errA))
})
test('both resolved and rejected', async (t) => {
const err = new Error()
await join(Promise.resolve(1), Promise.reject(err), () => t.fail()).catch(
(_err) => t.equal(_err, err),
)
})
async function rejects(t, promise, expectedMessage, message = 'rejected') {
return promise.catch((err) => {
t.ok(err instanceof Error && err.message === expectedMessage, message)
})
}