Skip to content

test(react-query/HydrationBoundary): use precise time in 'advanceTimersByTimeAsync', add 'expect' using 'toBeInTheDocument', and simplify 'queryFn' #9472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions packages/react-query/src/__tests__/HydrationBoundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ describe('React hydration', () => {
</HydrationBoundary>
</QueryClientProvider>,
)
await vi.advanceTimersByTimeAsync(1)

expect(rendered.getByText('stringCached')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(20)
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('string')).toBeInTheDocument()
queryClient.clear()
})
Expand Down Expand Up @@ -89,9 +89,8 @@ describe('React hydration', () => {
</QueryClientProvider>,
)

await vi.advanceTimersByTimeAsync(1)
expect(rendered.getByText('stringCached')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(20)
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('string')).toBeInTheDocument()

queryClientInner.clear()
Expand Down Expand Up @@ -123,9 +122,8 @@ describe('React hydration', () => {
</QueryClientProvider>,
)

await vi.advanceTimersByTimeAsync(1)
expect(rendered.getByText('stringCached')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(20)
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('string')).toBeInTheDocument()

const intermediateClient = new QueryClient()
Expand All @@ -134,7 +132,6 @@ describe('React hydration', () => {
queryKey: ['string'],
queryFn: () => sleep(20).then(() => ['should change']),
})
await vi.advanceTimersByTimeAsync(20)
intermediateClient.prefetchQuery({
queryKey: ['added'],
queryFn: () => sleep(20).then(() => ['added']),
Expand All @@ -158,9 +155,9 @@ describe('React hydration', () => {
// New query data should be available immediately
expect(rendered.getByText('added')).toBeInTheDocument()

await vi.advanceTimersByTimeAsync(20)
await vi.advanceTimersByTimeAsync(0)
// After effects phase has had time to run, the observer should have updated
expect(rendered.queryByText('string')).toBeNull()
expect(rendered.queryByText('string')).not.toBeInTheDocument()
expect(rendered.getByText('should change')).toBeInTheDocument()

queryClient.clear()
Expand Down Expand Up @@ -196,17 +193,15 @@ describe('React hydration', () => {
</QueryClientProvider>,
)

await vi.advanceTimersByTimeAsync(1)
expect(rendered.getByText('stringCached')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(20)
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('string')).toBeInTheDocument()

const intermediateClient = new QueryClient()
intermediateClient.prefetchQuery({
queryKey: ['string'],
queryFn: () => sleep(20).then(() => ['should not change']),
})
await vi.advanceTimersByTimeAsync(20)
intermediateClient.prefetchQuery({
queryKey: ['added'],
queryFn: () => sleep(20).then(() => ['added']),
Expand Down Expand Up @@ -238,7 +233,7 @@ describe('React hydration', () => {
</React.Suspense>,
)

rendered.getByText('loading')
expect(rendered.getByText('loading')).toBeInTheDocument()
})

React.startTransition(() => {
Expand All @@ -253,7 +248,9 @@ describe('React hydration', () => {

// This query existed before the transition so it should stay the same
expect(rendered.getByText('string')).toBeInTheDocument()
expect(rendered.queryByText('should not change')).toBeNull()
expect(
rendered.queryByText('should not change'),
).not.toBeInTheDocument()
// New query data should be available immediately because it was
// hydrated in the previous transition, even though the new dehydrated
// state did not contain it
Expand All @@ -263,7 +260,7 @@ describe('React hydration', () => {
await vi.advanceTimersByTimeAsync(20)
// It should stay the same even after effects have had a chance to run
expect(rendered.getByText('string')).toBeInTheDocument()
expect(rendered.queryByText('should not change')).toBeNull()
expect(rendered.queryByText('should not change')).not.toBeInTheDocument()

queryClient.clear()
})
Expand Down Expand Up @@ -292,9 +289,8 @@ describe('React hydration', () => {
</QueryClientProvider>,
)

await vi.advanceTimersByTimeAsync(1)
expect(rendered.getByText('stringCached')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(20)
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('string')).toBeInTheDocument()
const newClientQueryClient = new QueryClient()

Expand Down Expand Up @@ -359,7 +355,7 @@ describe('React hydration', () => {
</QueryClientProvider>,
)

await vi.runAllTimersAsync()
await vi.advanceTimersByTimeAsync(0)
expect(hydrateSpy).toHaveBeenCalledTimes(0)

hydrateSpy.mockRestore()
Expand All @@ -386,12 +382,11 @@ describe('React hydration', () => {
// For the bug to trigger, there needs to already be a query in the cache,
// with a dataUpdatedAt earlier than the dehydratedAt of the next query
const clientQueryClient = new QueryClient()
await clientQueryClient.prefetchQuery({
clientQueryClient.prefetchQuery({
queryKey: ['promise'],
queryFn: () => 'existing',
queryFn: () => sleep(20).then(() => 'existing'),
})

await vi.advanceTimersByTimeAsync(100)
await vi.advanceTimersByTimeAsync(20)

const prefetchQueryClient = new QueryClient({
defaultOptions: {
Expand All @@ -402,10 +397,8 @@ describe('React hydration', () => {
})
prefetchQueryClient.prefetchQuery({
queryKey: ['promise'],
queryFn: async () => {
await sleep(10)
throw new Error('Query failed')
},
queryFn: () =>
sleep(10).then(() => Promise.reject(new Error('Query failed'))),
})

const dehydratedState = dehydrate(prefetchQueryClient)
Expand All @@ -422,7 +415,7 @@ describe('React hydration', () => {
function Page() {
const { data } = useQuery({
queryKey: ['promise'],
queryFn: () => sleep(10).then(() => ['new']),
queryFn: () => sleep(20).then(() => ['new']),
})
return (
<div>
Expand All @@ -438,9 +431,9 @@ describe('React hydration', () => {
</HydrationBoundary>
</QueryClientProvider>,
)
await vi.advanceTimersByTimeAsync(1)

expect(rendered.getByText('existing')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(10)
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('new')).toBeInTheDocument()

process.removeListener('unhandledRejection', ignore)
Expand Down
Loading