-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy patherrorHook.test.ts
166 lines (118 loc) · 4.14 KB
/
errorHook.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { useState, useEffect } from 'react'
import { renderHook } from '..'
describe('error hook tests', () => {
function useError(throwError?: boolean) {
if (throwError) {
throw new Error('expected')
}
return true
}
function useAsyncError(throwError: boolean) {
const [value, setValue] = useState<boolean>()
useEffect(() => {
const timeout = setTimeout(() => setValue(throwError), 100)
return () => clearTimeout(timeout)
}, [throwError])
return useError(value)
}
function useEffectError(throwError: boolean) {
useEffect(() => {
useError(throwError)
}, [throwError])
return true
}
describe('synchronous', () => {
test('should raise error', () => {
const { result } = renderHook(() => useError(true))
expect(() => {
expect(result.current).not.toBe(undefined)
}).toThrow(Error('expected'))
})
test('should capture error', () => {
const { result } = renderHook(() => useError(true))
expect(result.error).toEqual(Error('expected'))
})
test('should not capture error', () => {
const { result } = renderHook(() => useError(false))
expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
test('should reset error', () => {
const { result, hydrate, rerender } = renderHook(({ throwError }) => useError(throwError), {
initialProps: { throwError: true }
})
expect(result.error).not.toBe(undefined)
hydrate()
rerender({ throwError: false })
expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
})
describe('asynchronous', () => {
test('should raise async error', async () => {
const { result, hydrate, waitForNextUpdate } = renderHook(() => useAsyncError(true))
hydrate()
await waitForNextUpdate()
expect(() => {
expect(result.current).not.toBe(undefined)
}).toThrow(Error('expected'))
})
test('should capture async error', async () => {
const { result, hydrate, waitForNextUpdate } = renderHook(() => useAsyncError(true))
hydrate()
await waitForNextUpdate()
expect(result.error).toEqual(Error('expected'))
})
test('should not capture async error', async () => {
const { result, hydrate, waitForNextUpdate } = renderHook(() => useAsyncError(false))
hydrate()
await waitForNextUpdate()
expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
test('should reset async error', async () => {
const { result, hydrate, waitForNextUpdate, rerender } = renderHook(
({ throwError }) => useAsyncError(throwError),
{ initialProps: { throwError: true } }
)
hydrate()
await waitForNextUpdate()
expect(result.error).not.toBe(undefined)
rerender({ throwError: false })
await waitForNextUpdate()
expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
})
describe('effect', () => {
test('should raise effect error', () => {
const { result, hydrate } = renderHook(() => useEffectError(true))
hydrate()
expect(() => {
expect(result.current).not.toBe(undefined)
}).toThrow(Error('expected'))
})
test('should capture effect error', () => {
const { result, hydrate } = renderHook(() => useEffectError(true))
hydrate()
expect(result.error).toEqual(Error('expected'))
})
test('should not capture effect error', () => {
const { result, hydrate } = renderHook(() => useEffectError(false))
hydrate()
expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
test('should reset effect error', () => {
const { result, hydrate, rerender } = renderHook(
({ throwError }) => useEffectError(throwError),
{ initialProps: { throwError: true } }
)
hydrate()
expect(result.error).not.toBe(undefined)
rerender({ throwError: false })
expect(result.current).not.toBe(undefined)
expect(result.error).toBe(undefined)
})
})
})