-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyPromise.test.js
161 lines (140 loc) · 4.48 KB
/
MyPromise.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const MyPromise = require("./MyPromise.js")
// const MyPromise = Promise
const DEFAULT_result = "default"
describe("then", () => {
it("with no chaining", () => {
return promise().then((v) => expect(v).toEqual(DEFAULT_result))
})
it("with multiple thens for same promise", () => {
const checkFunc = (v) => expect(v).toEqual(DEFAULT_result)
const mainPromise = promise()
const promise1 = mainPromise.then(checkFunc)
const promise2 = mainPromise.then(checkFunc)
return Promise.allSettled([promise1, promise2])
})
it("with then and catch", () => {
const checkFunc = (v) => expect(v).toEqual(DEFAULT_result)
const failFunc = (v) => expect(1).toEqual(2)
const resolvePromise = promise().then(checkFunc, failFunc)
const rejectPromise = promise({ fail: true }).then(failFunc, checkFunc)
return Promise.allSettled([resolvePromise, rejectPromise])
})
it("with chaining", () => {
return promise({ result: 3 })
.then((v) => v * 4)
.then((v) => expect(v).toEqual(12))
})
})
describe("catch", () => {
it("with no chaining", () => {
return promise({ fail: true }).catch((v) =>
expect(v).toEqual(DEFAULT_result)
)
})
it("with multiple catches for same promise", () => {
const checkFunc = (v) => expect(v).toEqual(DEFAULT_result)
const mainPromise = promise({ fail: true })
const promise1 = mainPromise.catch(checkFunc)
const promise2 = mainPromise.catch(checkFunc)
return Promise.allSettled([promise1, promise2])
})
it("with chaining", () => {
return promise({ result: 3 })
.then((v) => {
throw v * 4
})
.catch((v) => expect(v).toEqual(12))
})
})
describe("finally", () => {
it("with no chaining", () => {
const checkFunc = (v) => (v) => expect(v).toBeUndefined()
const successPromise = promise().finally(checkFunc)
const failPromise = promise({ fail: true }).finally(checkFunc)
return Promise.allSettled([successPromise, failPromise])
})
it("with multiple finallys for same promise", () => {
const checkFunc = (v) => expect(v).toBeUndefined()
const mainPromise = promise()
const promise1 = mainPromise.finally(checkFunc)
const promise2 = mainPromise.finally(checkFunc)
return Promise.allSettled([promise1, promise2])
})
it("with chaining", () => {
const checkFunc = (v) => (v) => expect(v).toBeUndefined()
const successPromise = promise()
.then((v) => v)
.finally(checkFunc)
const failPromise = promise({ fail: true })
.then((v) => v)
.finally(checkFunc)
return Promise.allSettled([successPromise, failPromise])
})
})
describe("static methods", () => {
it("resolve", () => {
return MyPromise.resolve(DEFAULT_result).then((v) =>
expect(v).toEqual(DEFAULT_result)
)
})
it("reject", () => {
return MyPromise.reject(DEFAULT_result).catch((v) =>
expect(v).toEqual(DEFAULT_result)
)
})
describe("all", () => {
it("with success", () => {
return MyPromise.all([
promise({ result: 1 }),
promise({ result: 2 }),
]).then((v) => expect(v).toEqual([1, 2]))
})
it("with fail", () => {
return MyPromise.all([promise(), promise({ fail: true })]).catch((v) =>
expect(v).toEqual(DEFAULT_result)
)
})
})
it("allSettled", () => {
return MyPromise.allSettled([promise(), promise({ fail: true })]).then(
(v) =>
expect(v).toEqual([
{ status: "fulfilled", result: DEFAULT_result },
{ status: "rejected", reason: DEFAULT_result },
])
)
})
describe("race", () => {
it("with success", () => {
return MyPromise.race([
promise({ result: 1 }),
promise({ result: 2 }),
]).then((v) => expect(v).toEqual(1))
})
it("with fail", () => {
return MyPromise.race([
promise({ fail: true, result: 1 }),
promise({ fail: true, result: 2 }),
]).catch((v) => expect(v).toEqual(1))
})
})
describe("any", () => {
it("with success", () => {
return MyPromise.any([
promise({ result: 1 }),
promise({ result: 2 }),
]).then((v) => expect(v).toEqual(1))
})
it("with fail", () => {
return MyPromise.any([
promise({ fail: true, result: 1 }),
promise({ result: 2 }),
]).catch((e) => expect(e.errors).toEqual([1, 2]))
})
})
})
function promise({ result = DEFAULT_result, fail = false } = {}) {
return new MyPromise((resolve, reject) => {
fail ? reject(result) : resolve(result)
})
}