Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { infiniteQueryOptions } from '../infiniteQueryOptions'
5import { useInfiniteQuery } from '../useInfiniteQuery'
6import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
7import { useQuery } from '../useQuery'
8import type {
9 DataTag,
10 InfiniteData,
11 InitialDataFunction,
12} from '@tanstack/query-core'
13β
14// Regression test for exported infiniteQueryOptions inference under declaration emit.
15// TypeScript should be able to name the return type without expanding the
16// internal data tag symbols into the consumer's .d.ts output.
17export const exportedInfiniteQueryOptions = infiniteQueryOptions({
18 queryKey: ['invalid'],
19 getNextPageParam: () => 1,
20 initialPageParam: 1,
21})
22β
23describe('infiniteQueryOptions', () => {
24 it('should not allow excess properties', () => {
25 assertType(
26 infiniteQueryOptions({
27 queryKey: queryKey(),
28 queryFn: () => Promise.resolve('data'),
29 getNextPageParam: () => 1,
30 initialPageParam: 1,
31 // @ts-expect-error this is a good error, because stallTime does not exist!
32 stallTime: 1000,
33 }),
34 )
35 })
36 it('should infer types for callbacks', () => {
37 infiniteQueryOptions({
38 queryKey: queryKey(),
39 queryFn: () => Promise.resolve('data'),
40 staleTime: 1000,
41 getNextPageParam: () => 1,
42 initialPageParam: 1,
43 select: (data) => {
44 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
45 },
46 })
47 })
48 it('should work when passed to useInfiniteQuery', () => {
49 const options = infiniteQueryOptions({
50 queryKey: queryKey(),
51 queryFn: () => Promise.resolve('string'),
52 initialPageParam: 1,
53 mode: 'manual',
54 })
55β
56 const { data, fetchNextPage, fetchPreviousPage } = useInfiniteQuery(options)
57β
58 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
59 expectTypeOf(data).toEqualTypeOf<
60 InfiniteData<string, unknown> | undefined
61 >()
62 fetchNextPage({ pageParam: 2 })
63 fetchPreviousPage({ pageParam: 0 })
64β
65 // @ts-expect-error pageParam is required in manual mode
66 fetchNextPage()
67 })
68 it('should work when passed to useInfiniteQuery with select', () => {
69 const options = infiniteQueryOptions({
70 queryKey: ['key'],
71 queryFn: () => Promise.resolve('string'),
72 getNextPageParam: () => 1,
73 initialPageParam: 1,
74 select: (data) => data.pages,
75 })
76β
77 const { data } = useInfiniteQuery(options)
78β
79 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
80 expectTypeOf(data).toEqualTypeOf<Array<string> | undefined>()
81 })
82 it('should work when passed to useSuspenseInfiniteQuery', () => {
83 const options = infiniteQueryOptions({
84 queryKey: queryKey(),
85 queryFn: () => Promise.resolve('string'),
86 getNextPageParam: () => 1,
87 initialPageParam: 1,
88 })
89β
90 const { data } = useSuspenseInfiniteQuery(options)
91β
92 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
93 })
94 it('should work when passed to useSuspenseInfiniteQuery with select', () => {
95 const options = infiniteQueryOptions({
96 queryKey: ['key'],
97 queryFn: () => Promise.resolve('string'),
98 getNextPageParam: () => 1,
99 initialPageParam: 1,
100 select: (data) => data.pages,
101 })
102β
103 const { data } = useSuspenseInfiniteQuery(options)
104β
105 expectTypeOf(data).toEqualTypeOf<Array<string>>()
106 })
107 it('should work when passed to infiniteQuery', async () => {
108 const options = infiniteQueryOptions({
109 queryKey: ['key'],
110 queryFn: () => Promise.resolve('string'),
111 initialPageParam: 1,
112 mode: 'manual',
113 })
114β
115 const data = await new QueryClient().infiniteQuery(options)
116β
117 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
118 })
119 it('should work when passed to infiniteQuery with select', async () => {
120 const options = infiniteQueryOptions({
121 queryKey: ['key'],
122 queryFn: () => Promise.resolve('string'),
123 getNextPageParam: () => 1,
124 initialPageParam: 1,
125 select: (data) => data.pages,
126 })
127β
128 const data = await new QueryClient().infiniteQuery(options)
129β
130 expectTypeOf(data).toEqualTypeOf<Array<string>>()
131 })
132 it('should work when passed to infiniteQuery with enabled: false', async () => {
133 const options = infiniteQueryOptions({
134 queryKey: ['key'],
135 queryFn: () => Promise.resolve('string'),
136 getNextPageParam: () => 1,
137 initialPageParam: 1,
138 enabled: false,
139 })
140β
141 const data = await new QueryClient().infiniteQuery(options)
142β
143 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
144 })
145 it('should work when passed to infiniteQuery with skipToken', async () => {
146 const options = infiniteQueryOptions({
147 queryKey: ['key'],
148 queryFn: skipToken,
149 getNextPageParam: () => 1,
150 initialPageParam: 1,
151 })
152β
153 const data = await new QueryClient().infiniteQuery(options)
154β
155 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
156 })
157 it('should work when passed to fetchInfiniteQuery', async () => {
158 const options = infiniteQueryOptions({
159 queryKey: queryKey(),
160 queryFn: () => Promise.resolve('string'),
161 getNextPageParam: () => 1,
162 initialPageParam: 1,
163 })
164β
165 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
166 const data = await new QueryClient().fetchInfiniteQuery(options)
167β
168 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
169 })
170 it('should ignore select when passed to fetchInfiniteQuery', async () => {
171 const options = infiniteQueryOptions({
172 queryKey: ['key'],
173 queryFn: () => Promise.resolve('string'),
174 getNextPageParam: () => 1,
175 initialPageParam: 1,
176 select: (data) => data.pages,
177 })
178β
179 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
180 const data = await new QueryClient().fetchInfiniteQuery(options)
181β
182 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
183 })
184 it('should tag the queryKey with the result type of the QueryFn', () => {
185 const { queryKey: tagged } = infiniteQueryOptions({
186 queryKey: queryKey(),
187 queryFn: () => Promise.resolve('string'),
188 getNextPageParam: () => 1,
189 initialPageParam: 1,
190 })
191β
192 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
193 })
194 it('should tag the queryKey even if no promise is returned', () => {
195 const { queryKey: tagged } = infiniteQueryOptions({
196 queryKey: queryKey(),
197 queryFn: () => 'string',
198 getNextPageParam: () => 1,
199 initialPageParam: 1,
200 })
201β
202 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
203 })
204 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
205 const { queryKey: tagged } = infiniteQueryOptions({
206 queryKey: queryKey(),
207 queryFn: () => Promise.resolve('string'),
208 select: (data) => data.pages,
209 getNextPageParam: () => 1,
210 initialPageParam: 1,
211 })
212β
213 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
214 })
215 it('should return the proper type when passed to getQueryData', () => {
216 const { queryKey: tagged } = infiniteQueryOptions({
217 queryKey: queryKey(),
218 queryFn: () => Promise.resolve('string'),
219 getNextPageParam: () => 1,
220 initialPageParam: 1,
221 })
222β
223 const queryClient = new QueryClient()
224 const data = queryClient.getQueryData(tagged)
225β
226 expectTypeOf(data).toEqualTypeOf<
227 InfiniteData<string, unknown> | undefined
228 >()
229 })
230 it('should properly type when passed to setQueryData', () => {
231 const { queryKey: tagged } = infiniteQueryOptions({
232 queryKey: queryKey(),
233 queryFn: () => Promise.resolve('string'),
234 getNextPageParam: () => 1,
235 initialPageParam: 1,
236 })
237β
238 const queryClient = new QueryClient()
239 const data = queryClient.setQueryData(tagged, (prev) => {
240 expectTypeOf(prev).toEqualTypeOf<
241 InfiniteData<string, unknown> | undefined
242 >()
243 return prev
244 })
245β
246 expectTypeOf(data).toEqualTypeOf<
247 InfiniteData<string, unknown> | undefined
248 >()
249 })
250 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
251 const options = infiniteQueryOptions({
252 queryKey: queryKey(),
253 queryFn:
254 Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
255 getNextPageParam: () => 1,
256 initialPageParam: 1,
257 })
258 // @ts-expect-error TS2345
259 const { data } = useSuspenseInfiniteQuery(options)
260 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
261 })
262β
263 it('should reject missing mode / getNextPageParam and reject getters in manual mode', () => {
264 // @ts-expect-error getNextPageParam is required unless mode is manual
265 infiniteQueryOptions({
266 queryKey: ['key'],
267 queryFn: () => Promise.resolve('string'),
268 initialPageParam: 1,
269 })
270β
271 // @ts-expect-error getNextPageParam is not allowed in manual mode
272 infiniteQueryOptions({
273 queryKey: ['key'],
274 queryFn: () => Promise.resolve('string'),
275 initialPageParam: 1,
276 mode: 'manual',
277 getNextPageParam: () => 1,
278 })
279β
280 // @ts-expect-error getPreviousPageParam is not allowed in manual mode
281 infiniteQueryOptions({
282 queryKey: ['key'],
283 queryFn: () => Promise.resolve('string'),
284 initialPageParam: 1,
285 mode: 'manual',
286 getPreviousPageParam: () => 0,
287 })
288 })
289β
290 it('should not be allowed to be passed to non-infinite query functions', () => {
291 const queryClient = new QueryClient()
292 const options = infiniteQueryOptions({
293 queryKey: queryKey(),
294 queryFn: () => Promise.resolve('string'),
295 getNextPageParam: () => 1,
296 initialPageParam: 1,
297 })
298 assertType(
299 // @ts-expect-error cannot pass infinite options to non-infinite query functions
300 useQuery(options),
301 )
302 assertType(
303 // @ts-expect-error cannot pass infinite options to non-infinite query functions
304 queryClient.query(options),
305 )
306β
307 // deprecated methods to be removed in v6
308 assertType(
309 // @ts-expect-error cannot pass infinite options to non-infinite query functions
310 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
311 queryClient.ensureQueryData(options),
312 )
313 assertType(
314 // @ts-expect-error cannot pass infinite options to non-infinite query functions
315 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
316 queryClient.fetchQuery(options),
317 )
318 assertType(
319 // @ts-expect-error cannot pass infinite options to non-infinite query functions
320 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
321 queryClient.prefetchQuery(options),
322 )
323 })
324β
325 it('should allow optional initialData function', () => {
326 const initialData: { example: boolean } | undefined = { example: true }
327 const queryOptions = infiniteQueryOptions({
328 queryKey: queryKey(),
329 queryFn: () => initialData,
330 initialData: initialData
331 ? () => ({ pages: [initialData], pageParams: [] })
332 : undefined,
333 getNextPageParam: () => 1,
334 initialPageParam: 1,
335 })
336 expectTypeOf(queryOptions.initialData).toExtend<
337 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
338 | InfiniteData<{ example: boolean }, number>
339 | undefined
340 >()
341 })
342β
343 it('should allow optional initialData object', () => {
344 const initialData: { example: boolean } | undefined = { example: true }
345 const queryOptions = infiniteQueryOptions({
346 queryKey: queryKey(),
347 queryFn: () => initialData,
348 initialData: initialData
349 ? { pages: [initialData], pageParams: [] }
350 : undefined,
351 getNextPageParam: () => 1,
352 initialPageParam: 1,
353 })
354 expectTypeOf(queryOptions.initialData).toExtend<
355 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
356 | InfiniteData<{ example: boolean }, number>
357 | undefined
358 >()
359 })
360β
361 it('should return a custom query key type', () => {
362 type MyQueryKey = [Array<string>, { type: 'foo' }]
363β
364 const options = infiniteQueryOptions({
365 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
366 queryFn: () => Promise.resolve(1),
367 getNextPageParam: () => 1,
368 initialPageParam: 1,
369 })
370β
371 expectTypeOf(options.queryKey).toEqualTypeOf<
372 DataTag<MyQueryKey, InfiniteData<number>, Error>
373 >()
374 })
375β
376 it('should return a custom query key type with datatag', () => {
377 type MyQueryKey = DataTag<
378 [Array<string>, { type: 'foo' }],
379 number,
380 Error & { myMessage: string }
381 >
382β
383 const options = infiniteQueryOptions({
384 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
385 queryFn: () => Promise.resolve(1),
386 getNextPageParam: () => 1,
387 initialPageParam: 1,
388 })
389β
390 expectTypeOf(options.queryKey).toEqualTypeOf<
391 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
392 >()
393 })
394})