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

infiniteQueryOptions.test-d.tsx

312 lines10.1 KB

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