Skip to content

Commit

Permalink
test: hydration test case for overwriting with promise
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo committed Oct 9, 2024
1 parent eb2f9d3 commit 05ccd65
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/query-core/src/__tests__/hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

0 comments on commit 05ccd65

Please sign in to comment.