Skip to content

Commit

Permalink
Handle skipToken in queryStatePreSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Dec 11, 2024
1 parent 40b8aed commit 69d1e62
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,16 +905,17 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
const { endpointName } = lastResult
const endpointDefinition = context.endpointDefinitions[endpointName]
if (
queryArgs !== skipToken &&
serializeQueryArgs({
queryArgs: lastResult.originalArgs,
endpointDefinition,
endpointName,
}) ===
serializeQueryArgs({
queryArgs,
endpointDefinition,
endpointName,
})
serializeQueryArgs({
queryArgs,
endpointDefinition,
endpointName,
})
)
lastResult = undefined
}
Expand Down
42 changes: 42 additions & 0 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,48 @@ describe('skip behavior', () => {
expect(getSubscriptionCount('getUser(1)')).toBe(0)
})

test('skipToken does not break serializeQueryArgs', async () => {
const { result, rerender } = renderHook(
([arg, options]: Parameters<
typeof api.endpoints.queryWithDeepArg.useQuery
>) => api.endpoints.queryWithDeepArg.useQuery(arg, options),
{
wrapper: storeRef.wrapper,
initialProps: [skipToken],
},
)

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

expect(getSubscriptionCount('nestedValue')).toBe(0)
// also no subscription on `getUser(skipToken)` or similar:
expect(getSubscriptions()).toEqual({})

rerender([{ param: { nested: 'nestedValue' } }])

await act(async () => {
await waitForFakeTimer(150)
})

expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
await waitMs(1)

expect(getSubscriptionCount('nestedValue')).toBe(1)
expect(getSubscriptions()).not.toEqual({})

rerender([skipToken])

expect(result.current).toEqual({
...uninitialized,
isSuccess: true,
currentData: undefined,
data: {},
})
await waitMs(1)
expect(getSubscriptionCount('nestedValue')).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>) =>
Expand Down

0 comments on commit 69d1e62

Please sign in to comment.