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

queryOptions.test-d.ts

337 lines9.6 KB

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