File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed
Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -127,4 +127,32 @@ describe('useAsync', () => {
127127 expect ( result . current . error ) . toBeNull ( ) ;
128128 expect ( queryFn ) . not . toHaveBeenCalled ( ) ;
129129 } ) ;
130+
131+ it ( 'should clear states on enabled change to false' , async ( ) => {
132+ const queryFn = jest . fn ( ) . mockResolvedValue ( 'data' ) ;
133+
134+ const { result, rerender } = renderHook (
135+ ( { enabled } ) =>
136+ useAsync ( {
137+ queryKey : 'test7' ,
138+ queryFn,
139+ enabled,
140+ } ) ,
141+ {
142+ initialProps : { enabled : true } ,
143+ }
144+ ) ;
145+
146+ await waitFor ( ( ) => {
147+ expect ( result . current . isLoading ) . toBe ( false ) ;
148+ } ) ;
149+
150+ expect ( result . current . data ) . toBe ( 'data' ) ;
151+
152+ rerender ( { enabled : false } ) ;
153+
154+ expect ( result . current . data ) . toBeNull ( ) ;
155+ expect ( result . current . isLoading ) . toBe ( false ) ;
156+ expect ( result . current . error ) . toBeNull ( ) ;
157+ } ) ;
130158} ) ;
Original file line number Diff line number Diff line change @@ -60,7 +60,14 @@ export function useAsync<T>({
6060 } , [ ] ) ;
6161
6262 useEffect ( ( ) => {
63- if ( ! enabled ) return ;
63+ if ( ! enabled ) {
64+ setState ( {
65+ data : null ,
66+ isLoading : false ,
67+ error : null ,
68+ } ) ;
69+ return ;
70+ }
6471
6572 if ( ! disableCache && cache . has ( cacheKey ) ) {
6673 setState ( prev => ( {
You can’t perform that action at this time.
0 commit comments