diff --git a/packages/toolkit/src/query/react/buildHooks.ts b/packages/toolkit/src/query/react/buildHooks.ts index daec42efe2..63aadd94e1 100644 --- a/packages/toolkit/src/query/react/buildHooks.ts +++ b/packages/toolkit/src/query/react/buildHooks.ts @@ -905,16 +905,17 @@ export function buildHooks({ 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 } diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx index d17184abb1..e3a0989300 100644 --- a/packages/toolkit/src/query/tests/buildHooks.test.tsx +++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx @@ -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) =>