1
1
import { fireEvent , render , screen } from '@testing-library/react' ;
2
2
import * as React from 'react' ;
3
3
4
- import { ErrorBoundary , ErrorBoundaryProps , FALLBACK_ERR_MESSAGE } from '../src/errorboundary' ;
4
+ import { ErrorBoundary , ErrorBoundaryProps } from '../src/errorboundary' ;
5
5
6
6
const mockCaptureException = jest . fn ( ) ;
7
7
const mockShowReportDialog = jest . fn ( ) ;
@@ -35,45 +35,43 @@ function Bam(): JSX.Element {
35
35
}
36
36
37
37
describe ( 'ErrorBoundary' , ( ) => {
38
- const consoleErrorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
38
+ jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
39
39
40
40
afterEach ( ( ) => {
41
- consoleErrorSpy . mockClear ( ) ;
42
41
mockCaptureException . mockClear ( ) ;
43
42
mockShowReportDialog . mockClear ( ) ;
44
43
} ) ;
45
44
46
- it ( 'throws an error if not given a valid `fallbackRender` prop' , ( ) => {
47
- expect ( ( ) => {
48
- render (
49
- // @ts -ignore
50
- < ErrorBoundary fallbackRender = { 'ok' } >
51
- < Bam />
52
- </ ErrorBoundary > ,
53
- ) ;
54
- } ) . toThrowError ( FALLBACK_ERR_MESSAGE ) ;
55
- expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
45
+ it ( 'renders null if not given a valid `fallbackRender` prop' , ( ) => {
46
+ const { container } = render (
47
+ // @ts -ignore
48
+ < ErrorBoundary fallbackRender = { 'ok' } >
49
+ < Bam />
50
+ </ ErrorBoundary > ,
51
+ ) ;
52
+
53
+ expect ( container . innerHTML ) . toBe ( '' ) ;
56
54
} ) ;
57
55
58
- it ( 'throws an error if not given a valid `fallback` prop' , ( ) => {
59
- expect ( ( ) => {
60
- render (
61
- < ErrorBoundary fallback = { new Error ( 'true' ) } >
62
- < Bam />
63
- </ ErrorBoundary > ,
64
- ) ;
65
- } ) . toThrowError ( FALLBACK_ERR_MESSAGE ) ;
66
- expect ( consoleErrorSpy ) . toHaveBeenCalled ( ) ;
56
+ it ( 'renders null if not given a valid `fallback` prop' , ( ) => {
57
+ const { container } = render (
58
+ // @ts -ignore
59
+ < ErrorBoundary fallback = { new Error ( 'true' ) } >
60
+ < Bam />
61
+ </ ErrorBoundary > ,
62
+ ) ;
63
+
64
+ expect ( container . innerHTML ) . toBe ( '' ) ;
67
65
} ) ;
68
66
69
- it ( 'does not throw an error if a fallback is given ' , ( ) => {
70
- expect ( ( ) => {
71
- render (
72
- < ErrorBoundary fallback = { < h1 > Error Component</ h1 > } >
73
- < h1 > children </ h1 >
74
- </ ErrorBoundary > ,
75
- ) ;
76
- } ) . not . toThrowError ( ) ;
67
+ it ( 'renders a fallback on error ' , ( ) => {
68
+ const { container } = render (
69
+ // @ts -ignore
70
+ < ErrorBoundary fallback = { < h1 > Error Component</ h1 > } >
71
+ < Bam / >
72
+ </ ErrorBoundary > ,
73
+ ) ;
74
+ expect ( container . innerHTML ) . toBe ( '<h1>Error Component</h1>' ) ;
77
75
} ) ;
78
76
79
77
it ( 'calls `onMount` when mounted' , ( ) => {
@@ -102,36 +100,36 @@ describe('ErrorBoundary', () => {
102
100
} ) ;
103
101
104
102
it ( 'renders children correctly when there is no error' , ( ) => {
105
- const { baseElement } = render (
103
+ const { container } = render (
106
104
< ErrorBoundary fallback = { < h1 > Error Component</ h1 > } >
107
105
< h1 > children</ h1 >
108
106
</ ErrorBoundary > ,
109
107
) ;
110
108
111
- expect ( baseElement . outerHTML ) . toContain ( '<h1>children</h1>' ) ;
109
+ expect ( container . innerHTML ) . toBe ( '<h1>children</h1>' ) ;
112
110
} ) ;
113
111
114
112
describe ( 'fallback' , ( ) => {
115
113
it ( 'renders a fallback component' , async ( ) => {
116
- const { baseElement } = render (
114
+ const { container } = render (
117
115
< TestApp fallback = { < p > You have hit an error</ p > } >
118
116
< h1 > children</ h1 >
119
117
</ TestApp > ,
120
118
) ;
121
119
122
- expect ( baseElement . outerHTML ) . toContain ( '<h1>children</h1>' ) ;
120
+ expect ( container . innerHTML ) . toContain ( '<h1>children</h1>' ) ;
123
121
124
122
const btn = screen . getByTestId ( 'errorBtn' ) ;
125
123
fireEvent . click ( btn ) ;
126
124
127
- expect ( baseElement . outerHTML ) . not . toContain ( '<h1>children</h1>' ) ;
128
- expect ( baseElement . outerHTML ) . toContain ( '<p>You have hit an error</p>' ) ;
125
+ expect ( container . innerHTML ) . not . toContain ( '<h1>children</h1>' ) ;
126
+ expect ( container . innerHTML ) . toBe ( '<p>You have hit an error</p>' ) ;
129
127
} ) ;
130
128
131
129
it ( 'renders a fallbackRender component' , async ( ) => {
132
130
let errorString = '' ;
133
131
let compStack = '' ;
134
- const { baseElement } = render (
132
+ const { container } = render (
135
133
< TestApp
136
134
fallbackRender = { ( { error, componentStack } ) => {
137
135
if ( error && componentStack ) {
@@ -145,13 +143,13 @@ describe('ErrorBoundary', () => {
145
143
</ TestApp > ,
146
144
) ;
147
145
148
- expect ( baseElement . outerHTML ) . toContain ( '<h1>children</h1>' ) ;
146
+ expect ( container . innerHTML ) . toContain ( '<h1>children</h1>' ) ;
149
147
150
148
const btn = screen . getByTestId ( 'errorBtn' ) ;
151
149
fireEvent . click ( btn ) ;
152
150
153
- expect ( baseElement . outerHTML ) . not . toContain ( '<h1>children</h1' ) ;
154
- expect ( baseElement . outerHTML ) . toContain ( '<div>Fallback here</div>' ) ;
151
+ expect ( container . innerHTML ) . not . toContain ( '<h1>children</h1' ) ;
152
+ expect ( container . innerHTML ) . toBe ( '<div>Fallback here</div>' ) ;
155
153
156
154
expect ( errorString ) . toBe ( 'Error: boom' ) ;
157
155
expect ( compStack ) . toBe ( `
@@ -181,7 +179,7 @@ describe('ErrorBoundary', () => {
181
179
182
180
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
183
181
expect ( mockCaptureException ) . toHaveBeenCalledWith ( expect . any ( Error ) , {
184
- contexts : { componentStack : expect . any ( String ) } ,
182
+ contexts : { react : { componentStack : expect . any ( String ) } } ,
185
183
} ) ;
186
184
} ) ;
187
185
@@ -204,7 +202,7 @@ describe('ErrorBoundary', () => {
204
202
205
203
it ( 'resets to initial state when reset' , ( ) => {
206
204
const mockOnReset = jest . fn ( ) ;
207
- const { baseElement } = render (
205
+ const { container } = render (
208
206
< TestApp
209
207
onReset = { mockOnReset }
210
208
fallbackRender = { ( { resetError } ) => < button data-testid = "reset" onClick = { resetError } /> }
@@ -213,13 +211,13 @@ describe('ErrorBoundary', () => {
213
211
</ TestApp > ,
214
212
) ;
215
213
216
- expect ( baseElement . outerHTML ) . toContain ( '<h1>children</h1>' ) ;
214
+ expect ( container . innerHTML ) . toContain ( '<h1>children</h1>' ) ;
217
215
expect ( mockOnReset ) . toHaveBeenCalledTimes ( 0 ) ;
218
216
219
217
const btn = screen . getByTestId ( 'errorBtn' ) ;
220
218
fireEvent . click ( btn ) ;
221
219
222
- expect ( baseElement . outerHTML ) . toContain ( '<button data-testid="reset">' ) ;
220
+ expect ( container . innerHTML ) . toContain ( '<button data-testid="reset">' ) ;
223
221
expect ( mockOnReset ) . toHaveBeenCalledTimes ( 0 ) ;
224
222
225
223
const reset = screen . getByTestId ( 'reset' ) ;
0 commit comments