Skip to content

test(react-query/useSuspenseQueries): use precise time in 'advanceTimersByTimeAsync', and add 'expect' using 'toBeInTheDocument' #9450

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
95 changes: 40 additions & 55 deletions packages/react-query/src/__tests__/useSuspenseQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ describe('useSuspenseQueries', () => {
</React.Suspense>,
)

rendered.getByText('loading...')
expect(rendered.getByText('loading...')).toBeInTheDocument()
expect(spy).not.toHaveBeenCalled()
await act(() => vi.advanceTimersByTimeAsync(30))
rendered.getByText('data')
expect(rendered.getByText('data')).toBeInTheDocument()
expect(spy).toHaveBeenCalled()
})
})
Expand Down Expand Up @@ -241,9 +241,9 @@ describe('useSuspenseQueries 2', () => {
</React.Suspense>,
)

rendered.getByText('loading')
await act(() => vi.advanceTimersByTimeAsync(21))
rendered.getByText('data: 1,2')
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(20))
expect(rendered.getByText('data: 1,2')).toBeInTheDocument()
expect(results).toEqual(['1', '2', 'loading'])
})

Expand Down Expand Up @@ -296,10 +296,10 @@ describe('useSuspenseQueries 2', () => {
</React.Suspense>,
)

rendered.getByText('loading')
expect(rendered.getByText('loading')).toBeInTheDocument()
expect(refs.length).toBe(2)
await act(() => vi.advanceTimersByTimeAsync(21))
rendered.getByText('data: 1,2')
await act(() => vi.advanceTimersByTimeAsync(20))
expect(rendered.getByText('data: 1,2')).toBeInTheDocument()
expect(refs[0]).toBe(refs[1])
})

