@@ -126,10 +126,10 @@ const api = createApi({
126
126
} ,
127
127
} ) ,
128
128
queryWithDeepArg : build . query < string , { param : { nested : string } } > ( {
129
- query : ( { param : { nested } } ) => nested ,
129
+ query : ( { param : { nested } } ) => nested ,
130
130
serializeQueryArgs : ( { queryArgs } ) => {
131
131
return queryArgs . param . nested
132
- }
132
+ } ,
133
133
} ) ,
134
134
} ) ,
135
135
} )
@@ -383,6 +383,54 @@ describe('hooks tests', () => {
383
383
expect ( fetchingHist ) . toEqual ( [ true , false , true , false ] )
384
384
} )
385
385
386
+ test ( '`isSuccess` does not jump back false on subsequent queries' , async ( ) => {
387
+ type LoadingState = {
388
+ id : number
389
+ isFetching : boolean
390
+ isSuccess : boolean
391
+ }
392
+ const loadingHistory : LoadingState [ ] = [ ]
393
+
394
+ function User ( { id } : { id : number } ) {
395
+ const queryRes = api . endpoints . getUser . useQuery ( id )
396
+
397
+ useEffect ( ( ) => {
398
+ const { isFetching, isSuccess } = queryRes
399
+ loadingHistory . push ( { id, isFetching, isSuccess } )
400
+ } , [ id , queryRes ] )
401
+ return (
402
+ < div data-testid = "status" >
403
+ { queryRes . status === QueryStatus . fulfilled && id }
404
+ </ div >
405
+ )
406
+ }
407
+
408
+ let { rerender } = render ( < User id = { 1 } /> , { wrapper : storeRef . wrapper } )
409
+
410
+ await waitFor ( ( ) =>
411
+ expect ( screen . getByTestId ( 'status' ) . textContent ) . toBe ( '1' ) ,
412
+ )
413
+ rerender ( < User id = { 2 } /> )
414
+
415
+ await waitFor ( ( ) =>
416
+ expect ( screen . getByTestId ( 'status' ) . textContent ) . toBe ( '2' ) ,
417
+ )
418
+
419
+ expect ( loadingHistory ) . toEqual ( [
420
+ // Initial render(s)
421
+ { id : 1 , isFetching : true , isSuccess : false } ,
422
+ { id : 1 , isFetching : true , isSuccess : false } ,
423
+ // Data returned
424
+ { id : 1 , isFetching : false , isSuccess : true } ,
425
+ // ID changed, there's an uninitialized cache entry.
426
+ // IMPORTANT: `isSuccess` should not be false here.
427
+ // We have valid data already for the old item.
428
+ { id : 2 , isFetching : true , isSuccess : true } ,
429
+ { id : 2 , isFetching : true , isSuccess : true } ,
430
+ { id : 2 , isFetching : false , isSuccess : true } ,
431
+ ] )
432
+ } )
433
+
386
434
test ( 'useQuery hook respects refetchOnMountOrArgChange: true' , async ( ) => {
387
435
let data , isLoading , isFetching
388
436
function User ( ) {
@@ -674,9 +722,11 @@ describe('hooks tests', () => {
674
722
} )
675
723
676
724
test ( `useQuery shouldn't call args serialization if request skipped` , async ( ) => {
677
- expect ( ( ) => renderHook ( ( ) => api . endpoints . queryWithDeepArg . useQuery ( skipToken ) , {
678
- wrapper : storeRef . wrapper ,
679
- } ) ) . not . toThrow ( )
725
+ expect ( ( ) =>
726
+ renderHook ( ( ) => api . endpoints . queryWithDeepArg . useQuery ( skipToken ) , {
727
+ wrapper : storeRef . wrapper ,
728
+ } ) ,
729
+ ) . not . toThrow ( )
680
730
} )
681
731
682
732
test ( `useQuery gracefully handles bigint types` , async ( ) => {
@@ -2861,6 +2911,7 @@ describe('skip behavior', () => {
2861
2911
} )
2862
2912
expect ( result . current ) . toEqual ( {
2863
2913
...uninitialized ,
2914
+ isSuccess : true ,
2864
2915
currentData : undefined ,
2865
2916
data : { name : 'Timmy' } ,
2866
2917
} )
@@ -2898,6 +2949,7 @@ describe('skip behavior', () => {
2898
2949
} )
2899
2950
expect ( result . current ) . toEqual ( {
2900
2951
...uninitialized ,
2952
+ isSuccess : true ,
2901
2953
currentData : undefined ,
2902
2954
data : { name : 'Timmy' } ,
2903
2955
} )
@@ -2936,7 +2988,7 @@ describe('skip behavior', () => {
2936
2988
// even though it's skipped. `currentData` is undefined, since that matches the current arg.
2937
2989
expect ( result . current ) . toMatchObject ( {
2938
2990
status : QueryStatus . uninitialized ,
2939
- isSuccess : false ,
2991
+ isSuccess : true ,
2940
2992
data : { name : 'Timmy' } ,
2941
2993
currentData : undefined ,
2942
2994
} )
0 commit comments