Skip to content

Commit 3fac499

Browse files
test(react-query/useSuspenseInfiniteQuery): add test for basic suspend and resolve states (#10213)
* test(react-query/useSuspenseInfiniteQuery): add test for basic suspend and resolve states * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 6d0a2a9 commit 3fac499

1 file changed

Lines changed: 66 additions & 2 deletions

File tree

packages/react-query/src/__tests__/useSuspenseInfiniteQuery.test.tsx

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,82 @@
1-
import { describe, expect, it, vi } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { act, fireEvent } from '@testing-library/react'
23
import * as React from 'react'
3-
import { queryKey } from '@tanstack/query-test-utils'
4+
import { queryKey, sleep } from '@tanstack/query-test-utils'
45
import {
56
QueryCache,
67
QueryClient,
78
skipToken,
89
useSuspenseInfiniteQuery,
910
} from '..'
1011
import { renderWithClient } from './utils'
12+
import type { InfiniteData, UseSuspenseInfiniteQueryResult } from '..'
1113

1214
describe('useSuspenseInfiniteQuery', () => {
15+
beforeEach(() => {
16+
vi.useFakeTimers()
17+
})
18+
19+
afterEach(() => {
20+
vi.useRealTimers()
21+
})
22+
1323
const queryCache = new QueryCache()
1424
const queryClient = new QueryClient({ queryCache })
1525

26+
it('should return the correct states for a successful infinite query', async () => {
27+
const key = queryKey()
28+
const states: Array<UseSuspenseInfiniteQueryResult<InfiniteData<number>>> =
29+
[]
30+
31+
function Page() {
32+
const [multiplier, setMultiplier] = React.useState(1)
33+
const state = useSuspenseInfiniteQuery({
34+
queryKey: [`${key}_${multiplier}`],
35+
queryFn: ({ pageParam }) =>
36+
sleep(10).then(() => pageParam * multiplier),
37+
initialPageParam: 1,
38+
getNextPageParam: (lastPage) => lastPage + 1,
39+
})
40+
41+
states.push(state)
42+
43+
return (
44+
<div>
45+
<button onClick={() => setMultiplier(2)}>next</button>
46+
data: {state.data?.pages.join(',')}
47+
</div>
48+
)
49+
}
50+
51+
const rendered = renderWithClient(
52+
queryClient,
53+
<React.Suspense fallback="loading">
54+
<Page />
55+
</React.Suspense>,
56+
)
57+
58+
expect(rendered.getByText('loading')).toBeInTheDocument()
59+
await act(() => vi.advanceTimersByTimeAsync(10))
60+
expect(rendered.getByText('data: 1')).toBeInTheDocument()
61+
62+
expect(states.length).toBe(1)
63+
expect(states[0]).toMatchObject({
64+
data: { pages: [1], pageParams: [1] },
65+
status: 'success',
66+
})
67+
68+
fireEvent.click(rendered.getByText('next'))
69+
expect(rendered.getByText('loading')).toBeInTheDocument()
70+
await act(() => vi.advanceTimersByTimeAsync(10))
71+
expect(rendered.getByText('data: 2')).toBeInTheDocument()
72+
73+
expect(states.length).toBe(2)
74+
expect(states[1]).toMatchObject({
75+
data: { pages: [2], pageParams: [1] },
76+
status: 'success',
77+
})
78+
})
79+
1680
it('should log an error when skipToken is passed as queryFn', () => {
1781
const consoleErrorSpy = vi
1882
.spyOn(console, 'error')

0 commit comments

Comments
 (0)