From 05ccd65795304ff1f20daf0311e42f8d37fc93c4 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister <office@dorfmeister.cc> Date: Wed, 9 Oct 2024 14:36:25 +0200 Subject: [PATCH] test: hydration test case for overwriting with promise --- .../src/__tests__/hydration.test.tsx | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/query-core/src/__tests__/hydration.test.tsx b/packages/query-core/src/__tests__/hydration.test.tsx index 0f2abb17eb..f3ebc6969c 100644 --- a/packages/query-core/src/__tests__/hydration.test.tsx +++ b/packages/query-core/src/__tests__/hydration.test.tsx @@ -1028,4 +1028,42 @@ describe('dehydration and rehydration', () => { queryClient.clear() hydrationClient.clear() }) + + test('should overwrite query in cache if hydrated query is newer (with promise)', async () => { + // --- server --- + + const serverQueryClient = createQueryClient({ + defaultOptions: { + dehydrate: { + shouldDehydrateQuery: () => true, + }, + }, + }) + + const promise = serverQueryClient.prefetchQuery({ + queryKey: ['data'], + queryFn: async () => { + await sleep(10) + return 'server data' + }, + }) + + const dehydrated = dehydrate(serverQueryClient) + + // --- client --- + + const clientQueryClient = createQueryClient() + + clientQueryClient.setQueryData(['data'], 'old data', { updatedAt: 10 }) + + hydrate(clientQueryClient, dehydrated) + + await promise + await waitFor(() => + expect(clientQueryClient.getQueryData(['data'])).toBe('server data'), + ) + + clientQueryClient.clear() + serverQueryClient.clear() + }) })