|
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' |
2 | 3 | import * as React from 'react' |
3 | | -import { queryKey } from '@tanstack/query-test-utils' |
| 4 | +import { queryKey, sleep } from '@tanstack/query-test-utils' |
4 | 5 | import { |
5 | 6 | QueryCache, |
6 | 7 | QueryClient, |
7 | 8 | skipToken, |
8 | 9 | useSuspenseInfiniteQuery, |
9 | 10 | } from '..' |
10 | 11 | import { renderWithClient } from './utils' |
| 12 | +import type { InfiniteData, UseSuspenseInfiniteQueryResult } from '..' |
11 | 13 |
|
12 | 14 | describe('useSuspenseInfiniteQuery', () => { |
| 15 | + beforeEach(() => { |
| 16 | + vi.useFakeTimers() |
| 17 | + }) |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + vi.useRealTimers() |
| 21 | + }) |
| 22 | + |
13 | 23 | const queryCache = new QueryCache() |
14 | 24 | const queryClient = new QueryClient({ queryCache }) |
15 | 25 |
|
| 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 | + |
16 | 80 | it('should log an error when skipToken is passed as queryFn', () => { |
17 | 81 | const consoleErrorSpy = vi |
18 | 82 | .spyOn(console, 'error') |
|
0 commit comments