-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
148 lines (117 loc) · 6.55 KB
/
index.test.ts
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
/* eslint-disable no-throw-literal, style/max-statements-per-line, prefer-promise-reject-errors */
import { describe, expect, it } from 'vitest'
import thenRef from './index'
const syncResolve = () => 1
const asyncResolve = () => Promise.resolve(1)
function syncReject() { throw 1 }
const asyncReject = () => Promise.reject(1)
describe('then-ref', () => {
it('value', async () => {
expect(thenRef(syncResolve)().value).toBe(1)
expect(thenRef(syncResolve()).value).toBe(1)
expect(await thenRef(asyncResolve()).value).toBe(1)
})
it('then', async () => {
expect(thenRef(syncResolve()).then((res: number) => ++res).value).toBe(2)
expect(await thenRef(asyncResolve()).then((res: number) => ++res).value).toBe(2)
})
it('catch', async () => {
expect(thenRef(syncReject)().catch((res: number) => ++res).value).toBe(2)
expect(await thenRef(asyncReject()).catch((res: number) => ++res).value).toBe(2)
})
it('finally', async () => {
let count = 0
expect(thenRef(syncResolve()).finally(() => count++).then((res: number) => ++res).value).toBe(2)
expect(count).toBe(1)
expect(await thenRef(asyncResolve()).finally(() => count++).then((res: number) => ++res).value).toBe(2)
expect(count).toBe(2)
expect(thenRef(syncReject)().finally(() => count++).catch((res: number) => ++res).value).toBe(2)
expect(count).toBe(3)
expect(await thenRef(asyncReject()).finally(() => count++).catch((res: number) => ++res).value).toBe(2)
expect(count).toBe(4)
})
it('then.onrejected', async () => {
expect(thenRef(syncReject)().then(null, (res: number) => ++res).value).toBe(2)
expect(await thenRef(asyncReject()).then(null, (res: number) => ++res).value).toBe(2)
})
it('then -> catch', async () => {
expect(thenRef(syncReject)().then((res: number) => ++res).catch((res: number) => ++res).value).toBe(2)
expect(await thenRef(asyncReject()).then((res: number) => ++res).catch((res: number) => ++res).value).toBe(2)
})
it('then -> finally', async () => {
expect(thenRef(syncResolve()).then((res: number) => ++res).finally(() => 3).value).toBe(2)
expect(await thenRef(asyncResolve()).then((res: number) => ++res).finally(() => 3).value).toBe(2)
})
it('then.onrejected -> then', async () => {
expect(thenRef(syncReject)().then(null, (res: number) => ++res).then((res: number) => ++res).value).toBe(3)
expect(await thenRef(asyncReject()).then(null, (res: number) => ++res).then((res: number) => ++res).value).toBe(3)
})
it('then.onrejected -> catch', async () => {
expect(thenRef(syncReject)().then(null, (res: number) => ++res).catch((res: number) => ++res).value).toBe(2)
expect(await thenRef(asyncReject()).then(null, (res: number) => ++res).catch((res: number) => ++res).value).toBe(2)
})
it('catch -> then', async () => {
expect(thenRef(syncReject)().catch((res: number) => ++res).then((res: number) => ++res).value).toBe(3)
expect(await thenRef(asyncReject()).catch((res: number) => ++res).then((res: number) => ++res).value).toBe(3)
})
it('catch -> finally', async () => {
expect(thenRef(syncReject)().catch((res: number) => ++res).finally(() => 3).value).toBe(2)
expect(await thenRef(asyncReject()).catch((res: number) => ++res).finally(() => 3).value).toBe(2)
})
it('then -> catch -> then', async () => {
expect(thenRef(syncResolve()).then((res: number) => { throw res + 1 }).catch((res: number) => ++res).value).toBe(3)
expect(await thenRef(asyncResolve()).then((res: number) => { throw res + 1 }).catch((res: number) => ++res).value).toBe(3)
})
it('then -> catch -> finally', async () => {
let count = 0
expect(thenRef(syncResolve()).then((res: number) => { throw res + 1 }).catch((res: number) => ++res).finally(() => count++).value).toBe(3)
expect(count).toBe(1)
expect(await thenRef(asyncResolve()).then((res: number) => { throw res + 1 }).catch((res: number) => ++res).finally(() => count++).value).toBe(3)
expect(count).toBe(2)
})
it('then -> finally -> then', async () => {
let count = 0
expect(thenRef(syncResolve()).then((res: number) => ++res).finally(() => count++).then((res: number) => ++res).value).toBe(3)
expect(count).toBe(1)
expect(await thenRef(asyncResolve()).then((res: number) => ++res).finally(() => count++).then((res: number) => ++res).value).toBe(3)
expect(count).toBe(2)
})
it('then -> finally -> catch', async () => {
let count = 0
expect(thenRef(syncResolve()).then((res: number) => { throw res + 1 }).finally(() => count++).catch((res: number) => ++res).value).toBe(3)
expect(count).toBe(1)
expect(await thenRef(asyncResolve()).then((res: number) => { throw res + 1 }).finally(() => count++).catch((res: number) => ++res).value).toBe(3)
expect(count).toBe(2)
})
it('catch -> then -> catch', async () => {
expect(thenRef(syncReject)().catch((res: number) => ++res).then((res: number) => { throw res + 1 }).catch((res: number) => ++res).value).toBe(4)
expect(await thenRef(asyncReject()).catch((res: number) => ++res).then((res: number) => { throw res + 1 }).catch((res: number) => ++res).value).toBe(4)
})
it('catch -> then -> finally', async () => {
let count = 0
expect(thenRef(syncReject)().catch((res: number) => ++res).then((res: number) => ++res).finally(() => count++).value).toBe(3)
expect(count).toBe(1)
expect(await thenRef(asyncReject()).catch((res: number) => ++res).then((res: number) => ++res).finally(() => count++).value).toBe(3)
expect(count).toBe(2)
})
it('catch -> finally -> then', async () => {
let count = 0
expect(thenRef(syncReject)().catch((res: number) => ++res).finally(() => count++).then((res: number) => ++res).value).toBe(3)
expect(count).toBe(1)
expect(await thenRef(asyncReject()).catch((res: number) => ++res).finally(() => count++).then((res: number) => ++res).value).toBe(3)
expect(count).toBe(2)
})
it('catch -> finally -> catch', async () => {
let count = 0
expect(thenRef(syncReject)().catch((res: number) => ++res).finally(() => count++).catch((res: number) => ++res).value).toBe(2)
expect(count).toBe(1)
expect(await thenRef(asyncReject()).catch((res: number) => ++res).finally(() => count++).catch((res: number) => ++res).value).toBe(2)
expect(count).toBe(2)
})
it('nesting', async () => {
expect(thenRef(thenRef(syncResolve))().value).toBe(1)
expect(await thenRef(thenRef(asyncResolve())).value).toBe(1)
expect(thenRef(syncResolve)().then((res: number) => thenRef(res)).value).toBe(1)
expect(await thenRef(asyncResolve)().then((res: number) => thenRef(res)).value).toBe(1)
})
})