11import { fireEvent , render , screen } from '@testing-library/react' ;
22import * as React from 'react' ;
33
4- import { ErrorBoundary , ErrorBoundaryProps } from '../src/errorboundary' ;
4+ import { ErrorBoundary , ErrorBoundaryProps , UNKNOWN_COMPONENT , withErrorBoundary } from '../src/errorboundary' ;
55
66const mockCaptureException = jest . fn ( ) ;
77const mockShowReportDialog = jest . fn ( ) ;
8+ const EVENT_ID = 'test-id-123' ;
89
910jest . mock ( '@sentry/browser' , ( ) => ( {
1011 captureException : ( err : any , ctx : any ) => {
1112 mockCaptureException ( err , ctx ) ;
13+ return EVENT_ID ;
1214 } ,
1315 showReportDialog : ( options : any ) => {
1416 mockShowReportDialog ( options ) ;
@@ -18,7 +20,15 @@ jest.mock('@sentry/browser', () => ({
1820const TestApp : React . FC < ErrorBoundaryProps > = ( { children, ...props } ) => {
1921 const [ isError , setError ] = React . useState ( false ) ;
2022 return (
21- < ErrorBoundary { ...props } >
23+ < ErrorBoundary
24+ { ...props }
25+ onReset = { ( err : Error , stack : string ) => {
26+ setError ( false ) ;
27+ if ( props . onReset ) {
28+ props . onReset ( err , stack ) ;
29+ }
30+ } }
31+ >
2232 { isError ? < Bam /> : children }
2333 < button
2434 data-testid = "errorBtn"
@@ -34,6 +44,20 @@ function Bam(): JSX.Element {
3444 throw new Error ( 'boom' ) ;
3545}
3646
47+ describe ( 'withErrorBoundary' , ( ) => {
48+ it ( 'sets displayName properly' , ( ) => {
49+ const TestComponent = ( ) => < h1 > Hello World</ h1 > ;
50+
51+ const Component = withErrorBoundary ( TestComponent , { fallback : < h1 > fallback</ h1 > } ) ;
52+ expect ( Component . displayName ) . toBe ( 'errorBoundary(TestComponent)' ) ;
53+ } ) ;
54+
55+ it ( 'defaults to an unknown displayName' , ( ) => {
56+ const Component = withErrorBoundary ( ( ) => < h1 > Hello World</ h1 > , { fallback : < h1 > fallback</ h1 > } ) ;
57+ expect ( Component . displayName ) . toBe ( `errorBoundary(${ UNKNOWN_COMPONENT } )` ) ;
58+ } ) ;
59+ } ) ;
60+
3761describe ( 'ErrorBoundary' , ( ) => {
3862 jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
3963
@@ -44,7 +68,6 @@ describe('ErrorBoundary', () => {
4468
4569 it ( 'renders null if not given a valid `fallback` prop' , ( ) => {
4670 const { container } = render (
47- // @ts -ignore
4871 < ErrorBoundary fallback = { new Error ( 'true' ) } >
4972 < Bam />
5073 </ ErrorBoundary > ,
@@ -55,7 +78,6 @@ describe('ErrorBoundary', () => {
5578
5679 it ( 'renders a fallback on error' , ( ) => {
5780 const { container } = render (
58- // @ts -ignore
5981 < ErrorBoundary fallback = { < h1 > Error Component</ h1 > } >
6082 < Bam />
6183 </ ErrorBoundary > ,
@@ -186,12 +208,30 @@ describe('ErrorBoundary', () => {
186208 fireEvent . click ( btn ) ;
187209
188210 expect ( mockShowReportDialog ) . toHaveBeenCalledTimes ( 1 ) ;
189- expect ( mockShowReportDialog ) . toHaveBeenCalledWith ( options ) ;
211+ expect ( mockShowReportDialog ) . toHaveBeenCalledWith ( { ... options , eventId : EVENT_ID } ) ;
190212 } ) ;
191213
192- it ( 'resets to initial state when reset' , ( ) => {
193- const mockOnReset = jest . fn ( ) ;
214+ it ( 'resets to initial state when reset' , async ( ) => {
194215 const { container } = render (
216+ < TestApp fallback = { ( { resetError } ) => < button data-testid = "reset" onClick = { resetError } /> } >
217+ < h1 > children</ h1 >
218+ </ TestApp > ,
219+ ) ;
220+
221+ expect ( container . innerHTML ) . toContain ( '<h1>children</h1>' ) ;
222+ const btn = screen . getByTestId ( 'errorBtn' ) ;
223+ fireEvent . click ( btn ) ;
224+ expect ( container . innerHTML ) . toContain ( '<button data-testid="reset">' ) ;
225+
226+ const reset = screen . getByTestId ( 'reset' ) ;
227+ fireEvent . click ( reset ) ;
228+
229+ expect ( container . innerHTML ) . toContain ( '<h1>children</h1>' ) ;
230+ } ) ;
231+
232+ it ( 'calls `onReset()` when reset' , ( ) => {
233+ const mockOnReset = jest . fn ( ) ;
234+ render (
195235 < TestApp
196236 onReset = { mockOnReset }
197237 fallback = { ( { resetError } ) => < button data-testid = "reset" onClick = { resetError } /> }
@@ -200,17 +240,14 @@ describe('ErrorBoundary', () => {
200240 </ TestApp > ,
201241 ) ;
202242
203- expect ( container . innerHTML ) . toContain ( '<h1>children</h1>' ) ;
204243 expect ( mockOnReset ) . toHaveBeenCalledTimes ( 0 ) ;
205-
206244 const btn = screen . getByTestId ( 'errorBtn' ) ;
207245 fireEvent . click ( btn ) ;
208-
209- expect ( container . innerHTML ) . toContain ( '<button data-testid="reset">' ) ;
210246 expect ( mockOnReset ) . toHaveBeenCalledTimes ( 0 ) ;
211247
212248 const reset = screen . getByTestId ( 'reset' ) ;
213249 fireEvent . click ( reset ) ;
250+
214251 expect ( mockOnReset ) . toHaveBeenCalledTimes ( 1 ) ;
215252 expect ( mockOnReset ) . toHaveBeenCalledWith ( expect . any ( Error ) , expect . any ( String ) ) ;
216253 } ) ;
0 commit comments