Expand All @@ -311,7 +311,7 @@ describe('useSuspenseQueries 2', () => {

const { data } = useSuspenseQuery({
queryKey: [id],
queryFn: () => Promise.resolve(`Data ${id}`),
queryFn: () => sleep(10).then(() => `Data ${id}`),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

})

// defensive guard here
Expand All @@ -335,8 +335,8 @@ describe('useSuspenseQueries 2', () => {
)

expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(0))
rendered.getByText('Data 0')
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('Data 0')).toBeInTheDocument()

// go offline
document.dispatchEvent(new CustomEvent('offline'))
Expand All @@ -348,7 +348,7 @@ describe('useSuspenseQueries 2', () => {
document.dispatchEvent(new CustomEvent('online'))

fireEvent.click(rendered.getByText('fetch'))
await act(() => vi.advanceTimersByTimeAsync(0))
await act(() => vi.advanceTimersByTimeAsync(10))
// query should resume
expect(rendered.getByText('Data 1')).toBeInTheDocument()
})
Expand All @@ -363,15 +363,11 @@ describe('useSuspenseQueries 2', () => {
const [fail, setFail] = React.useState(false)
const { data } = useSuspenseQuery({
queryKey: [key, fail],
queryFn: async () => {
await sleep(10)

if (fail) {
throw new Error('Suspense Error Bingo')
} else {
queryFn: () =>
sleep(10).then(() => {
if (fail) throw new Error('Suspense Error Bingo')
return 'data'
}
},
}),
retry: 0,
})

Expand All @@ -393,12 +389,12 @@ describe('useSuspenseQueries 2', () => {
</ErrorBoundary>,
)

rendered.getByText('Loading...')
await act(() => vi.advanceTimersByTimeAsync(11))
rendered.getByText('rendered: data')
expect(rendered.getByText('Loading...')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('rendered: data')).toBeInTheDocument()
fireEvent.click(rendered.getByText('trigger fail'))
await act(() => vi.advanceTimersByTimeAsync(11))
rendered.getByText('error boundary')
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('error boundary')).toBeInTheDocument()
expect(consoleMock.mock.calls[0]?.[1]).toStrictEqual(
new Error('Suspense Error Bingo'),
)
Expand All @@ -413,10 +409,7 @@ describe('useSuspenseQueries 2', () => {
const [isPending, startTransition] = React.useTransition()
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: async () => {
await sleep(10)
return 'data' + count
},
queryFn: async () => sleep(10).then(() => 'data' + count),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
queryFn: async () => sleep(10).then(() => 'data' + count),
queryFn: () => sleep(10).then(() => 'data' + count),

Copy link
Contributor Author

@sukvvon sukvvon Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manudeli

I missed this. I gonna fix this next time

})

return (
Expand All @@ -438,11 +431,11 @@ describe('useSuspenseQueries 2', () => {
)

expect(rendered.getByText('Loading...')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(11))
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data0')).toBeInTheDocument()
fireEvent.click(rendered.getByText('inc'))
expect(rendered.getByText('Pending...')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(11))
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data1')).toBeInTheDocument()
})

Expand Down Expand Up @@ -490,12 +483,12 @@ describe('useSuspenseQueries 2', () => {
<App />,
)

rendered.getByText('Loading...')
await act(() => vi.advanceTimersByTimeAsync(11))
rendered.getByText('data0')
expect(rendered.getByText('Loading...')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data0')).toBeInTheDocument()
fireEvent.click(rendered.getByText('inc'))
await act(() => vi.advanceTimersByTimeAsync(11))
rendered.getByText('data1')
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data1')).toBeInTheDocument()
expect(queryFnCount).toBe(2)
})

Expand All @@ -514,10 +507,7 @@ describe('useSuspenseQueries 2', () => {
const [isPending, startTransition] = React.useTransition()
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: async () => {
await sleep(10)
return 'data' + count
},
queryFn: async () => sleep(10).then(() => 'data' + count),
})

return (
Expand All @@ -539,11 +529,11 @@ describe('useSuspenseQueries 2', () => {
)

expect(rendered.getByText('Loading...')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(11))
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data0')).toBeInTheDocument()
fireEvent.click(rendered.getByText('inc'))
expect(rendered.getByText('Pending...')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(11))
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data1')).toBeInTheDocument()
})

Expand All @@ -557,8 +547,9 @@ describe('useSuspenseQueries 2', () => {
function Page() {
useSuspenseQuery({
queryKey: key,
queryFn: () => {
queryFn: async () => {
count++
await sleep(10)
return Promise.reject(new Error('Query failed'))
},
gcTime: 0,
Expand All @@ -584,8 +575,8 @@ describe('useSuspenseQueries 2', () => {

const rendered = renderWithClient(queryClient, <App />)

await act(() => vi.advanceTimersByTimeAsync(0))
rendered.getByText('There was an error!')
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('There was an error!')).toBeInTheDocument()
expect(count).toBe(1)
consoleMock.mockRestore()
})
Expand Down Expand Up @@ -613,10 +604,7 @@ describe('useSuspenseQueries 2', () => {
function Component() {
const { data } = useSuspenseQuery({
queryKey: key,
queryFn: async () => {
await sleep(3000)
return 'data'
},
queryFn: async () => sleep(3000).then(() => 'data'),
gcTime: 1000,
})

Expand All @@ -639,14 +627,11 @@ describe('useSuspenseQueries 2', () => {

const rendered = renderWithClient(queryClient, <App />)

await act(() => vi.advanceTimersByTimeAsync(200))
rendered.getByText('loading')
// unmount while still fetching
expect(rendered.getByText('loading')).toBeInTheDocument()
fireEvent.click(rendered.getByText('hide'))
await act(() => vi.advanceTimersByTimeAsync(800))
rendered.getByText('page2')
expect(rendered.getByText('page2')).toBeInTheDocument()
// wait for query to be resolved
await act(() => vi.advanceTimersByTimeAsync(2000))
Comment on lines -642 to -649
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍

await act(() => vi.advanceTimersByTimeAsync(3000))
expect(queryClient.getQueryData(key)).toBe('data')
// wait for gc
await act(() => vi.advanceTimersByTimeAsync(1000))
Expand Down
Loading