Skip to content

Commit 857a1d3

Browse files
committed
Add some test cases for the same loop promise.
1 parent 17329ed commit 857a1d3

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

test/unit/same-loop-promise.test.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* global describe, it, expect */
2+
3+
import { SameLoopPromise } from '../../dist/lib/dynamic'
4+
5+
describe('SameLoopPromise', () => {
6+
describe('basic api', () => {
7+
it('should support basic promise resolving', (done) => {
8+
const promise = new SameLoopPromise((resolve) => {
9+
setTimeout(() => {
10+
resolve(1000)
11+
}, 100)
12+
})
13+
14+
promise.then((value) => {
15+
expect(value).toBe(1000)
16+
done()
17+
})
18+
})
19+
20+
it('should support resolving in the same loop', () => {
21+
let gotValue = null
22+
const promise = new SameLoopPromise((resolve) => {
23+
resolve(1000)
24+
})
25+
26+
promise.then((value) => {
27+
gotValue = value
28+
})
29+
30+
expect(gotValue).toBe(1000)
31+
})
32+
33+
it('should support basic promise rejecting', (done) => {
34+
const error = new Error('Hello Error')
35+
const promise = new SameLoopPromise((resolve, reject) => {
36+
setTimeout(() => {
37+
reject(error)
38+
}, 100)
39+
})
40+
41+
promise.catch((err) => {
42+
expect(err).toBe(error)
43+
done()
44+
})
45+
})
46+
47+
it('should support rejecting in the same loop', () => {
48+
const error = new Error('Hello Error')
49+
let gotError = null
50+
const promise = new SameLoopPromise((resolve, reject) => {
51+
reject(error)
52+
})
53+
54+
promise.catch((err) => {
55+
gotError = err
56+
})
57+
58+
expect(gotError).toBe(error)
59+
})
60+
})
61+
62+
describe('complex usage', () => {
63+
it('should support a chain of promises', (done) => {
64+
const promise = new SameLoopPromise((resolve) => {
65+
setTimeout(() => {
66+
resolve(1000)
67+
}, 100)
68+
})
69+
70+
promise
71+
.then((value) => value * 2)
72+
.then((value) => value + 10)
73+
.then((value) => {
74+
expect(value).toBe(2010)
75+
done()
76+
})
77+
})
78+
79+
it('should handle the error inside the then', (done) => {
80+
const error = new Error('1000')
81+
const promise = new SameLoopPromise((resolve, reject) => {
82+
setTimeout(() => {
83+
reject(error)
84+
}, 100)
85+
})
86+
87+
promise
88+
.then(
89+
() => 4000,
90+
(err) => parseInt(err.message)
91+
)
92+
.then((value) => value + 10)
93+
.then((value) => {
94+
expect(value).toBe(1010)
95+
done()
96+
})
97+
})
98+
99+
it('should catch the error at the end', (done) => {
100+
const error = new Error('1000')
101+
const promise = new SameLoopPromise((resolve, reject) => {
102+
setTimeout(() => {
103+
reject(error)
104+
}, 100)
105+
})
106+
107+
promise
108+
.then((value) => value * 2)
109+
.then((value) => value + 10)
110+
.catch((err) => {
111+
expect(err).toBe(error)
112+
done()
113+
})
114+
})
115+
116+
it('should catch and proceed', (done) => {
117+
const error = new Error('1000')
118+
const promise = new SameLoopPromise((resolve, reject) => {
119+
setTimeout(() => {
120+
reject(error)
121+
}, 100)
122+
})
123+
124+
promise
125+
.then((value) => value * 2)
126+
.then((value) => value + 10)
127+
.catch((err) => {
128+
expect(err).toBe(error)
129+
return 5000
130+
})
131+
.then((value) => {
132+
expect(value).toBe(5000)
133+
done()
134+
})
135+
})
136+
})
137+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy