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

infiniteQueryOptions.test-d.tsx

353 lines11.3 KB

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