@@ -4,7 +4,7 @@ import { fireEvent, render, screen } from '@testing-library/react';
44import * as React from 'react' ;
55import { useState } from 'react' ;
66
7- import type { ErrorBoundaryProps } from '../src/errorboundary' ;
7+ import type { ErrorBoundaryProps , FallbackRender } from '../src/errorboundary' ;
88import { ErrorBoundary , UNKNOWN_COMPONENT , withErrorBoundary } from '../src/errorboundary' ;
99
1010const mockCaptureException = jest . fn ( ) ;
@@ -537,47 +537,47 @@ describe('ErrorBoundary', () => {
537537 expect ( mockOnReset ) . toHaveBeenCalledTimes ( 1 ) ;
538538 expect ( mockOnReset ) . toHaveBeenCalledWith ( expect . any ( Error ) , expect . any ( String ) , expect . any ( String ) ) ;
539539 } ) ;
540+ it . each `
541+ fallback | handled | expected
542+ ${ true } | ${ undefined } | ${ true }
543+ ${ false } | ${ undefined } | ${ false }
544+ ${ true } | ${ false } | ${ false }
545+ ${ true } | ${ true } | ${ true }
546+ ${ false } | ${ true } | ${ true }
547+ ${ false } | ${ false } | ${ false }
548+ ` (
549+ 'sets `handled: $expected` when `handled` is $handled and `fallback` is $fallback' ,
550+ async ( {
551+ fallback,
552+ handled,
553+ expected,
554+ } : {
555+ fallback : boolean ;
556+ handled : boolean | undefined ;
557+ expected : boolean ;
558+ } ) => {
559+ const fallbackComponent : FallbackRender | undefined = fallback
560+ ? ( { resetError } ) => < button data-testid = "reset" onClick = { resetError } />
561+ : undefined ;
562+ render (
563+ < TestApp handled = { handled } fallback = { fallbackComponent } >
564+ < h1 > children</ h1 >
565+ </ TestApp > ,
566+ ) ;
540567
541- it ( 'sets `handled: true` when a fallback is provided' , async ( ) => {
542- render (
543- < TestApp fallback = { ( { resetError } ) => < button data-testid = "reset" onClick = { resetError } /> } >
544- < h1 > children</ h1 >
545- </ TestApp > ,
546- ) ;
547-
548- expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
549-
550- const btn = screen . getByTestId ( 'errorBtn' ) ;
551- fireEvent . click ( btn ) ;
552-
553- expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
554- expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Object ) , {
555- captureContext : {
556- contexts : { react : { componentStack : expect . any ( String ) } } ,
557- } ,
558- mechanism : { handled : true } ,
559- } ) ;
560- } ) ;
561-
562- it ( 'sets `handled: false` when no fallback is provided' , async ( ) => {
563- render (
564- < TestApp >
565- < h1 > children</ h1 >
566- </ TestApp > ,
567- ) ;
568-
569- expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
570-
571- const btn = screen . getByTestId ( 'errorBtn' ) ;
572- fireEvent . click ( btn ) ;
568+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 0 ) ;
573569
574- expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
575- expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Object ) , {
576- captureContext : {
577- contexts : { react : { componentStack : expect . any ( String ) } } ,
578- } ,
579- mechanism : { handled : false } ,
580- } ) ;
581- } ) ;
570+ const btn = screen . getByTestId ( 'errorBtn' ) ;
571+ fireEvent . click ( btn ) ;
572+
573+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
574+ expect ( mockCaptureException ) . toHaveBeenLastCalledWith ( expect . any ( Object ) , {
575+ captureContext : {
576+ contexts : { react : { componentStack : expect . any ( String ) } } ,
577+ } ,
578+ mechanism : { handled : expected } ,
579+ } ) ;
580+ } ,
581+ ) ;
582582 } ) ;
583583} ) ;
0 commit comments