1
+ import util from 'util'
1
2
import * as React from 'react'
2
3
import type {
3
4
UseMutation ,
@@ -2472,7 +2473,11 @@ describe('skip behaviour', () => {
2472
2473
await act ( async ( ) => {
2473
2474
rerender ( [ 1 , { skip : true } ] )
2474
2475
} )
2475
- expect ( result . current ) . toEqual ( uninitialized )
2476
+ expect ( result . current ) . toEqual ( {
2477
+ ...uninitialized ,
2478
+ currentData : undefined ,
2479
+ data : { name : 'Timmy' } ,
2480
+ } )
2476
2481
await delay ( 1 )
2477
2482
expect ( subscriptionCount ( 'getUser(1)' ) ) . toBe ( 0 )
2478
2483
} )
@@ -2489,6 +2494,7 @@ describe('skip behaviour', () => {
2489
2494
2490
2495
expect ( result . current ) . toEqual ( uninitialized )
2491
2496
await delay ( 1 )
2497
+
2492
2498
expect ( subscriptionCount ( 'getUser(1)' ) ) . toBe ( 0 )
2493
2499
// also no subscription on `getUser(skipToken)` or similar:
2494
2500
expect ( storeRef . store . getState ( ) . api . subscriptions ) . toEqual ( { } )
@@ -2504,10 +2510,51 @@ describe('skip behaviour', () => {
2504
2510
await act ( async ( ) => {
2505
2511
rerender ( [ skipToken ] )
2506
2512
} )
2507
- expect ( result . current ) . toEqual ( uninitialized )
2513
+ expect ( result . current ) . toEqual ( {
2514
+ ...uninitialized ,
2515
+ currentData : undefined ,
2516
+ data : { name : 'Timmy' } ,
2517
+ } )
2508
2518
await delay ( 1 )
2509
2519
expect ( subscriptionCount ( 'getUser(1)' ) ) . toBe ( 0 )
2510
2520
} )
2521
+
2522
+ test ( 'skipping a previously fetched query retains the existing value as `data`, but clears `currentData`' , async ( ) => {
2523
+ const { result, rerender } = renderHook (
2524
+ ( [ arg , options ] : Parameters < typeof api . endpoints . getUser . useQuery > ) =>
2525
+ api . endpoints . getUser . useQuery ( arg , options ) ,
2526
+ {
2527
+ wrapper : storeRef . wrapper ,
2528
+ initialProps : [ 1 ] ,
2529
+ }
2530
+ )
2531
+
2532
+ await act ( async ( ) => {
2533
+ await delay ( 1 )
2534
+ } )
2535
+
2536
+ // Normal fulfilled result, with both `data` and `currentData`
2537
+ expect ( result . current ) . toMatchObject ( {
2538
+ status : QueryStatus . fulfilled ,
2539
+ isSuccess : true ,
2540
+ data : { name : 'Timmy' } ,
2541
+ currentData : { name : 'Timmy' } ,
2542
+ } )
2543
+
2544
+ await act ( async ( ) => {
2545
+ rerender ( [ 1 , { skip : true } ] )
2546
+ await delay ( 1 )
2547
+ } )
2548
+
2549
+ // After skipping, the query is "uninitialized", but still retains the last fetched `data`
2550
+ // even though it's skipped. `currentData` is undefined, since that matches the current arg.
2551
+ expect ( result . current ) . toMatchObject ( {
2552
+ status : QueryStatus . uninitialized ,
2553
+ isSuccess : false ,
2554
+ data : { name : 'Timmy' } ,
2555
+ currentData : undefined ,
2556
+ } )
2557
+ } )
2511
2558
} )
2512
2559
2513
2560
// type tests:
0 commit comments