1+ import util from 'util'
12import * as React from 'react'
23import type {
34 UseMutation ,
@@ -2472,7 +2473,11 @@ describe('skip behaviour', () => {
24722473 await act ( async ( ) => {
24732474 rerender ( [ 1 , { skip : true } ] )
24742475 } )
2475- expect ( result . current ) . toEqual ( uninitialized )
2476+ expect ( result . current ) . toEqual ( {
2477+ ...uninitialized ,
2478+ currentData : undefined ,
2479+ data : { name : 'Timmy' } ,
2480+ } )
24762481 await delay ( 1 )
24772482 expect ( subscriptionCount ( 'getUser(1)' ) ) . toBe ( 0 )
24782483 } )
@@ -2489,6 +2494,7 @@ describe('skip behaviour', () => {
24892494
24902495 expect ( result . current ) . toEqual ( uninitialized )
24912496 await delay ( 1 )
2497+
24922498 expect ( subscriptionCount ( 'getUser(1)' ) ) . toBe ( 0 )
24932499 // also no subscription on `getUser(skipToken)` or similar:
24942500 expect ( storeRef . store . getState ( ) . api . subscriptions ) . toEqual ( { } )
@@ -2504,10 +2510,51 @@ describe('skip behaviour', () => {
25042510 await act ( async ( ) => {
25052511 rerender ( [ skipToken ] )
25062512 } )
2507- expect ( result . current ) . toEqual ( uninitialized )
2513+ expect ( result . current ) . toEqual ( {
2514+ ...uninitialized ,
2515+ currentData : undefined ,
2516+ data : { name : 'Timmy' } ,
2517+ } )
25082518 await delay ( 1 )
25092519 expect ( subscriptionCount ( 'getUser(1)' ) ) . toBe ( 0 )
25102520 } )
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+ } )
25112558} )
25122559
25132560// type tests:
0 commit comments