File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import ErrorPage from './ErrorPage';
11
11
* @memberof module:React
12
12
* @extends {Component }
13
13
*/
14
- export default class ErrorBoundary extends Component {
14
+ class ErrorBoundary extends Component {
15
15
constructor ( props ) {
16
16
super ( props ) ;
17
17
this . state = { hasError : false } ;
@@ -28,17 +28,20 @@ export default class ErrorBoundary extends Component {
28
28
29
29
render ( ) {
30
30
if ( this . state . hasError ) {
31
- return < ErrorPage /> ;
31
+ return this . props . fallbackComponent || < ErrorPage /> ;
32
32
}
33
-
34
33
return this . props . children ;
35
34
}
36
35
}
37
36
38
37
ErrorBoundary . propTypes = {
39
38
children : PropTypes . node ,
39
+ fallbackComponent : PropTypes . node ,
40
40
} ;
41
41
42
42
ErrorBoundary . defaultProps = {
43
43
children : null ,
44
+ fallbackComponent : undefined ,
44
45
} ;
46
+
47
+ export default ErrorBoundary ;
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import React from 'react';
2
2
import { mount } from 'enzyme' ;
3
3
4
4
import ErrorBoundary from './ErrorBoundary' ;
5
+ import ErrorPage from './ErrorPage' ;
5
6
import { initializeMockApp } from '..' ;
6
7
7
8
describe ( 'ErrorBoundary' , ( ) => {
@@ -54,4 +55,30 @@ describe('ErrorBoundary', () => {
54
55
} ) ,
55
56
) ;
56
57
} ) ;
58
+ it ( 'should render the fallback component when an error occurs' , ( ) => {
59
+ function FallbackComponent ( ) {
60
+ return < div > Oops, something went wrong!</ div > ;
61
+ }
62
+ function ComponentError ( ) {
63
+ throw new Error ( 'An error occurred during the click event!' ) ;
64
+ }
65
+ const wrapper = mount (
66
+ < ErrorBoundary fallbackComponent = { < FallbackComponent /> } >
67
+ < ComponentError />
68
+ </ ErrorBoundary > ,
69
+ ) ;
70
+ expect ( wrapper . contains ( < FallbackComponent /> ) ) . toBe ( true ) ;
71
+ } ) ;
72
+
73
+ it ( 'should render the ErrorPage fallbackComponent is null' , ( ) => {
74
+ function ComponentError ( ) {
75
+ throw new Error ( 'An error occurred during the click event!' ) ;
76
+ }
77
+ const wrapper = mount (
78
+ < ErrorBoundary fallbackComponent = { null } >
79
+ < ComponentError />
80
+ </ ErrorBoundary > ,
81
+ ) ;
82
+ expect ( wrapper . contains ( < ErrorPage /> ) ) . toBe ( true ) ;
83
+ } ) ;
57
84
} ) ;
You can’t perform that action at this time.
0 commit comments