forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsame-loop-promise.test.js
More file actions
137 lines (118 loc) · 3.17 KB
/
same-loop-promise.test.js
File metadata and controls
137 lines (118 loc) · 3.17 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* global describe, it, expect */
import { SameLoopPromise } from '../../dist/lib/dynamic'
describe('SameLoopPromise', () => {
describe('basic api', () => {
it('should support basic promise resolving', (done) => {
const promise = new SameLoopPromise((resolve) => {
setTimeout(() => {
resolve(1000)
}, 100)
})
promise.then((value) => {
expect(value).toBe(1000)
done()
})
})
it('should support resolving in the same loop', () => {
let gotValue = null
const promise = new SameLoopPromise((resolve) => {
resolve(1000)
})
promise.then((value) => {
gotValue = value
})
expect(gotValue).toBe(1000)
})
it('should support basic promise rejecting', (done) => {
const error = new Error('Hello Error')
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error)
}, 100)
})
promise.catch((err) => {
expect(err).toBe(error)
done()
})
})
it('should support rejecting in the same loop', () => {
const error = new Error('Hello Error')
let gotError = null
const promise = new SameLoopPromise((resolve, reject) => {
reject(error)
})
promise.catch((err) => {
gotError = err
})
expect(gotError).toBe(error)
})
})
describe('complex usage', () => {
it('should support a chain of promises', (done) => {
const promise = new SameLoopPromise((resolve) => {
setTimeout(() => {
resolve(1000)
}, 100)
})
promise
.then((value) => value * 2)
.then((value) => value + 10)
.then((value) => {
expect(value).toBe(2010)
done()
})
})
it('should handle the error inside the then', (done) => {
const error = new Error('1000')
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error)
}, 100)
})
promise
.then(
() => 4000,
(err) => parseInt(err.message)
)
.then((value) => value + 10)
.then((value) => {
expect(value).toBe(1010)
done()
})
})
it('should catch the error at the end', (done) => {
const error = new Error('1000')
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error)
}, 100)
})
promise
.then((value) => value * 2)
.then((value) => value + 10)
.catch((err) => {
expect(err).toBe(error)
done()
})
})
it('should catch and proceed', (done) => {
const error = new Error('1000')
const promise = new SameLoopPromise((resolve, reject) => {
setTimeout(() => {
reject(error)
}, 100)
})
promise
.then((value) => value * 2)
.then((value) => value + 10)
.catch((err) => {
expect(err).toBe(error)
return 5000
})
.then((value) => {
expect(value).toBe(5000)
done()
})
})
})
})