-
Notifications
You must be signed in to change notification settings - Fork 0
/
promises.test.mjs
72 lines (61 loc) · 1.47 KB
/
promises.test.mjs
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
import { promise, promises } from './promises.mjs'
import { test } from 'tap'
// promise()
test('promise resolves with correct value', async t => {
const p = promise()
p.resolve('hello')
const result = await p
t.equal(result, 'hello')
})
test('promise rejects with correct error', async t => {
const p = promise()
const error = new Error()
p.reject(error)
try {
await p
} catch (err) {
t.equal(err, error)
}
})
test('promise has resolve and reject methods', t => {
const p = promise()
t.type(p.resolve, 'function')
t.type(p.reject, 'function')
t.end()
})
test('promise is an instance of Promise', t => {
const p = promise()
t.ok(p instanceof Promise)
t.end()
})
// promises()
test('promises have promise, resolve, and reject methods', t => {
const ps = promises()
t.type(ps.promise, 'function')
t.type(ps.resolve, 'function')
t.type(ps.reject, 'function')
t.end()
})
test('promises resolve with correct value', async t => {
const ps = promises()
ps.promises = [
ps.promise('hello'),
ps.promise('ignored'),
ps.promise('values')
]
ps.resolve('hello')
const results = await Promise.all(ps.promises)
t.same(results, ['hello', 'hello', 'hello'])
})
test('promises reject with correct error', async t => {
t.plan(1)
const ps = promises()
ps.promises = [ps.promise(), ps.promise()]
const error = new Error()
ps.reject(error)
try {
await Promise.all(ps.promises)
} catch (err) {
t.equal(error, err)
}
})