/
errorboundary.test.tsx
231 lines (192 loc) · 6.73 KB
/
errorboundary.test.tsx
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { fireEvent, render, screen } from '@testing-library/react';
import * as React from 'react';
import { ErrorBoundary, ErrorBoundaryProps, FALLBACK_ERR_MESSAGE } from '../src/errorboundary';
const mockCaptureException = jest.fn();
const mockShowReportDialog = jest.fn();
jest.mock('@sentry/browser', () => ({
captureException: (err: any, ctx: any) => {
mockCaptureException(err, ctx);
},
showReportDialog: (options: any) => {
mockShowReportDialog(options);
},
}));
const TestApp: React.FC<ErrorBoundaryProps> = ({ children, ...props }) => {
const [isError, setError] = React.useState(false);
return (
<ErrorBoundary {...props}>
{isError ? <Bam /> : children}
<button
data-testid="errorBtn"
onClick={() => {
setError(true);
}}
/>
</ErrorBoundary>
);
};
function Bam(): JSX.Element {
throw new Error('boom');
}
describe('ErrorBoundary', () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
afterEach(() => {
consoleErrorSpy.mockClear();
mockCaptureException.mockClear();
mockShowReportDialog.mockClear();
});
it('throws an error if not given a valid `fallbackRender` prop', () => {
expect(() => {
render(
// @ts-ignore
<ErrorBoundary fallbackRender={'ok'}>
<Bam />
</ErrorBoundary>,
);
}).toThrowError(FALLBACK_ERR_MESSAGE);
expect(consoleErrorSpy).toHaveBeenCalled();
});
it('throws an error if not given a valid `fallback` prop', () => {
expect(() => {
render(
<ErrorBoundary fallback={new Error('true')}>
<Bam />
</ErrorBoundary>,
);
}).toThrowError(FALLBACK_ERR_MESSAGE);
expect(consoleErrorSpy).toHaveBeenCalled();
});
it('does not throw an error if a fallback is given', () => {
expect(() => {
render(
<ErrorBoundary fallback={<h1>Error Component</h1>}>
<h1>children</h1>
</ErrorBoundary>,
);
}).not.toThrowError();
});
it('calls `onMount` when mounted', () => {
const mockOnMount = jest.fn();
render(
<ErrorBoundary fallback={<h1>Error Component</h1>} onMount={mockOnMount}>
<h1>children</h1>
</ErrorBoundary>,
);
expect(mockOnMount).toHaveBeenCalledTimes(1);
});
it('calls `onUnmount` when unmounted', () => {
const mockOnUnmount = jest.fn();
const { unmount } = render(
<ErrorBoundary fallback={<h1>Error Component</h1>} onUnmount={mockOnUnmount}>
<h1>children</h1>
</ErrorBoundary>,
);
expect(mockOnUnmount).toHaveBeenCalledTimes(0);
unmount();
expect(mockOnUnmount).toHaveBeenCalledTimes(1);
expect(mockOnUnmount).toHaveBeenCalledWith(null, null);
});
it('renders children correctly when there is no error', () => {
const { baseElement } = render(
<ErrorBoundary fallback={<h1>Error Component</h1>}>
<h1>children</h1>
</ErrorBoundary>,
);
expect(baseElement.outerHTML).toContain('<h1>children</h1>');
});
describe('fallback', () => {
it('renders a fallback component', async () => {
const { baseElement } = render(
<TestApp fallback={<p>You have hit an error</p>}>
<h1>children</h1>
</TestApp>,
);
expect(baseElement.outerHTML).toContain('<h1>children</h1>');
const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);
expect(baseElement.outerHTML).not.toContain('<h1>children</h1>');
expect(baseElement.outerHTML).toContain('<p>You have hit an error</p>');
});
it('renders a fallbackRender component', async () => {
let errorString = '';
let compStack = '';
const { baseElement } = render(
<TestApp
fallbackRender={({ error, componentStack }) => {
if (error && componentStack) {
errorString = error.toString();
compStack = componentStack;
}
return <div>Fallback here</div>;
}}
>
<h1>children</h1>
</TestApp>,
);
expect(baseElement.outerHTML).toContain('<h1>children</h1>');
const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);
expect(baseElement.outerHTML).not.toContain('<h1>children</h1');
expect(baseElement.outerHTML).toContain('<div>Fallback here</div>');
expect(errorString).toBe('Error: boom');
expect(compStack).toBe(`
in Bam (created by TestApp)
in ErrorBoundary (created by TestApp)
in TestApp`);
});
});
describe('error', () => {
it('calls `componentDidCatch() when an error occurs`', () => {
const mockOnError = jest.fn();
render(
<TestApp fallback={<p>You have hit an error</p>} onError={mockOnError}>
<h1>children</h1>
</TestApp>,
);
expect(mockOnError).toHaveBeenCalledTimes(0);
expect(mockCaptureException).toHaveBeenCalledTimes(0);
const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);
expect(mockOnError).toHaveBeenCalledTimes(1);
expect(mockOnError).toHaveBeenCalledWith(expect.any(Error), expect.any(String));
expect(mockCaptureException).toHaveBeenCalledTimes(1);
expect(mockCaptureException).toHaveBeenCalledWith(expect.any(Error), {
contexts: { componentStack: expect.any(String) },
});
});
it('shows a Sentry Report Dialog with correct options', () => {
const options = { title: 'custom title' };
render(
<TestApp fallback={<p>You have hit an error</p>} showDialog dialogOptions={options}>
<h1>children</h1>
</TestApp>,
);
expect(mockShowReportDialog).toHaveBeenCalledTimes(0);
const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);
expect(mockShowReportDialog).toHaveBeenCalledTimes(1);
expect(mockShowReportDialog).toHaveBeenCalledWith(options);
});
it('resets to initial state when reset', () => {
const mockOnReset = jest.fn();
const { baseElement } = render(
<TestApp
onReset={mockOnReset}
fallbackRender={({ resetError }) => <button data-testid="reset" onClick={resetError} />}
>
<h1>children</h1>
</TestApp>,
);
expect(baseElement.outerHTML).toContain('<h1>children</h1>');
expect(mockOnReset).toHaveBeenCalledTimes(0);
const btn = screen.getByTestId('errorBtn');
fireEvent.click(btn);
expect(baseElement.outerHTML).toContain('<button data-testid="reset">');
expect(mockOnReset).toHaveBeenCalledTimes(0);
const reset = screen.getByTestId('reset');
fireEvent.click(reset);
expect(mockOnReset).toHaveBeenCalledTimes(1);
expect(mockOnReset).toHaveBeenCalledWith(expect.any(Error), expect.any(String));
});
});
});