Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { computed, reactive, ref } from 'vue-demi'
3import { dataTagSymbol } 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 const data2 = queryClient.fetchQuery(options)
166β
167 expectTypeOf(data).toEqualTypeOf<Promise<void>>()
168 expectTypeOf(data2).toEqualTypeOf<Promise<number>>()
169 })
170β
171 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
172 const key = queryKey()
173 const { data } = reactive(
174 useQuery(
175 queryOptions({
176 queryKey: key,
177 queryFn: () => {
178 return {
179 wow: true,
180 }
181 },
182 initialData: () => ({
183 wow: true,
184 }),
185 }),
186 ),
187 )
188β
189 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
190 })
191β
192 it('TData should have undefined in the union when initialData is NOT provided', () => {
193 const key = queryKey()
194 const { data } = reactive(
195 useQuery(
196 queryOptions({
197 queryKey: key,
198 queryFn: () => {
199 return {
200 wow: true,
201 }
202 },
203 }),
204 ),
205 )
206β
207 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
208 })
209β
210 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
211 const key = queryKey()
212 const { data } = reactive(
213 useQuery(
214 queryOptions({
215 queryKey: key,
216 queryFn: () => {
217 return {
218 wow: true,
219 }
220 },
221 initialData: () => undefined as { wow: boolean } | undefined,
222 }),
223 ),
224 )
225β
226 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
227 })
228β
229 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
230 const key = queryKey()
231 const { data, isSuccess } = reactive(
232 useQuery(
233 queryOptions({
234 queryKey: key,
235 queryFn: () => {
236 return {
237 wow: true,
238 }
239 },
240 initialData: () => undefined as { wow: boolean } | undefined,
241 }),
242 ),
243 )
244β
245 if (isSuccess) {
246 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
247 }
248 })
249β
250 it('data should not have undefined when initialData is provided', () => {
251 const key = queryKey()
252 const { data } = reactive(
253 useQuery(
254 queryOptions({
255 queryKey: key,
256 initialData: 42,
257 }),
258 ),
259 )
260β
261 expectTypeOf(data).toEqualTypeOf<number>()
262 })
263β
264 it('should allow accessing queryFn and other properties on the returned options object', () => {
265 const options = queryOptions({
266 queryKey: queryKey(),
267 queryFn: () => Promise.resolve([]),
268 })
269β
270 expectTypeOf(options.queryFn).not.toBeUndefined()
271 expectTypeOf(options.queryKey).not.toBeUndefined()
272 expectTypeOf(options.staleTime).not.toBeUndefined()
273 })
274β
275 it('should allow accessing queryFn and other properties on the returned options when used with getter', () => {
276 const options = queryOptions(() => ({
277 queryKey: queryKey(),
278 queryFn: () => Promise.resolve([]),
279 }))
280β
281 const resolvedGetter = options()
282β
283 expectTypeOf(resolvedGetter.queryFn).not.toBeUndefined()
284 expectTypeOf(resolvedGetter.queryKey).not.toBeUndefined()
285 })
286β
287 it('should allow computed ref as enabled property', () => {
288 const enabled = computed(() => true)
289β
290 // This was broken in #10452, fixed in #10458
291 const options = queryOptions({
292 queryKey: queryKey(),
293 queryFn: () => Promise.resolve(1),
294 enabled,
295 })
296β
297 expectTypeOf(options.queryKey).not.toBeUndefined()
298 })
299β
300 it('should allow ref as enabled property', () => {
301 const enabled = ref(true)
302β
303 const options = queryOptions({
304 queryKey: queryKey(),
305 queryFn: () => Promise.resolve(1),
306 enabled,
307 })
308β
309 expectTypeOf(options.queryKey).not.toBeUndefined()
310 })
311β
312 it('should allow boolean as enabled property', () => {
313 const options = queryOptions({
314 queryKey: queryKey(),
315 queryFn: () => Promise.resolve(1),
316 enabled: true,
317 })
318β
319 expectTypeOf(options.queryKey).not.toBeUndefined()
320 })
321β
322 it('should allow getter function as enabled property', () => {
323 const options = queryOptions({
324 queryKey: queryKey(),
325 queryFn: () => Promise.resolve(1),
326 enabled: () => true,
327 })
328β
329 expectTypeOf(options.queryKey).not.toBeUndefined()
330 })
331β
332 it('should allow computed ref as queryKey', () => {
333 const id = ref<string | null>('1')
334β
335 // This was broken in #10452, the #10465 fix only covered `enabled`
336 const options = queryOptions({
337 queryKey: computed(() => ['foo', id.value] as const),
338 queryFn: () => Promise.resolve({ id: '1' }),
339 })
340β
341 expectTypeOf(options.queryKey).not.toBeUndefined()
342 })
343β
344 it('should allow ref as queryKey', () => {
345 const keyRef = ref(['foo', '1'] as const)
346β
347 const options = queryOptions({
348 queryKey: keyRef,
349 queryFn: () => Promise.resolve({ id: '1' }),
350 })
351β
352 expectTypeOf(options.queryKey).not.toBeUndefined()
353 })
354β
355 it('should allow getter function as queryKey', () => {
356 const id = ref<string | null>('1')
357β
358 const options = queryOptions({
359 queryKey: () => ['foo', id.value] as const,
360 queryFn: () => Promise.resolve({ id: '1' }),
361 })
362β
363 expectTypeOf(options.queryKey).not.toBeUndefined()
364 })
365})