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

queryOptions.test-d.ts

410 lines11.8 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { computed, reactive, ref } from 'vue-demi'
3import { dataTagSymbol, skipToken } from '@tanstack/query-core'
4import { queryKey } from '@tanstack/query-test-utils'
5import { QueryClient } from '../queryClient'
6import { queryOptions } from '../queryOptions'
7import { useQuery } from '../useQuery'
8import type { InitialDataFunction } from '@tanstack/query-core'
9​
10// Regression test for exported queryOptions inference under declaration emit.
11// TypeScript should be able to name the return type without expanding the
12// internal data tag symbols into the consumer's .d.ts output.
13export const exportedQueryOptions = queryOptions({
14 queryKey: ['invalid'],
15})
16​
17export const exportedQueryOptionsGetter = queryOptions(() => ({
18 queryKey: ['invalid'],
19}))
20​
21describe('queryOptions', () => {
22 it('should not allow excess properties', () => {
23 const key = queryKey()
24 assertType(
25 queryOptions({
26 // @ts-expect-error this is a good error, because stallTime does not exist!
27 queryKey: key,
28 queryFn: () => Promise.resolve(5),
29 stallTime: 1000,
30 }),
31 )
32 })
33 it('should infer types for callbacks', () => {
34 const key = queryKey()
35 queryOptions({
36 queryKey: key,
37 queryFn: () => Promise.resolve(5),
38 staleTime: 1000,
39 select: (data) => {
40 expectTypeOf(data).toEqualTypeOf<number>()
41 },
42 })
43 })
44 it('should work when passed to useQuery', () => {
45 const key = queryKey()
46 const options = queryOptions({
47 queryKey: key,
48 queryFn: () => Promise.resolve(5),
49 })
50​
51 const { data } = reactive(useQuery(options))
52 expectTypeOf(data).toEqualTypeOf<number | undefined>()
53 })
54 it('should work when passed to query', async () => {
55 const options = queryOptions({
56 queryKey: ['key'],
57 queryFn: () => Promise.resolve(5),
58 })
59​
60 const data = await new QueryClient().query(options)
61 expectTypeOf(data).toEqualTypeOf<number>()
62 })
63 it('should work when passed to query with select', async () => {
64 const options = queryOptions({
65 queryKey: ['key'],
66 queryFn: () => Promise.resolve(5),
67 select: (data) => data.toString(),
68 })
69​
70 const data = await new QueryClient().query(options)
71 expectTypeOf(data).toEqualTypeOf<string>()
72 })
73 it('should tag the queryKey with the result type of the QueryFn', () => {
74 const key = queryKey()
75 const { queryKey: tagged } = queryOptions({
76 queryKey: key,
77 queryFn: () => Promise.resolve(5),
78 })
79​
80 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
81 })
82 it('should tag the queryKey even if no promise is returned', () => {
83 const key = queryKey()
84 const { queryKey: tagged } = queryOptions({
85 queryKey: key,
86 queryFn: () => 5,
87 })
88​
89 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
90 })
91 it('should tag the queryKey with unknown if there is no queryFn', () => {
92 const key = queryKey()
93 const { queryKey: tagged } = queryOptions({
94 queryKey: key,
95 })
96​
97 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
98 })
99 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
100 const key = queryKey()
101 const { queryKey: tagged } = queryOptions({
102 queryKey: key,
103 queryFn: () => Promise.resolve(5),
104 select: (data) => data.toString(),
105 })
106​
107 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
108 })
109 it('should return the proper type when passed to getQueryData', () => {
110 const key = queryKey()
111 const { queryKey: tagged } = queryOptions({
112 queryKey: key,
113 queryFn: () => Promise.resolve(5),
114 })
115​
116 const queryClient = new QueryClient()
117 const data = queryClient.getQueryData(tagged)
118​
119 expectTypeOf(data).toEqualTypeOf<number | undefined>()
120 })
121 it('should properly type updaterFn when passed to setQueryData', () => {
122 const key = queryKey()
123 const { queryKey: tagged } = queryOptions({
124 queryKey: key,
125 queryFn: () => Promise.resolve(5),
126 })
127​
128 const queryClient = new QueryClient()
129 const data = queryClient.setQueryData(tagged, (prev) => {
130 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
131 return prev
132 })
133 expectTypeOf(data).toEqualTypeOf<number | undefined>()
134 })
135 it('should properly type value when passed to setQueryData', () => {
136 const key = queryKey()
137 const { queryKey: tagged } = queryOptions({
138 queryKey: key,
139 queryFn: () => Promise.resolve(5),
140 })
141​
142 const queryClient = new QueryClient()
143​
144 // @ts-expect-error value should be a number
145 queryClient.setQueryData(tagged, '5')
146 // @ts-expect-error value should be a number
147 queryClient.setQueryData(tagged, () => '5')
148​
149 const data = queryClient.setQueryData(tagged, 5)
150​
151 expectTypeOf(data).toEqualTypeOf<number | undefined>()
152 })
153 it('should allow to be passed to QueryClient methods while containing getter', () => {
154 const ref1 = ref(1)
155 const ref2 = ref(2)
156 const options = queryOptions(() => ({
157 queryKey: [...queryKey(), ref1.value, { nested: ref2.value }],
158 queryFn: () => Promise.resolve(5),
159 }))
160​
161 const queryClient = new QueryClient()
162​
163 // Should not error
164 const data = queryClient.invalidateQueries(options)
165 // Should not error
166 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
167 const data2 = queryClient.fetchQuery(options)
168​
169 expectTypeOf(data).toEqualTypeOf<Promise<void>>()
170 expectTypeOf(data2).toEqualTypeOf<Promise<number>>()
171 })
172​
173 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
174 const key = queryKey()
175 const { data } = reactive(
176 useQuery(
177 queryOptions({
178 queryKey: key,
179 queryFn: () => {
180 return {
181 wow: true,
182 }
183 },
184 initialData: () => ({
185 wow: true,
186 }),
187 }),
188 ),
189 )
190​
191 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
192 })
193​
194 it('TData should have undefined in the union when initialData is NOT provided', () => {
195 const key = queryKey()
196 const { data } = reactive(
197 useQuery(
198 queryOptions({
199 queryKey: key,
200 queryFn: () => {
201 return {
202 wow: true,
203 }
204 },
205 }),
206 ),
207 )
208​
209 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
210 })
211​
212 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
213 const key = queryKey()
214 const { data } = reactive(
215 useQuery(
216 queryOptions({
217 queryKey: key,
218 queryFn: () => {
219 return {
220 wow: true,
221 }
222 },
223 initialData: () => undefined as { wow: boolean } | undefined,
224 }),
225 ),
226 )
227​
228 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
229 })
230​
231 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
232 const key = queryKey()
233 const { data, isSuccess } = reactive(
234 useQuery(
235 queryOptions({
236 queryKey: key,
237 queryFn: () => {
238 return {
239 wow: true,
240 }
241 },
242 initialData: () => undefined as { wow: boolean } | undefined,
243 }),
244 ),
245 )
246​
247 if (isSuccess) {
248 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
249 }
250 })
251​
252 it('data should not have undefined when initialData is provided', () => {
253 const key = queryKey()
254 const { data } = reactive(
255 useQuery(
256 queryOptions({
257 queryKey: key,
258 initialData: 42,
259 }),
260 ),
261 )
262​
263 expectTypeOf(data).toEqualTypeOf<number>()
264 })
265​
266 it('should allow optional initialData object', () => {
267 const options = queryOptions({
268 queryKey: queryKey(),
269 queryFn: () => Promise.resolve('something string'),
270 initialData: Math.random() > 0.5 ? 'initial string' : undefined,
271 })
272​
273 expectTypeOf(options.initialData).toExtend<
274 InitialDataFunction<string> | string | undefined
275 >()
276 })
277​
278 it('should allow accessing queryFn and other properties on the returned options object', () => {
279 const options = queryOptions({
280 queryKey: queryKey(),
281 queryFn: () => Promise.resolve([]),
282 })
283​
284 expectTypeOf(options.queryFn).not.toBeUndefined()
285 expectTypeOf(options.queryKey).not.toBeUndefined()
286 expectTypeOf(options.staleTime).not.toBeUndefined()
287 })
288​
289 it('should allow accessing queryFn and other properties on the returned options when used with getter', () => {
290 const options = queryOptions(() => ({
291 queryKey: queryKey(),
292 queryFn: () => Promise.resolve([]),
293 }))
294​
295 const resolvedGetter = options()
296​
297 expectTypeOf(resolvedGetter.queryFn).not.toBeUndefined()
298 expectTypeOf(resolvedGetter.queryKey).not.toBeUndefined()
299 })
300​
301 it('should allow computed ref as enabled property', () => {
302 const enabled = computed(() => true)
303​
304 // This was broken in #10452, fixed in #10458
305 const options = queryOptions({
306 queryKey: queryKey(),
307 queryFn: () => Promise.resolve(1),
308 enabled,
309 })
310​
311 expectTypeOf(options.queryKey).not.toBeUndefined()
312 })
313​
314 it('should allow ref as enabled property', () => {
315 const enabled = ref(true)
316​
317 const options = queryOptions({
318 queryKey: queryKey(),
319 queryFn: () => Promise.resolve(1),
320 enabled,
321 })
322​
323 expectTypeOf(options.queryKey).not.toBeUndefined()
324 })
325​
326 it('should allow boolean as enabled property', () => {
327 const options = queryOptions({
328 queryKey: queryKey(),
329 queryFn: () => Promise.resolve(1),
330 enabled: true,
331 })
332​
333 expectTypeOf(options.queryKey).not.toBeUndefined()
334 })
335​
336 it('should allow getter function as enabled property', () => {
337 const options = queryOptions({
338 queryKey: queryKey(),
339 queryFn: () => Promise.resolve(1),
340 enabled: () => true,
341 })
342​
343 expectTypeOf(options.queryKey).not.toBeUndefined()
344 })
345​
346 it('should allow computed ref as queryKey', () => {
347 const id = ref<string | null>('1')
348​
349 // This was broken in #10452, the #10465 fix only covered `enabled`
350 const options = queryOptions({
351 queryKey: computed(() => ['foo', id.value] as const),
352 queryFn: () => Promise.resolve({ id: '1' }),
353 })
354​
355 expectTypeOf(options.queryKey).not.toBeUndefined()
356 })
357​
358 it('should allow ref as queryKey', () => {
359 const keyRef = ref(['foo', '1'] as const)
360​
361 const options = queryOptions({
362 queryKey: keyRef,
363 queryFn: () => Promise.resolve({ id: '1' }),
364 })
365​
366 expectTypeOf(options.queryKey).not.toBeUndefined()
367 })
368​
369 it('should allow getter function as queryKey', () => {
370 const id = ref<string | null>('1')
371​
372 const options = queryOptions({
373 queryKey: () => ['foo', id.value] as const,
374 queryFn: () => Promise.resolve({ id: '1' }),
375 })
376​
377 expectTypeOf(options.queryKey).not.toBeUndefined()
378 })
379​
380 it('should allow computed queryFn resolving to skipToken', () => {
381 const id = ref<string | null>('1')
382​
383 const options = queryOptions({
384 queryKey: computed(() => ['foo', id.value]),
385 queryFn: computed(() =>
386 id.value ? () => Promise.resolve({ id: '1' }) : skipToken,
387 ),
388 })
389​
390 const { data } = reactive(useQuery(options))
391​
392 expectTypeOf(data).toEqualTypeOf<{ id: string } | undefined>()
393 })
394​
395 it('should allow skipToken inside a whole-options getter', () => {
396 const id = ref<string | null>('1')
397​
398 const options = queryOptions(() => {
399 const current = id.value
400 return {
401 queryKey: ['foo', current],
402 queryFn: current ? () => Promise.resolve({ id: current }) : skipToken,
403 }
404 })
405​
406 const { data } = reactive(useQuery(options))
407​
408 expectTypeOf(data).toEqualTypeOf<{ id: string } | undefined>()
409 })
410})