Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useSuspenseInfiniteQuery.test.tsx

185 lines4.7 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { act, fireEvent } from '@testing-library/react'
3import * as React from 'react'
4import { queryKey, sleep } from '@tanstack/query-test-utils'
5import {
6 QueryCache,
7 QueryClient,
8 skipToken,
9 useSuspenseInfiniteQuery,
10} from '..'
11import { renderWithClient } from './utils'
12import type { InfiniteData, UseSuspenseInfiniteQueryResult } from '..'
13​
14describe('useSuspenseInfiniteQuery', () => {
15 let queryCache: QueryCache
16 let queryClient: QueryClient
17​
18 beforeEach(() => {
19 vi.useFakeTimers()
20 queryCache = new QueryCache()
21 queryClient = new QueryClient({ queryCache })
22 })
23​
24 afterEach(() => {
25 queryClient.clear()
26 vi.useRealTimers()
27 })
28​
29 it('should return the correct states for a successful infinite query', async () => {
30 const key = queryKey()
31 const states: Array<UseSuspenseInfiniteQueryResult<InfiniteData<number>>> =
32 []
33​
34 function Page() {
35 const [multiplier, setMultiplier] = React.useState(1)
36 const state = useSuspenseInfiniteQuery({
37 queryKey: [`${key}_${multiplier}`],
38 queryFn: ({ pageParam }) =>
39 sleep(10).then(() => pageParam * multiplier),
40 initialPageParam: 1,
41 getNextPageParam: (lastPage) => lastPage + 1,
42 })
43​
44 states.push(state)
45​
46 return (
47 <div>
48 <button onClick={() => setMultiplier(2)}>next</button>
49 data: {state.data?.pages.join(',')}
50 </div>
51 )
52 }
53​
54 const rendered = renderWithClient(
55 queryClient,
56 <React.Suspense fallback="loading">
57 <Page />
58 </React.Suspense>,
59 )
60​
61 expect(rendered.getByText('loading')).toBeInTheDocument()
62 await act(() => vi.advanceTimersByTimeAsync(10))
63 expect(rendered.getByText('data: 1')).toBeInTheDocument()
64​
65 expect(states.length).toBe(1)
66 expect(states[0]).toMatchObject({
67 data: { pages: [1], pageParams: [1] },
68 status: 'success',
69 })
70​
71 fireEvent.click(rendered.getByText('next'))
72 expect(rendered.getByText('loading')).toBeInTheDocument()
73 await act(() => vi.advanceTimersByTimeAsync(10))
74 expect(rendered.getByText('data: 2')).toBeInTheDocument()
75​
76 expect(states.length).toBe(2)
77 expect(states[1]).toMatchObject({
78 data: { pages: [2], pageParams: [1] },
79 status: 'success',
80 })
81 })
82​
83 it('should log an error when skipToken is passed as queryFn', () => {
84 const consoleErrorSpy = vi
85 .spyOn(console, 'error')
86 .mockImplementation(() => {})
87 const key = queryKey()
88​
89 function Page() {
90 useSuspenseInfiniteQuery({
91 queryKey: key,
92 initialPageParam: 1,
93 getNextPageParam: () => 1,
94 // @ts-expect-error
95 // eslint-disable-next-line react-hooks/purity
96 queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
97 })
98​
99 return null
100 }
101​
102 function App() {
103 return (
104 <React.Suspense fallback="Loading...">
105 <Page />
106 </React.Suspense>
107 )
108 }
109​
110 renderWithClient(queryClient, <App />)
111​
112 expect(consoleErrorSpy).toHaveBeenCalledWith(
113 'skipToken is not allowed for useSuspenseInfiniteQuery',
114 )
115 consoleErrorSpy.mockRestore()
116 })
117​
118 it('should log an error when skipToken is used in development environment', () => {
119 const envCopy = process.env.NODE_ENV
120 process.env.NODE_ENV = 'development'
121​
122 const consoleErrorSpy = vi
123 .spyOn(console, 'error')
124 .mockImplementation(() => undefined)
125 const key = queryKey()
126​
127 function Page() {
128 useSuspenseInfiniteQuery({
129 queryKey: key,
130 queryFn: skipToken as any,
131 initialPageParam: 1,
132 getNextPageParam: () => 1,
133 })
134​
135 return null
136 }
137​
138 renderWithClient(
139 queryClient,
140 <React.Suspense fallback="Loading...">
141 <Page />
142 </React.Suspense>,
143 )
144​
145 expect(consoleErrorSpy).toHaveBeenCalledWith(
146 'skipToken is not allowed for useSuspenseInfiniteQuery',
147 )
148​
149 consoleErrorSpy.mockRestore()
150 process.env.NODE_ENV = envCopy
151 })
152​
153 it('should not log an error when skipToken is used in production environment', () => {
154 const envCopy = process.env.NODE_ENV
155 process.env.NODE_ENV = 'production'
156​
157 const consoleErrorSpy = vi
158 .spyOn(console, 'error')
159 .mockImplementation(() => undefined)
160 const key = queryKey()
161​
162 function Page() {
163 useSuspenseInfiniteQuery({
164 queryKey: key,
165 queryFn: skipToken as any,
166 initialPageParam: 1,
167 getNextPageParam: () => 1,
168 })
169​
170 return null
171 }
172​
173 renderWithClient(
174 queryClient,
175 <React.Suspense fallback="Loading...">
176 <Page />
177 </React.Suspense>,
178 )
179​
180 expect(consoleErrorSpy).not.toHaveBeenCalled()
181​
182 consoleErrorSpy.mockRestore()
183 process.env.NODE_ENV = envCopy
184 })
185})