Skip to content

Commit

Permalink
Revert "clear data on skip" back to its original behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Feb 20, 2023
1 parent 43d94a0 commit 140ca1a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
4 changes: 1 addition & 3 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
)
lastResult = undefined
}
if (queryArgs === skipToken) {
lastResult = undefined
}

// data is the last known good request result we have tracked - or if none has been tracked yet the last good result for the current args
let data = currentState.isSuccess ? currentState.data : lastResult?.data
if (data === undefined) data = currentState.data
Expand Down
51 changes: 49 additions & 2 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import util from 'util'
import * as React from 'react'
import type {
UseMutation,
Expand Down Expand Up @@ -2472,7 +2473,11 @@ describe('skip behaviour', () => {
await act(async () => {
rerender([1, { skip: true }])
})
expect(result.current).toEqual(uninitialized)
expect(result.current).toEqual({
...uninitialized,
currentData: undefined,
data: { name: 'Timmy' },
})
await delay(1)
expect(subscriptionCount('getUser(1)')).toBe(0)
})
Expand All @@ -2489,6 +2494,7 @@ describe('skip behaviour', () => {

expect(result.current).toEqual(uninitialized)
await delay(1)

expect(subscriptionCount('getUser(1)')).toBe(0)
// also no subscription on `getUser(skipToken)` or similar:
expect(storeRef.store.getState().api.subscriptions).toEqual({})
Expand All @@ -2504,10 +2510,51 @@ describe('skip behaviour', () => {
await act(async () => {
rerender([skipToken])
})
expect(result.current).toEqual(uninitialized)
expect(result.current).toEqual({
...uninitialized,
currentData: undefined,
data: { name: 'Timmy' },
})
await delay(1)
expect(subscriptionCount('getUser(1)')).toBe(0)
})

test('skipping a previously fetched query retains the existing value as `data`, but clears `currentData`', async () => {
const { result, rerender } = renderHook(
([arg, options]: Parameters<typeof api.endpoints.getUser.useQuery>) =>
api.endpoints.getUser.useQuery(arg, options),
{
wrapper: storeRef.wrapper,
initialProps: [1],
}
)

await act(async () => {
await delay(1)
})

// Normal fulfilled result, with both `data` and `currentData`
expect(result.current).toMatchObject({
status: QueryStatus.fulfilled,
isSuccess: true,
data: { name: 'Timmy' },
currentData: { name: 'Timmy' },
})

await act(async () => {
rerender([1, { skip: true }])
await delay(1)
})

// After skipping, the query is "uninitialized", but still retains the last fetched `data`
// even though it's skipped. `currentData` is undefined, since that matches the current arg.
expect(result.current).toMatchObject({
status: QueryStatus.uninitialized,
isSuccess: false,
data: { name: 'Timmy' },
currentData: undefined,
})
})
})

// type tests:
Expand Down

0 comments on commit 140ca1a

Please sign in to comment.