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 getNextPageParam: () => 1,
53 initialPageParam: 1,
54 })
55β
56 const { data } = 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 })
63 it('should work when passed to useInfiniteQuery with select', () => {
64 const options = infiniteQueryOptions({
65 queryKey: ['key'],
66 queryFn: () => Promise.resolve('string'),
67 getNextPageParam: () => 1,
68 initialPageParam: 1,
69 select: (data) => data.pages,
70 })
71β
72 const { data } = useInfiniteQuery(options)
73β
74 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
75 expectTypeOf(data).toEqualTypeOf<Array<string> | undefined>()
76 })
77 it('should work when passed to useSuspenseInfiniteQuery', () => {
78 const options = infiniteQueryOptions({
79 queryKey: queryKey(),
80 queryFn: () => Promise.resolve('string'),
81 getNextPageParam: () => 1,
82 initialPageParam: 1,
83 })
84β
85 const { data } = useSuspenseInfiniteQuery(options)
86β
87 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
88 })
89 it('should work when passed to useSuspenseInfiniteQuery with select', () => {
90 const options = infiniteQueryOptions({
91 queryKey: ['key'],
92 queryFn: () => Promise.resolve('string'),
93 getNextPageParam: () => 1,
94 initialPageParam: 1,
95 select: (data) => data.pages,
96 })
97β
98 const { data } = useSuspenseInfiniteQuery(options)
99β
100 expectTypeOf(data).toEqualTypeOf<Array<string>>()
101 })
102 it('should work when passed to infiniteQuery', async () => {
103 const options = infiniteQueryOptions({
104 queryKey: ['key'],
105 queryFn: () => Promise.resolve('string'),
106 getNextPageParam: () => 1,
107 initialPageParam: 1,
108 })
109β
110 const data = await new QueryClient().infiniteQuery(options)
111β
112 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
113 })
114 it('should work when passed to infiniteQuery with select', async () => {
115 const options = infiniteQueryOptions({
116 queryKey: ['key'],
117 queryFn: () => Promise.resolve('string'),
118 getNextPageParam: () => 1,
119 initialPageParam: 1,
120 select: (data) => data.pages,
121 })
122β
123 const data = await new QueryClient().infiniteQuery(options)
124β
125 expectTypeOf(data).toEqualTypeOf<Array<string>>()
126 })
127 it('should work when passed to infiniteQuery with enabled: false', async () => {
128 const options = infiniteQueryOptions({
129 queryKey: ['key'],
130 queryFn: () => Promise.resolve('string'),
131 getNextPageParam: () => 1,
132 initialPageParam: 1,
133 enabled: false,
134 })
135β
136 const data = await new QueryClient().infiniteQuery(options)
137β
138 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
139 })
140 it('should work when passed to infiniteQuery with skipToken', async () => {
141 const options = infiniteQueryOptions({
142 queryKey: ['key'],
143 queryFn: skipToken,
144 getNextPageParam: () => 1,
145 initialPageParam: 1,
146 })
147β
148 const data = await new QueryClient().infiniteQuery(options)
149β
150 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
151 })
152 it('should work when passed to fetchInfiniteQuery', async () => {
153 const options = infiniteQueryOptions({
154 queryKey: queryKey(),
155 queryFn: () => Promise.resolve('string'),
156 getNextPageParam: () => 1,
157 initialPageParam: 1,
158 })
159β
160 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
161 const data = await new QueryClient().fetchInfiniteQuery(options)
162β
163 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
164 })
165 it('should ignore select when passed to fetchInfiniteQuery', async () => {
166 const options = infiniteQueryOptions({
167 queryKey: ['key'],
168 queryFn: () => Promise.resolve('string'),
169 getNextPageParam: () => 1,
170 initialPageParam: 1,
171 select: (data) => data.pages,
172 })
173β
174 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
175 const data = await new QueryClient().fetchInfiniteQuery(options)
176β
177 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
178 })
179 it('should tag the queryKey with the result type of the QueryFn', () => {
180 const { queryKey: tagged } = infiniteQueryOptions({
181 queryKey: queryKey(),
182 queryFn: () => Promise.resolve('string'),
183 getNextPageParam: () => 1,
184 initialPageParam: 1,
185 })
186β
187 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
188 })
189 it('should tag the queryKey even if no promise is returned', () => {
190 const { queryKey: tagged } = infiniteQueryOptions({
191 queryKey: queryKey(),
192 queryFn: () => 'string',
193 getNextPageParam: () => 1,
194 initialPageParam: 1,
195 })
196β
197 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
198 })
199 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
200 const { queryKey: tagged } = infiniteQueryOptions({
201 queryKey: queryKey(),
202 queryFn: () => Promise.resolve('string'),
203 select: (data) => data.pages,
204 getNextPageParam: () => 1,
205 initialPageParam: 1,
206 })
207β
208 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
209 })
210 it('should return the proper type when passed to getQueryData', () => {
211 const { queryKey: tagged } = infiniteQueryOptions({
212 queryKey: queryKey(),
213 queryFn: () => Promise.resolve('string'),
214 getNextPageParam: () => 1,
215 initialPageParam: 1,
216 })
217β
218 const queryClient = new QueryClient()
219 const data = queryClient.getQueryData(tagged)
220β
221 expectTypeOf(data).toEqualTypeOf<
222 InfiniteData<string, unknown> | undefined
223 >()
224 })
225 it('should properly type when passed to setQueryData', () => {
226 const { queryKey: tagged } = infiniteQueryOptions({
227 queryKey: queryKey(),
228 queryFn: () => Promise.resolve('string'),
229 getNextPageParam: () => 1,
230 initialPageParam: 1,
231 })
232β
233 const queryClient = new QueryClient()
234 const data = queryClient.setQueryData(tagged, (prev) => {
235 expectTypeOf(prev).toEqualTypeOf<
236 InfiniteData<string, unknown> | undefined
237 >()
238 return prev
239 })
240β
241 expectTypeOf(data).toEqualTypeOf<
242 InfiniteData<string, unknown> | undefined
243 >()
244 })
245 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
246 const options = infiniteQueryOptions({
247 queryKey: queryKey(),
248 queryFn:
249 Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
250 getNextPageParam: () => 1,
251 initialPageParam: 1,
252 })
253 // @ts-expect-error TS2345
254 const { data } = useSuspenseInfiniteQuery(options)
255 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
256 })
257β
258 it('should not be allowed to be passed to non-infinite query functions', () => {
259 const queryClient = new QueryClient()
260 const options = infiniteQueryOptions({
261 queryKey: queryKey(),
262 queryFn: () => Promise.resolve('string'),
263 getNextPageParam: () => 1,
264 initialPageParam: 1,
265 })
266 assertType(
267 // @ts-expect-error cannot pass infinite options to non-infinite query functions
268 useQuery(options),
269 )
270 assertType(
271 // @ts-expect-error cannot pass infinite options to non-infinite query functions
272 queryClient.query(options),
273 )
274β
275 // deprecated methods to be removed in v6
276 assertType(
277 // @ts-expect-error cannot pass infinite options to non-infinite query functions
278 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
279 queryClient.ensureQueryData(options),
280 )
281 assertType(
282 // @ts-expect-error cannot pass infinite options to non-infinite query functions
283 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
284 queryClient.fetchQuery(options),
285 )
286 assertType(
287 // @ts-expect-error cannot pass infinite options to non-infinite query functions
288 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
289 queryClient.prefetchQuery(options),
290 )
291 })
292β
293 it('should allow optional initialData function', () => {
294 const initialData: { example: boolean } | undefined = { example: true }
295 const queryOptions = infiniteQueryOptions({
296 queryKey: queryKey(),
297 queryFn: () => initialData,
298 initialData: initialData
299 ? () => ({ pages: [initialData], pageParams: [] })
300 : undefined,
301 getNextPageParam: () => 1,
302 initialPageParam: 1,
303 })
304 expectTypeOf(queryOptions.initialData).toExtend<
305 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
306 | InfiniteData<{ example: boolean }, number>
307 | undefined
308 >()
309 })
310β
311 it('should allow optional initialData object', () => {
312 const initialData: { example: boolean } | undefined = { example: true }
313 const queryOptions = infiniteQueryOptions({
314 queryKey: queryKey(),
315 queryFn: () => initialData,
316 initialData: initialData
317 ? { pages: [initialData], pageParams: [] }
318 : undefined,
319 getNextPageParam: () => 1,
320 initialPageParam: 1,
321 })
322 expectTypeOf(queryOptions.initialData).toExtend<
323 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
324 | InfiniteData<{ example: boolean }, number>
325 | undefined
326 >()
327 })
328β
329 it('should return a custom query key type', () => {
330 type MyQueryKey = [Array<string>, { type: 'foo' }]
331β
332 const options = infiniteQueryOptions({
333 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
334 queryFn: () => Promise.resolve(1),
335 getNextPageParam: () => 1,
336 initialPageParam: 1,
337 })
338β
339 expectTypeOf(options.queryKey).toEqualTypeOf<
340 DataTag<MyQueryKey, InfiniteData<number>, Error>
341 >()
342 })
343β
344 it('should return a custom query key type with datatag', () => {
345 type MyQueryKey = DataTag<
346 [Array<string>, { type: 'foo' }],
347 number,
348 Error & { myMessage: string }
349 >
350β
351 const options = infiniteQueryOptions({
352 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
353 queryFn: () => Promise.resolve(1),
354 getNextPageParam: () => 1,
355 initialPageParam: 1,
356 })
357β
358 expectTypeOf(options.queryKey).toEqualTypeOf<
359 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
360 >()
361 })
362})