-
Notifications
You must be signed in to change notification settings - Fork 534
Expand file tree
/
Copy patherror-boundary.test.tsx
More file actions
184 lines (159 loc) · 5.21 KB
/
Copy patherror-boundary.test.tsx
File metadata and controls
184 lines (159 loc) · 5.21 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React from 'react'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import ErrorBoundary from './error-boundary'
// Helpers
const Ok: React.FC = () => <div data-testid="ok">ok</div>
const Thrower: React.FC<{ msg?: string }> = ({ msg = 'boom' }) => {
throw new Error(msg)
}
const CustomFallback: React.FC<{ error?: Error; componentStack?: string }> = ({ error, componentStack }) => (
<div role="alert">
<span data-testid="cf-msg">{error?.message}</span>
<span data-testid="cf-stack">{componentStack ?? ''}</span>
</div>
)
describe('ErrorBoundary', () => {
let consoleErrorSpy: jest.SpyInstance
beforeEach(() => {
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
consoleErrorSpy.mockRestore()
})
it('renders children when no error occurs', () => {
render(
<ErrorBoundary>
<Ok />
</ErrorBoundary>
)
expect(screen.getByTestId('ok')).toBeInTheDocument()
})
it('renders default fallback UI when a child component throws an error', () => {
render(
<ErrorBoundary>
<Thrower msg="kapow" />
</ErrorBoundary>
)
const alert = screen.getByRole('alert')
expect(alert).toBeInTheDocument()
expect(alert).toHaveTextContent('kapow')
expect(consoleErrorSpy).toHaveBeenCalled()
})
it('calls onError callback with error and component stack info', () => {
const onError = jest.fn()
render(
<ErrorBoundary onError={onError}>
<Thrower />
</ErrorBoundary>
)
expect(onError).toHaveBeenCalledTimes(1)
const [err, info] = onError.mock.calls[0]
expect((err as Error).message).toBe('boom')
expect(info && typeof (info as any).componentStack).toBe('string')
})
it('auto-resets error state when resetKeys prop changes', () => {
const { rerender } = render(
<ErrorBoundary resetKeys={[0]}>
<Thrower />
</ErrorBoundary>
)
expect(screen.getByRole('alert')).toBeInTheDocument()
rerender(
<ErrorBoundary resetKeys={[1]}>
<Ok />
</ErrorBoundary>
)
expect(screen.getByTestId('ok')).toBeInTheDocument()
})
it('does not reset error state when resetKeys prop remains the same', () => {
const { rerender } = render(
<ErrorBoundary resetKeys={[1, 2, 3]}>
<Thrower />
</ErrorBoundary>
)
expect(screen.getByRole('alert')).toBeInTheDocument()
rerender(
<ErrorBoundary resetKeys={[1, 2, 3]}>
<Ok />
</ErrorBoundary>
)
expect(screen.getByRole('alert')).toBeInTheDocument()
})
it('renders custom fallback component with error and component stack', () => {
render(
<ErrorBoundary fallback={CustomFallback}>
<Thrower msg="custom-msg" />
</ErrorBoundary>
)
expect(screen.getByRole('alert')).toBeInTheDocument()
expect(screen.getByTestId('cf-msg')).toHaveTextContent('custom-msg')
expect(screen.getByTestId('cf-stack')).toBeInTheDocument()
})
it('renders default fallback when fallback prop is undefined', () => {
render(
<ErrorBoundary fallback={undefined}>
<Thrower msg="undefined-fallback" />
</ErrorBoundary>
)
const alert = screen.getByRole('alert')
expect(alert).toBeInTheDocument()
expect(alert).toHaveTextContent('undefined-fallback')
})
it('renders nothing when fallback component returns null', () => {
const NullFallback = () => null
render(
<ErrorBoundary fallback={NullFallback}>
<Thrower msg="null-fallback" />
</ErrorBoundary>
)
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
})
it('handles error with undefined error message gracefully', () => {
const ThrowerUndefined: React.FC = () => {
const error = new Error()
error.message = undefined as any
throw error
}
render(
<ErrorBoundary>
<ThrowerUndefined />
</ErrorBoundary>
)
const alert = screen.getByRole('alert')
expect(alert).toBeInTheDocument()
expect(alert.querySelector('pre')).toBeInTheDocument()
})
it('renders error message with component stack when available', () => {
const original = ErrorBoundary.prototype.componentDidCatch
ErrorBoundary.prototype.componentDidCatch = function (error, info) {
console.error(`${error.message} - ${info.componentStack}`)
this.setState({ componentStack: 'at <Thrower />' })
this.props.onError?.(error, info)
}
render(
<ErrorBoundary>
<Thrower msg="stacked" />
</ErrorBoundary>
)
const pre = screen.getByRole('alert').querySelector('pre')!
expect(pre).toHaveTextContent('stacked')
expect(pre).toHaveTextContent(' - at <Thrower />')
ErrorBoundary.prototype.componentDidCatch = original
})
it('resets error state when resetKeys arrays have different lengths', () => {
const { rerender } = render(
<ErrorBoundary resetKeys={[1, 2, 3]}>
<Thrower />
</ErrorBoundary>
)
expect(screen.getByRole('alert')).toBeInTheDocument()
// Different length arrays should reset
rerender(
<ErrorBoundary resetKeys={[1, 2]}>
<Ok />
</ErrorBoundary>
)
expect(screen.getByTestId('ok')).toBeInTheDocument()
})
})