File tree Expand file tree Collapse file tree 3 files changed +45
-8
lines changed
Expand file tree Collapse file tree 3 files changed +45
-8
lines changed Original file line number Diff line number Diff line change 11{
22 "dist/index.js" : {
3- "bundled" : 31764 ,
4- "minified" : 16087 ,
5- "gzipped" : 4618
3+ "bundled" : 31777 ,
4+ "minified" : 16088 ,
5+ "gzipped" : 4619
66 },
77 "dist/index.es.js" : {
8- "bundled" : 31223 ,
9- "minified" : 15604 ,
10- "gzipped" : 4516 ,
8+ "bundled" : 31236 ,
9+ "minified" : 15605 ,
10+ "gzipped" : 4517 ,
1111 "treeshaked" : {
1212 "rollup" : {
1313 "code" : 3329 ,
Original file line number Diff line number Diff line change 55 waitForElement ,
66} from '@testing-library/react'
77import * as React from 'react'
8- import { act } from 'react-dom/test-utils'
98
109import { useMutation } from '../index'
1110
@@ -43,4 +42,39 @@ describe('useMutation', () => {
4342
4443 expect ( getByTestId ( 'title' ) . textContent ) . toBe ( '' )
4544 } )
45+
46+ it ( 'should be able to reset `error`' , async ( ) => {
47+ function Page ( ) {
48+ const [ mutate , mutationResult ] = useMutation (
49+ ( ) => Promise . reject ( new Error ( 'something went wrong' ) ) ,
50+ {
51+ throwOnError : false
52+ }
53+ )
54+
55+ return (
56+ < div >
57+ { mutationResult . error &&
58+ < h1 data-testid = "error" > { mutationResult . error . message } </ h1 >
59+ }
60+ < button onClick = { mutationResult . reset } > reset</ button >
61+ < button onClick = { mutate } > mutate</ button >
62+ </ div >
63+ )
64+ }
65+
66+ const { getByTestId, getByText, queryByTestId } = render ( < Page /> )
67+
68+ expect ( queryByTestId ( 'error' ) ) . toBeNull ( )
69+
70+ fireEvent . click ( getByText ( 'mutate' ) )
71+
72+ await waitForElement ( ( ) => getByTestId ( 'error' ) )
73+
74+ expect ( getByTestId ( 'error' ) . textContent ) . toBe ( 'something went wrong' )
75+
76+ fireEvent . click ( getByText ( 'reset' ) )
77+
78+ expect ( queryByTestId ( 'error' ) ) . toBeNull ( )
79+ } )
4680} )
Original file line number Diff line number Diff line change @@ -697,7 +697,10 @@ export function useMutation(
697697 [ refetchQueries , refetchQueriesOnFailure , throwOnError ]
698698 )
699699
700- const reset = React . useCallback ( ( ) => setData ( null ) , [ ] )
700+ const reset = React . useCallback ( ( ) => {
701+ setData ( null )
702+ setError ( null )
703+ } , [ ] )
701704
702705 React . useEffect ( ( ) => {
703706 if ( useErrorBoundary && error ) {
You can’t perform that action at this time.
0 commit comments