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 const data = await new QueryClient().fetchInfiniteQuery(options)
161β
162 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
163 })
164 it('should ignore select when passed to fetchInfiniteQuery', async () => {
165 const options = infiniteQueryOptions({
166 queryKey: ['key'],
167 queryFn: () => Promise.resolve('string'),
168 getNextPageParam: () => 1,
169 initialPageParam: 1,
170 select: (data) => data.pages,
171 })
172β
173 const data = await new QueryClient().fetchInfiniteQuery(options)
174β
175 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
176 })
177 it('should tag the queryKey with the result type of the QueryFn', () => {
178 const { queryKey: tagged } = infiniteQueryOptions({
179 queryKey: queryKey(),
180 queryFn: () => Promise.resolve('string'),
181 getNextPageParam: () => 1,
182 initialPageParam: 1,
183 })
184β
185 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
186 })
187 it('should tag the queryKey even if no promise is returned', () => {
188 const { queryKey: tagged } = infiniteQueryOptions({
189 queryKey: queryKey(),
190 queryFn: () => 'string',
191 getNextPageParam: () => 1,
192 initialPageParam: 1,
193 })
194β
195 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
196 })
197 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
198 const { queryKey: tagged } = infiniteQueryOptions({
199 queryKey: queryKey(),
200 queryFn: () => Promise.resolve('string'),
201 select: (data) => data.pages,
202 getNextPageParam: () => 1,
203 initialPageParam: 1,
204 })
205β
206 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
207 })
208 it('should return the proper type when passed to getQueryData', () => {
209 const { queryKey: tagged } = infiniteQueryOptions({
210 queryKey: queryKey(),
211 queryFn: () => Promise.resolve('string'),
212 getNextPageParam: () => 1,
213 initialPageParam: 1,
214 })
215β
216 const queryClient = new QueryClient()
217 const data = queryClient.getQueryData(tagged)
218β
219 expectTypeOf(data).toEqualTypeOf<
220 InfiniteData<string, unknown> | undefined
221 >()
222 })
223 it('should properly type when passed to setQueryData', () => {
224 const { queryKey: tagged } = infiniteQueryOptions({
225 queryKey: queryKey(),
226 queryFn: () => Promise.resolve('string'),
227 getNextPageParam: () => 1,
228 initialPageParam: 1,
229 })
230β
231 const queryClient = new QueryClient()
232 const data = queryClient.setQueryData(tagged, (prev) => {
233 expectTypeOf(prev).toEqualTypeOf<
234 InfiniteData<string, unknown> | undefined
235 >()
236 return prev
237 })
238β
239 expectTypeOf(data).toEqualTypeOf<
240 InfiniteData<string, unknown> | undefined
241 >()
242 })
243 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
244 const options = infiniteQueryOptions({
245 queryKey: queryKey(),
246 queryFn:
247 Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
248 getNextPageParam: () => 1,
249 initialPageParam: 1,
250 })
251 // @ts-expect-error TS2345
252 const { data } = useSuspenseInfiniteQuery(options)
253 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
254 })
255β
256 it('should not be allowed to be passed to non-infinite query functions', () => {
257 const queryClient = new QueryClient()
258 const options = infiniteQueryOptions({
259 queryKey: queryKey(),
260 queryFn: () => Promise.resolve('string'),
261 getNextPageParam: () => 1,
262 initialPageParam: 1,
263 })
264 assertType(
265 // @ts-expect-error cannot pass infinite options to non-infinite query functions
266 useQuery(options),
267 )
268 assertType(
269 // @ts-expect-error cannot pass infinite options to non-infinite query functions
270 queryClient.ensureQueryData(options),
271 )
272 assertType(
273 // @ts-expect-error cannot pass infinite options to non-infinite query functions
274 queryClient.fetchQuery(options),
275 )
276 assertType(
277 // @ts-expect-error cannot pass infinite options to non-infinite query functions
278 queryClient.prefetchQuery(options),
279 )
280 })
281β
282 it('allow optional initialData function', () => {
283 const initialData: { example: boolean } | undefined = { example: true }
284 const queryOptions = infiniteQueryOptions({
285 queryKey: queryKey(),
286 queryFn: () => initialData,
287 initialData: initialData
288 ? () => ({ pages: [initialData], pageParams: [] })
289 : undefined,
290 getNextPageParam: () => 1,
291 initialPageParam: 1,
292 })
293 expectTypeOf(queryOptions.initialData).toExtend<
294 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
295 | InfiniteData<{ example: boolean }, number>
296 | undefined
297 >()
298 })
299β
300 it('allow optional initialData object', () => {
301 const initialData: { example: boolean } | undefined = { example: true }
302 const queryOptions = infiniteQueryOptions({
303 queryKey: queryKey(),
304 queryFn: () => initialData,
305 initialData: initialData
306 ? { pages: [initialData], pageParams: [] }
307 : undefined,
308 getNextPageParam: () => 1,
309 initialPageParam: 1,
310 })
311 expectTypeOf(queryOptions.initialData).toExtend<
312 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
313 | InfiniteData<{ example: boolean }, number>
314 | undefined
315 >()
316 })
317β
318 it('should return a custom query key type', () => {
319 type MyQueryKey = [Array<string>, { type: 'foo' }]
320β
321 const options = infiniteQueryOptions({
322 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
323 queryFn: () => Promise.resolve(1),
324 getNextPageParam: () => 1,
325 initialPageParam: 1,
326 })
327β
328 expectTypeOf(options.queryKey).toEqualTypeOf<
329 DataTag<MyQueryKey, InfiniteData<number>, Error>
330 >()
331 })
332β
333 it('should return a custom query key type with datatag', () => {
334 type MyQueryKey = DataTag<
335 [Array<string>, { type: 'foo' }],
336 number,
337 Error & { myMessage: string }
338 >
339β
340 const options = infiniteQueryOptions({
341 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
342 queryFn: () => Promise.resolve(1),
343 getNextPageParam: () => 1,
344 initialPageParam: 1,
345 })
346β
347 expectTypeOf(options.queryKey).toEqualTypeOf<
348 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
349 >()
350 })
351})