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

infinite-query-options.test-d.ts

314 lines10.0 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { queryKey } 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 = queryKey()
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 it('should infer types for callbacks', () => {
37 const key = queryKey()
38 infiniteQueryOptions({
39 queryKey: key,
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 key = queryKey()
51 const options = infiniteQueryOptions({
52 queryKey: key,
53 queryFn: () => Promise.resolve('string'),
54 getNextPageParam: () => 1,
55 initialPageParam: 1,
56 })
57​
58 const { data } = injectInfiniteQuery(() => 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 fetchInfiniteQuery', async () => {
67 const key = queryKey()
68 const options = infiniteQueryOptions({
69 queryKey: key,
70 queryFn: () => Promise.resolve('string'),
71 getNextPageParam: () => 1,
72 initialPageParam: 1,
73 })
74​
75 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
76 const data = await new QueryClient().fetchInfiniteQuery(options)
77​
78 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
79 })
80​
81 it('should work when passed to infiniteQuery', async () => {
82 const options = infiniteQueryOptions({
83 queryKey: ['key'],
84 queryFn: () => Promise.resolve('string'),
85 getNextPageParam: () => 1,
86 initialPageParam: 1,
87 })
88​
89 const data = await new QueryClient().infiniteQuery(options)
90​
91 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
92 })
93​
94 it('should work when passed to infiniteQuery with select', async () => {
95 const options = infiniteQueryOptions({
96 queryKey: ['key'],
97 queryFn: () => Promise.resolve('string'),
98 getNextPageParam: () => 1,
99 initialPageParam: 1,
100 select: (data) => data.pages,
101 })
102​
103 const data = await new QueryClient().infiniteQuery(options)
104​
105 expectTypeOf(data).toEqualTypeOf<Array<string>>()
106 })
107​
108 it('should work when passed to infiniteQuery with enabled: false', async () => {
109 const options = infiniteQueryOptions({
110 queryKey: ['key'],
111 queryFn: () => Promise.resolve('string'),
112 getNextPageParam: () => 1,
113 initialPageParam: 1,
114 enabled: false,
115 })
116​
117 const data = await new QueryClient().infiniteQuery(options)
118​
119 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
120 })
121​
122 it('should work when passed to infiniteQuery with skipToken', async () => {
123 const options = infiniteQueryOptions({
124 queryKey: ['key'],
125 queryFn: skipToken,
126 getNextPageParam: () => 1,
127 initialPageParam: 1,
128 })
129​
130 const data = await new QueryClient().infiniteQuery(options)
131​
132 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
133 })
134​
135 it('should tag the queryKey with the result type of the QueryFn', () => {
136 const key = queryKey()
137 const { queryKey: tagged } = infiniteQueryOptions({
138 queryKey: key,
139 queryFn: () => Promise.resolve('string'),
140 getNextPageParam: () => 1,
141 initialPageParam: 1,
142 })
143​
144 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
145 })
146 it('should tag the queryKey even if no promise is returned', () => {
147 const key = queryKey()
148 const { queryKey: tagged } = infiniteQueryOptions({
149 queryKey: key,
150 queryFn: () => 'string',
151 getNextPageParam: () => 1,
152 initialPageParam: 1,
153 })
154​
155 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
156 })
157 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
158 const key = queryKey()
159 const { queryKey: tagged } = infiniteQueryOptions({
160 queryKey: key,
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 key = queryKey()
171 const { queryKey: tagged } = infiniteQueryOptions({
172 queryKey: key,
173 queryFn: () => Promise.resolve('string'),
174 getNextPageParam: () => 1,
175 initialPageParam: 1,
176 })
177​
178 const queryClient = new QueryClient()
179 const data = queryClient.getQueryData(tagged)
180​
181 expectTypeOf(data).toEqualTypeOf<
182 InfiniteData<string, unknown> | undefined
183 >()
184 })
185 it('should properly type when passed to setQueryData', () => {
186 const key = queryKey()
187 const { queryKey: tagged } = infiniteQueryOptions({
188 queryKey: key,
189 queryFn: () => Promise.resolve('string'),
190 getNextPageParam: () => 1,
191 initialPageParam: 1,
192 })
193​
194 const queryClient = new QueryClient()
195 const data = queryClient.setQueryData(tagged, (prev) => {
196 expectTypeOf(prev).toEqualTypeOf<
197 InfiniteData<string, unknown> | undefined
198 >()
199 return prev
200 })
201​
202 expectTypeOf(data).toEqualTypeOf<
203 InfiniteData<string, unknown> | undefined
204 >()
205 })
206​
207 it('should not be allowed to be passed to non-infinite query functions', () => {
208 const key = queryKey()
209 const queryClient = new QueryClient()
210 const options = infiniteQueryOptions({
211 queryKey: key,
212 queryFn: () => Promise.resolve('string'),
213 getNextPageParam: () => 1,
214 initialPageParam: 1,
215 })
216 assertType(
217 // @ts-expect-error cannot pass infinite options to non-infinite query functions
218 injectQuery(() => options),
219 )
220 assertType(
221 // @ts-expect-error cannot pass infinite options to non-infinite query functions
222 queryClient.query(options),
223 )
224​
225 // deprecated methods below to be removed next major version
226 assertType(
227 // @ts-expect-error cannot pass infinite options to non-infinite query functions
228 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
229 queryClient.ensureQueryData(options),
230 )
231 assertType(
232 // @ts-expect-error cannot pass infinite options to non-infinite query functions
233 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
234 queryClient.fetchQuery(options),
235 )
236 assertType(
237 // @ts-expect-error cannot pass infinite options to non-infinite query functions
238 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
239 queryClient.prefetchQuery(options),
240 )
241 })
242​
243 it('should allow optional initialData function', () => {
244 const key = queryKey()
245 const initialData: { example: boolean } | undefined = { example: true }
246 const queryOptions = infiniteQueryOptions({
247 queryKey: key,
248 queryFn: () => initialData,
249 initialData: initialData
250 ? () => ({ pages: [initialData], pageParams: [] })
251 : undefined,
252 getNextPageParam: () => 1,
253 initialPageParam: 1,
254 })
255 expectTypeOf(queryOptions.initialData).toExtend<
256 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
257 | InfiniteData<{ example: boolean }, number>
258 | undefined
259 >()
260 })
261​
262 it('should allow optional initialData object', () => {
263 const key = queryKey()
264 const initialData: { example: boolean } | undefined = { example: true }
265 const queryOptions = infiniteQueryOptions({
266 queryKey: key,
267 queryFn: () => initialData,
268 initialData: initialData
269 ? { pages: [initialData], pageParams: [] }
270 : undefined,
271 getNextPageParam: () => 1,
272 initialPageParam: 1,
273 })
274 expectTypeOf(queryOptions.initialData).toExtend<
275 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
276 | InfiniteData<{ example: boolean }, number>
277 | undefined
278 >()
279 })
280​
281 it('should return a custom query key type', () => {
282 type MyQueryKey = [Array<string>, { type: 'foo' }]
283​
284 const options = infiniteQueryOptions({
285 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
286 queryFn: () => Promise.resolve(1),
287 getNextPageParam: () => 1,
288 initialPageParam: 1,
289 })
290​
291 expectTypeOf(options.queryKey).toEqualTypeOf<
292 DataTag<MyQueryKey, InfiniteData<number>, Error>
293 >()
294 })
295​
296 it('should return a custom query key type with datatag', () => {
297 type MyQueryKey = DataTag<
298 [Array<string>, { type: 'foo' }],
299 number,
300 Error & { myMessage: string }
301 >
302​
303 const options = infiniteQueryOptions({
304 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
305 queryFn: () => Promise.resolve(1),
306 getNextPageParam: () => 1,
307 initialPageParam: 1,
308 })
309​
310 expectTypeOf(options.queryKey).toEqualTypeOf<
311 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
312 >()
313 })
314})