Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import {
3 QueriesObserver,
4 QueryClient,
5 dataTagSymbol,
6 skipToken,
7} from '@tanstack/query-core'
8import { queryKey } from '@tanstack/query-test-utils'
9import { queryOptions } from '../queryOptions'
10import { useQuery } from '../useQuery'
11import { useQueries } from '../useQueries'
12import { useSuspenseQuery } from '../useSuspenseQuery'
13import type { AnyUseQueryOptions } from '../types'
14import type {
15 DataTag,
16 InitialDataFunction,
17 QueryObserverResult,
18 QueryPersister,
19} from '@tanstack/query-core'
20β
21describe('queryOptions', () => {
22 it('should not allow excess properties', () => {
23 assertType(
24 queryOptions({
25 queryKey: queryKey(),
26 queryFn: () => Promise.resolve(5),
27 // @ts-expect-error this is a good error, because stallTime does not exist!
28 stallTime: 1000,
29 }),
30 )
31 })
32 it('should infer types for callbacks', () => {
33 queryOptions({
34 queryKey: queryKey(),
35 queryFn: () => Promise.resolve(5),
36 staleTime: 1000,
37 select: (data) => {
38 expectTypeOf(data).toEqualTypeOf<number>()
39 },
40 })
41 })
42 it('should work when passed to useQuery', () => {
43 const options = queryOptions({
44 queryKey: queryKey(),
45 queryFn: () => Promise.resolve(5),
46 })
47β
48 const { data } = useQuery(options)
49 expectTypeOf(data).toEqualTypeOf<number | undefined>()
50 })
51 it('should work when passed to useSuspenseQuery', () => {
52 const options = queryOptions({
53 queryKey: queryKey(),
54 queryFn: () => Promise.resolve(5),
55 })
56β
57 const { data } = useSuspenseQuery(options)
58 expectTypeOf(data).toEqualTypeOf<number>()
59 })
60 it('should work when passed to query', async () => {
61 const options = queryOptions({
62 queryKey: ['key'],
63 queryFn: () => Promise.resolve(5),
64 })
65β
66 const data = await new QueryClient().query(options)
67 expectTypeOf(data).toEqualTypeOf<number>()
68 })
69β
70 it('should work when passed to fetchQuery', async () => {
71 const options = queryOptions({
72 queryKey: queryKey(),
73 queryFn: () => Promise.resolve(5),
74 })
75β
76 const data = await new QueryClient().fetchQuery(options)
77 expectTypeOf(data).toEqualTypeOf<number>()
78 })
79 it('should work when passed to query with select', async () => {
80 const options = queryOptions({
81 queryKey: ['key'],
82 queryFn: () => Promise.resolve(5),
83 select: (data) => data.toString(),
84 })
85β
86 const data = await new QueryClient().query(options)
87 expectTypeOf(data).toEqualTypeOf<string>()
88 })
89 it('should work when passed to query with enabled: false', async () => {
90 const options = queryOptions({
91 queryKey: ['key'],
92 queryFn: () => Promise.resolve(5),
93 enabled: false,
94 })
95β
96 const data = await new QueryClient().query(options)
97 expectTypeOf(data).toEqualTypeOf<number>()
98 })
99 it('should work when passed to query with skipToken', async () => {
100 const options = queryOptions({
101 queryKey: ['key'],
102 queryFn: skipToken,
103 })
104β
105 const data = await new QueryClient().query(options)
106 expectTypeOf(data).toEqualTypeOf<unknown>()
107 })
108 it('should work when passed to useQueries', () => {
109 const options = queryOptions({
110 queryKey: queryKey(),
111 queryFn: () => Promise.resolve(5),
112 })
113β
114 const [{ data }] = useQueries({
115 queries: [options],
116 })
117β
118 expectTypeOf(data).toEqualTypeOf<number | undefined>()
119 })
120 it('should tag the queryKey with the result type of the QueryFn', () => {
121 const { queryKey: tagged } = queryOptions({
122 queryKey: queryKey(),
123 queryFn: () => Promise.resolve(5),
124 })
125β
126 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
127 })
128 it('should tag the queryKey even if no promise is returned', () => {
129 const { queryKey: tagged } = queryOptions({
130 queryKey: queryKey(),
131 queryFn: () => 5,
132 })
133β
134 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
135 })
136 it('should tag the queryKey with unknown if there is no queryFn', () => {
137 const { queryKey: tagged } = queryOptions({
138 queryKey: queryKey(),
139 })
140β
141 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
142 })
143 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
144 const { queryKey: tagged } = queryOptions({
145 queryKey: queryKey(),
146 queryFn: () => Promise.resolve(5),
147 select: (data) => data.toString(),
148 })
149β
150 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
151 })
152 it('should return the proper type when passed to getQueryData', () => {
153 const { queryKey: tagged } = queryOptions({
154 queryKey: queryKey(),
155 queryFn: () => Promise.resolve(5),
156 })
157β
158 const queryClient = new QueryClient()
159 const data = queryClient.getQueryData(tagged)
160 expectTypeOf(data).toEqualTypeOf<number | undefined>()
161 })
162 it('should return the proper type when passed to getQueryState', () => {
163 const { queryKey: tagged } = queryOptions({
164 queryKey: queryKey(),
165 queryFn: () => Promise.resolve(5),
166 })
167β
168 const queryClient = new QueryClient()
169 const state = queryClient.getQueryState(tagged)
170 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
171 })
172 it('should properly type updaterFn when passed to setQueryData', () => {
173 const { queryKey: tagged } = queryOptions({
174 queryKey: queryKey(),
175 queryFn: () => Promise.resolve(5),
176 })
177β
178 const queryClient = new QueryClient()
179 const data = queryClient.setQueryData(tagged, (prev) => {
180 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
181 return prev
182 })
183 expectTypeOf(data).toEqualTypeOf<number | undefined>()
184 })
185 it('should properly type value when passed to setQueryData', () => {
186 const { queryKey: tagged } = queryOptions({
187 queryKey: queryKey(),
188 queryFn: () => Promise.resolve(5),
189 })
190β
191 const queryClient = new QueryClient()
192β
193 // @ts-expect-error value should be a number
194 queryClient.setQueryData(tagged, '5')
195 // @ts-expect-error value should be a number
196 queryClient.setQueryData(tagged, () => '5')
197β
198 const data = queryClient.setQueryData(tagged, 5)
199 expectTypeOf(data).toEqualTypeOf<number | undefined>()
200 })
201β
202 it('should infer even if there is a conditional skipToken', () => {
203 const options = queryOptions({
204 queryKey: queryKey(),
205 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
206 })
207β
208 const queryClient = new QueryClient()
209 const data = queryClient.getQueryData(options.queryKey)
210 expectTypeOf(data).toEqualTypeOf<number | undefined>()
211 })
212β
213 it('should infer to unknown if we disable a query with just a skipToken', () => {
214 const options = queryOptions({
215 queryKey: queryKey(),
216 queryFn: skipToken,
217 })
218β
219 const queryClient = new QueryClient()
220 const data = queryClient.getQueryData(options.queryKey)
221 expectTypeOf(data).toEqualTypeOf<unknown>()
222 })
223β
224 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
225 const options = queryOptions({
226 queryKey: queryKey(),
227 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
228 })
229 // @ts-expect-error TS2345
230 const { data } = useSuspenseQuery(options)
231 expectTypeOf(data).toEqualTypeOf<number>()
232 })
233β
234 it('should return the proper type when passed to QueriesObserver', () => {
235 const options = queryOptions({
236 queryKey: queryKey(),
237 queryFn: () => Promise.resolve(5),
238 })
239β
240 const queryClient = new QueryClient()
241 const queriesObserver = new QueriesObserver(queryClient, [options])
242 expectTypeOf(queriesObserver).toEqualTypeOf<
243 QueriesObserver<Array<QueryObserverResult>>
244 >()
245 })
246β
247 it('should allow undefined response in initialData', () => {
248 assertType((id: string | null) =>
249 queryOptions({
250 queryKey: ['todo', id],
251 queryFn: () =>
252 Promise.resolve({
253 id: '1',
254 title: 'Do Laundry',
255 }),
256 initialData: () =>
257 !id
258 ? undefined
259 : {
260 id,
261 title: 'Initial Data',
262 },
263 }),
264 )
265 })
266β
267 it('should allow optional initialData object', () => {
268 const testFn = (id?: string) => {
269 const options = queryOptions({
270 queryKey: queryKey(),
271 queryFn: () => Promise.resolve('something string'),
272 initialData: id ? 'initial string' : undefined,
273 })
274 expectTypeOf(options.initialData).toExtend<
275 InitialDataFunction<string> | string | undefined
276 >()
277 }
278 testFn('id')
279 testFn()
280 })
281β
282 it('should be passable to UseQueryOptions', () => {
283 function somethingWithQueryOptions<TQueryOpts extends AnyUseQueryOptions>(
284 options: TQueryOpts,
285 ) {
286 return options.queryKey
287 }
288β
289 const options = queryOptions({
290 queryKey: queryKey(),
291 queryFn: () => Promise.resolve(1),
292 })
293β
294 assertType(somethingWithQueryOptions(options))
295 })
296β
297 it('should return a custom query key type', () => {
298 type MyQueryKey = [Array<string>, { type: 'foo' }]
299β
300 const options = queryOptions({
301 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
302 queryFn: () => Promise.resolve(1),
303 })
304β
305 expectTypeOf(options.queryKey).toEqualTypeOf<
306 DataTag<MyQueryKey, number, Error>
307 >()
308 })
309β
310 it('should return a custom query key type with datatag', () => {
311 type MyQueryKey = DataTag<
312 [Array<string>, { type: 'foo' }],
313 number,
314 Error & { myMessage: string }
315 >
316β
317 const options = queryOptions({
318 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
319 queryFn: () => Promise.resolve(1),
320 })
321β
322 expectTypeOf(options.queryKey).toEqualTypeOf<
323 DataTag<MyQueryKey, number, Error & { myMessage: string }>
324 >()
325 })
326β
327 it('should infer TQueryFnData from persister paired with a queryFn declaring a parameter (#7842)', () => {
328 const persister = undefined as unknown as QueryPersister<string, any>
329β
330 const options = queryOptions({
331 queryKey: ['key'],
332 queryFn: (_context) => 'hello',
333 persister,
334 })
335β
336 expectTypeOf(options.queryFn!).returns.toEqualTypeOf<
337 string | Promise<string>
338 >()
339 })
340β
341 it('should still error when persister and queryFn return types genuinely conflict', () => {
342 const persister = undefined as unknown as QueryPersister<string, any>
343β
344 assertType(
345 queryOptions({
346 queryKey: ['key'],
347 // @ts-expect-error persister expects string, queryFn returns number
348 queryFn: () => 42,
349 persister,
350 }),
351 )
352β
353 assertType(
354 queryOptions({
355 queryKey: ['key'],
356 // @ts-expect-error persister expects string, queryFn with arg returns number
357 queryFn: (_context) => 42,
358 persister,
359 }),
360 )
361 })
362})