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