1
1
/* tslint:disable:jsx-wrap-multiline */
2
- import * as Sentry from '@sentry/react' ;
3
2
import { mount } from 'enzyme' ;
4
3
import * as React from 'react' ;
5
4
5
+ import { GenericReactErrorBoundaryError } from '../error' ;
6
6
import { ErrorBoundary } from '../ErrorBoundary' ;
7
7
import { ErrorBoundaryProvider } from '../ErrorBoundaryProvider' ;
8
8
import { FallbackComponent } from '../FallbackComponent' ;
@@ -20,12 +20,22 @@ describe('ErrorBoundary component', () => {
20
20
return < span > { String ( value ) } </ span > ;
21
21
} ;
22
22
23
+ let reporter : { error : jest . MockedFunction < typeof console . error > } ;
24
+
25
+ beforeEach ( ( ) => {
26
+ reporter = {
27
+ error : jest . fn ( ) ,
28
+ } ;
29
+ } ) ;
30
+
23
31
describe ( 'when exception is not thrown' , ( ) => {
24
32
it ( 'renders children' , ( ) => {
25
33
const wrapper = mount (
26
- < ErrorBoundary >
27
- < TestComponent value = "test" />
28
- </ ErrorBoundary > ,
34
+ < ErrorBoundaryProvider reporter = { reporter } >
35
+ < ErrorBoundary >
36
+ < TestComponent value = "test" />
37
+ </ ErrorBoundary >
38
+ </ ErrorBoundaryProvider > ,
29
39
) ;
30
40
31
41
expect ( wrapper . find ( TestComponent ) ) . toHaveHTML ( '<span>test</span>' ) ;
@@ -37,32 +47,69 @@ describe('ErrorBoundary component', () => {
37
47
describe ( 'when exception is thrown' , ( ) => {
38
48
it ( 'renders fallback component and passes error-related props' , ( ) => {
39
49
const wrapper = mount (
40
- < ErrorBoundary >
41
- < TestComponent value = { 0 } />
42
- </ ErrorBoundary > ,
50
+ < ErrorBoundaryProvider reporter = { reporter } >
51
+ < ErrorBoundary >
52
+ < TestComponent value = { 0 } />
53
+ </ ErrorBoundary >
54
+ </ ErrorBoundaryProvider > ,
43
55
) ;
44
56
45
57
expect ( wrapper . find ( FallbackComponent ) ) . toExist ( ) ;
46
58
expect ( wrapper . find ( FallbackComponent ) ) . toHaveProp ( {
47
59
error : ex ,
48
60
componentStack : expect . stringContaining ( 'in TestComponent' ) ,
49
- tryRecovering : ( wrapper . find ( Sentry . ErrorBoundary ) . instance ( ) as any ) . resetErrorBoundary ,
61
+ tryRecovering : ( wrapper . find ( ErrorBoundary ) . instance ( ) as any ) . recover ,
50
62
} ) ;
51
63
52
64
wrapper . unmount ( ) ;
53
65
} ) ;
54
66
55
- it ( 'calls onError prop' , ( ) => {
56
- const onError = jest . fn ( ) ;
57
- const wrapper = mount (
58
- < ErrorBoundary onError = { onError } >
59
- < TestComponent value = { 0 } />
60
- </ ErrorBoundary > ,
61
- ) ;
67
+ describe ( 'error reporting' , ( ) => {
68
+ it ( 'calls onError prop' , ( ) => {
69
+ const onError = jest . fn ( ) ;
70
+ const wrapper = mount (
71
+ < ErrorBoundaryProvider reporter = { reporter } >
72
+ < ErrorBoundary onError = { onError } >
73
+ < TestComponent value = { 0 } />
74
+ </ ErrorBoundary >
75
+ </ ErrorBoundaryProvider > ,
76
+ ) ;
62
77
63
- expect ( onError ) . toBeCalledWith ( ex , expect . stringContaining ( 'in TestComponent' ) , expect . any ( String ) ) ;
78
+ expect ( onError ) . toBeCalledWith ( new GenericReactErrorBoundaryError ( ex , null ) ) ;
64
79
65
- wrapper . unmount ( ) ;
80
+ expect ( onError . mock . calls [ 0 ] [ 0 ] ) . toHaveProperty ( 'componentStack' , expect . stringContaining ( 'in TestComponent' ) ) ;
81
+
82
+ wrapper . unmount ( ) ;
83
+ } ) ;
84
+
85
+ it ( 'reports errors by default' , ( ) => {
86
+ const wrapper = mount (
87
+ < ErrorBoundaryProvider reporter = { reporter } >
88
+ < ErrorBoundary >
89
+ < TestComponent value = { 0 } />
90
+ </ ErrorBoundary >
91
+ </ ErrorBoundaryProvider > ,
92
+ ) ;
93
+
94
+ expect ( reporter . error ) . toBeCalledWith ( new GenericReactErrorBoundaryError ( ex , null ) ) ;
95
+ expect ( reporter . error . mock . calls [ 0 ] [ 0 ] ) . toHaveProperty ( 'componentStack' , expect . any ( String ) ) ;
96
+
97
+ wrapper . unmount ( ) ;
98
+ } ) ;
99
+
100
+ it ( 'does reports error if reporting is disabled' , ( ) => {
101
+ const wrapper = mount (
102
+ < ErrorBoundaryProvider reporter = { reporter } >
103
+ < ErrorBoundary reportErrors = { false } >
104
+ < TestComponent value = { 0 } />
105
+ </ ErrorBoundary >
106
+ </ ErrorBoundaryProvider > ,
107
+ ) ;
108
+
109
+ expect ( reporter . error ) . not . toBeCalled ( ) ;
110
+
111
+ wrapper . unmount ( ) ;
112
+ } ) ;
66
113
} ) ;
67
114
68
115
describe ( 'and a custom fallback component is provided' , ( ) => {
@@ -71,17 +118,19 @@ describe('ErrorBoundary component', () => {
71
118
const CustomFallbackComponent = ( ) => < div > foo</ div > ;
72
119
73
120
const wrapper = mount (
74
- < ErrorBoundary FallbackComponent = { CustomFallbackComponent } >
75
- < TestComponent value = { 0 } />
76
- </ ErrorBoundary > ,
121
+ < ErrorBoundaryProvider reporter = { reporter } >
122
+ < ErrorBoundary FallbackComponent = { CustomFallbackComponent } >
123
+ < TestComponent value = { 0 } />
124
+ </ ErrorBoundary >
125
+ </ ErrorBoundaryProvider > ,
77
126
) ;
78
127
79
128
expect ( wrapper . find ( FallbackComponent ) ) . not . toExist ( ) ; // makes sure we don't render the original one
80
129
expect ( wrapper . find ( CustomFallbackComponent ) ) . toExist ( ) ;
81
130
expect ( wrapper . find ( CustomFallbackComponent ) ) . toHaveProp ( {
82
131
error : ex ,
83
132
componentStack : expect . stringContaining ( 'in TestComponent' ) ,
84
- tryRecovering : ( wrapper . find ( Sentry . ErrorBoundary ) . instance ( ) as any ) . resetErrorBoundary ,
133
+ tryRecovering : ( wrapper . find ( ErrorBoundary ) . instance ( ) as any ) . recover ,
85
134
} ) ;
86
135
87
136
wrapper . unmount ( ) ;
@@ -93,7 +142,7 @@ describe('ErrorBoundary component', () => {
93
142
const CustomFallbackComponent = ( ) => < div > foo</ div > ;
94
143
95
144
const wrapper = mount (
96
- < ErrorBoundaryProvider FallbackComponent = { CustomFallbackComponent } >
145
+ < ErrorBoundaryProvider reporter = { reporter } FallbackComponent = { CustomFallbackComponent } >
97
146
>
98
147
< ErrorBoundary >
99
148
< TestComponent value = { 0 } />
@@ -106,7 +155,7 @@ describe('ErrorBoundary component', () => {
106
155
expect ( wrapper . find ( CustomFallbackComponent ) ) . toHaveProp ( {
107
156
error : ex ,
108
157
componentStack : expect . stringContaining ( 'in TestComponent' ) ,
109
- tryRecovering : ( wrapper . find ( Sentry . ErrorBoundary ) . instance ( ) as any ) . resetErrorBoundary ,
158
+ tryRecovering : ( wrapper . find ( ErrorBoundary ) . instance ( ) as any ) . recover ,
110
159
} ) ;
111
160
112
161
wrapper . unmount ( ) ;
@@ -119,7 +168,7 @@ describe('ErrorBoundary component', () => {
119
168
const CustomFallbackPropsComponent = ( ) => < div > level!</ div > ;
120
169
121
170
const wrapper = mount (
122
- < ErrorBoundaryProvider FallbackComponent = { CustomFallbackContextComponent } >
171
+ < ErrorBoundaryProvider reporter = { reporter } FallbackComponent = { CustomFallbackContextComponent } >
123
172
< ErrorBoundary FallbackComponent = { CustomFallbackPropsComponent } >
124
173
< TestComponent value = { 0 } />
125
174
</ ErrorBoundary >
@@ -139,9 +188,11 @@ describe('ErrorBoundary component', () => {
139
188
const getValue = jest . fn ( ) . mockReturnValue ( 0 ) ;
140
189
141
190
const wrapper = mount (
142
- < ErrorBoundary FallbackComponent = { CustomFallbackComponent } >
143
- < TestComponent getValue = { getValue } />
144
- </ ErrorBoundary > ,
191
+ < ErrorBoundaryProvider reporter = { reporter } >
192
+ < ErrorBoundary FallbackComponent = { CustomFallbackComponent } >
193
+ < TestComponent getValue = { getValue } />
194
+ </ ErrorBoundary >
195
+ </ ErrorBoundaryProvider > ,
145
196
) ;
146
197
147
198
expect ( wrapper . find ( CustomFallbackComponent ) ) . toExist ( ) ;
0 commit comments