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

queryOptions.test-d.tsx

324 lines9.5 KB

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​
61 it('should work when passed to fetchQuery', async () => {
62 const options = queryOptions({
63 queryKey: queryKey(),
64 queryFn: () => Promise.resolve(5),
65 })
66​
67 const data = await new QueryClient().fetchQuery(options)
68 expectTypeOf(data).toEqualTypeOf<number>()
69 })
70 it('should work when passed to useQueries', () => {
71 const options = queryOptions({
72 queryKey: queryKey(),
73 queryFn: () => Promise.resolve(5),
74 })
75​
76 const [{ data }] = useQueries({
77 queries: [options],
78 })
79​
80 expectTypeOf(data).toEqualTypeOf<number | undefined>()
81 })
82 it('should tag the queryKey with the result type of the QueryFn', () => {
83 const { queryKey: tagged } = queryOptions({
84 queryKey: queryKey(),
85 queryFn: () => Promise.resolve(5),
86 })
87​
88 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
89 })
90 it('should tag the queryKey even if no promise is returned', () => {
91 const { queryKey: tagged } = queryOptions({
92 queryKey: queryKey(),
93 queryFn: () => 5,
94 })
95​
96 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
97 })
98 it('should tag the queryKey with unknown if there is no queryFn', () => {
99 const { queryKey: tagged } = queryOptions({
100 queryKey: queryKey(),
101 })
102​
103 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
104 })
105 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
106 const { queryKey: tagged } = queryOptions({
107 queryKey: queryKey(),
108 queryFn: () => Promise.resolve(5),
109 select: (data) => data.toString(),
110 })
111​
112 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
113 })
114 it('should return the proper type when passed to getQueryData', () => {
115 const { queryKey: tagged } = queryOptions({
116 queryKey: queryKey(),
117 queryFn: () => Promise.resolve(5),
118 })
119​
120 const queryClient = new QueryClient()
121 const data = queryClient.getQueryData(tagged)
122 expectTypeOf(data).toEqualTypeOf<number | undefined>()
123 })
124 it('should return the proper type when passed to getQueryState', () => {
125 const { queryKey: tagged } = queryOptions({
126 queryKey: queryKey(),
127 queryFn: () => Promise.resolve(5),
128 })
129​
130 const queryClient = new QueryClient()
131 const state = queryClient.getQueryState(tagged)
132 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
133 })
134 it('should properly type updaterFn when passed to setQueryData', () => {
135 const { queryKey: tagged } = queryOptions({
136 queryKey: queryKey(),
137 queryFn: () => Promise.resolve(5),
138 })
139​
140 const queryClient = new QueryClient()
141 const data = queryClient.setQueryData(tagged, (prev) => {
142 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
143 return prev
144 })
145 expectTypeOf(data).toEqualTypeOf<number | undefined>()
146 })
147 it('should properly type value when passed to setQueryData', () => {
148 const { queryKey: tagged } = queryOptions({
149 queryKey: queryKey(),
150 queryFn: () => Promise.resolve(5),
151 })
152​
153 const queryClient = new QueryClient()
154​
155 // @ts-expect-error value should be a number
156 queryClient.setQueryData(tagged, '5')
157 // @ts-expect-error value should be a number
158 queryClient.setQueryData(tagged, () => '5')
159​
160 const data = queryClient.setQueryData(tagged, 5)
161 expectTypeOf(data).toEqualTypeOf<number | undefined>()
162 })
163​
164 it('should infer even if there is a conditional skipToken', () => {
165 const options = queryOptions({
166 queryKey: queryKey(),
167 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
168 })
169​
170 const queryClient = new QueryClient()
171 const data = queryClient.getQueryData(options.queryKey)
172 expectTypeOf(data).toEqualTypeOf<number | undefined>()
173 })
174​
175 it('should infer to unknown if we disable a query with just a skipToken', () => {
176 const options = queryOptions({
177 queryKey: queryKey(),
178 queryFn: skipToken,
179 })
180​
181 const queryClient = new QueryClient()
182 const data = queryClient.getQueryData(options.queryKey)
183 expectTypeOf(data).toEqualTypeOf<unknown>()
184 })
185​
186 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
187 const options = queryOptions({
188 queryKey: queryKey(),
189 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
190 })
191 // @ts-expect-error TS2345
192 const { data } = useSuspenseQuery(options)
193 expectTypeOf(data).toEqualTypeOf<number>()
194 })
195​
196 it('should return the proper type when passed to QueriesObserver', () => {
197 const options = queryOptions({
198 queryKey: queryKey(),
199 queryFn: () => Promise.resolve(5),
200 })
201​
202 const queryClient = new QueryClient()
203 const queriesObserver = new QueriesObserver(queryClient, [options])
204 expectTypeOf(queriesObserver).toEqualTypeOf<
205 QueriesObserver<Array<QueryObserverResult>>
206 >()
207 })
208​
209 it('should allow undefined response in initialData', () => {
210 assertType((id: string | null) =>
211 queryOptions({
212 queryKey: ['todo', id],
213 queryFn: () =>
214 Promise.resolve({
215 id: '1',
216 title: 'Do Laundry',
217 }),
218 initialData: () =>
219 !id
220 ? undefined
221 : {
222 id,
223 title: 'Initial Data',
224 },
225 }),
226 )
227 })
228​
229 it('should allow optional initialData object', () => {
230 const testFn = (id?: string) => {
231 const options = queryOptions({
232 queryKey: queryKey(),
233 queryFn: () => Promise.resolve('something string'),
234 initialData: id ? 'initial string' : undefined,
235 })
236 expectTypeOf(options.initialData).toExtend<
237 InitialDataFunction<string> | string | undefined
238 >()
239 }
240 testFn('id')
241 testFn()
242 })
243​
244 it('should be passable to UseQueryOptions', () => {
245 function somethingWithQueryOptions<TQueryOpts extends AnyUseQueryOptions>(
246 options: TQueryOpts,
247 ) {
248 return options.queryKey
249 }
250​
251 const options = queryOptions({
252 queryKey: queryKey(),
253 queryFn: () => Promise.resolve(1),
254 })
255​
256 assertType(somethingWithQueryOptions(options))
257 })
258​
259 it('should return a custom query key type', () => {
260 type MyQueryKey = [Array<string>, { type: 'foo' }]
261​
262 const options = queryOptions({
263 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
264 queryFn: () => Promise.resolve(1),
265 })
266​
267 expectTypeOf(options.queryKey).toEqualTypeOf<
268 DataTag<MyQueryKey, number, Error>
269 >()
270 })
271​
272 it('should return a custom query key type with datatag', () => {
273 type MyQueryKey = DataTag<
274 [Array<string>, { type: 'foo' }],
275 number,
276 Error & { myMessage: string }
277 >
278​
279 const options = queryOptions({
280 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
281 queryFn: () => Promise.resolve(1),
282 })
283​
284 expectTypeOf(options.queryKey).toEqualTypeOf<
285 DataTag<MyQueryKey, number, Error & { myMessage: string }>
286 >()
287 })
288​
289 it('should infer TQueryFnData from persister paired with a queryFn declaring a parameter (#7842)', () => {
290 const persister = undefined as unknown as QueryPersister<string, any>
291​
292 const options = queryOptions({
293 queryKey: ['key'],
294 queryFn: (_context) => 'hello',
295 persister,
296 })
297​
298 expectTypeOf(options.queryFn!).returns.toEqualTypeOf<
299 string | Promise<string>
300 >()
301 })
302​
303 it('should still error when persister and queryFn return types genuinely conflict', () => {
304 const persister = undefined as unknown as QueryPersister<string, any>
305​
306 assertType(
307 queryOptions({
308 queryKey: ['key'],
309 // @ts-expect-error persister expects string, queryFn returns number
310 queryFn: () => 42,
311 persister,
312 }),
313 )
314​
315 assertType(
316 queryOptions({
317 queryKey: ['key'],
318 // @ts-expect-error persister expects string, queryFn with arg returns number
319 queryFn: (_context) => 42,
320 persister,
321 }),
322 )
323 })
324})