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

queryOptions.test-d.ts

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