File tree Expand file tree Collapse file tree 3 files changed +54
-3
lines changed Expand file tree Collapse file tree 3 files changed +54
-3
lines changed Original file line number Diff line number Diff line change @@ -659,6 +659,57 @@ describe('useMutation', () => {
659
659
} )
660
660
} )
661
661
662
+ it ( 'should be able to throw an error when useErrorBoundary is a function that returns true' , async ( ) => {
663
+ let boundary = false
664
+ function Page ( ) {
665
+ const { mutate, error } = useMutation < string , Error > (
666
+ ( ) => {
667
+ const err = new Error ( 'mock error' )
668
+ err . stack = ''
669
+ return Promise . reject ( err )
670
+ } ,
671
+ {
672
+ useErrorBoundary : ( ) => {
673
+ boundary = ! boundary
674
+ return ! boundary
675
+ } ,
676
+ }
677
+ )
678
+
679
+ return (
680
+ < div >
681
+ < button onClick = { ( ) => mutate ( ) } > mutate</ button >
682
+ { error && error . message }
683
+ </ div >
684
+ )
685
+ }
686
+
687
+ const { getByText, queryByText } = renderWithClient (
688
+ queryClient ,
689
+ < ErrorBoundary
690
+ fallbackRender = { ( ) => (
691
+ < div >
692
+ < span > error boundary</ span >
693
+ </ div >
694
+ ) }
695
+ >
696
+ < Page />
697
+ </ ErrorBoundary >
698
+ )
699
+
700
+ // first error goes to component
701
+ fireEvent . click ( getByText ( 'mutate' ) )
702
+ await waitFor ( ( ) => {
703
+ expect ( queryByText ( 'mock error' ) ) . not . toBeNull ( )
704
+ } )
705
+
706
+ // second error goes to boundary
707
+ fireEvent . click ( getByText ( 'mutate' ) )
708
+ await waitFor ( ( ) => {
709
+ expect ( queryByText ( 'error boundary' ) ) . not . toBeNull ( )
710
+ } )
711
+ } )
712
+
662
713
it ( 'should pass meta to mutation' , async ( ) => {
663
714
const errorMock = jest . fn ( )
664
715
const successMock = jest . fn ( )
Original file line number Diff line number Diff line change @@ -116,7 +116,7 @@ export function useMutation<
116
116
117
117
if (
118
118
currentResult . error &&
119
- shouldThrowError ( ! ! obsRef . current . options . useErrorBoundary , [
119
+ shouldThrowError ( obsRef . current . options . useErrorBoundary , [
120
120
currentResult . error ,
121
121
] )
122
122
) {
Original file line number Diff line number Diff line change 1
1
export function shouldThrowError < T extends ( ...args : any [ ] ) => boolean > (
2
- _useErrorBoundary : boolean | T ,
2
+ _useErrorBoundary : boolean | T | undefined ,
3
3
params : Parameters < T >
4
4
) : boolean {
5
5
// Allow useErrorBoundary function to override throwing behavior on a per-error basis
6
6
if ( typeof _useErrorBoundary === 'function' ) {
7
7
return _useErrorBoundary ( ...params )
8
8
}
9
9
10
- return _useErrorBoundary
10
+ return ! ! _useErrorBoundary
11
11
}
You can’t perform that action at this time.
0 commit comments