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

queryOptions.test-d.ts

338 lines9.7 KB

1import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
2import { createKey } 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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
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: createKey(),
218 queryFn: skipToken,
219 })
220​
221 const data = new QueryClient().getQueryData(options.queryKey)
222 expectTypeOf(data).toEqualTypeOf<unknown>()
223 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
224 })
225​
226 it('should allow undefined response in initialData', () => {
227 assertType((id: string | null) =>
228 queryOptions({
229 queryKey: ['todo', id],
230 queryFn: () =>
231 Promise.resolve({
232 id: '1',
233 title: 'Do Laundry',
234 }),
235 initialData: () =>
236 !id
237 ? undefined
238 : {
239 id,
240 title: 'Initial Data',
241 },
242 }),
243 )
244 })
245​
246 it('should allow optional initialData object', () => {
247 const options = queryOptions({
248 queryKey: createKey(),
249 queryFn: () => Promise.resolve('something string'),
250 initialData: Math.random() > 0.5 ? 'initial string' : undefined,
251 })
252​
253 expectTypeOf(options.initialData).toExtend<
254 InitialDataFunction<string> | string | undefined
255 >()
256 })
257​
258 it('should be passable to CreateQueryOptions', () => {
259 function somethingWithQueryOptions<
260 TQueryOpts extends CreateQueryOptions<any, any, any, any, any>,
261 >(options: TQueryOpts) {
262 return options.queryKey
263 }
264​
265 const options = queryOptions({
266 queryKey: createKey(),
267 queryFn: () => Promise.resolve(1),
268 })
269​
270 assertType(somethingWithQueryOptions(options))
271 })
272​
273 it('should return a custom query key type', () => {
274 type MyQueryKey = [Array<string>, { type: 'foo' }]
275​
276 const options = queryOptions({
277 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
278 queryFn: () => Promise.resolve(1),
279 })
280​
281 expectTypeOf(options.queryKey).toEqualTypeOf<
282 DataTag<MyQueryKey, number, Error>
283 >()
284 })
285​
286 it('should return a custom query key type with datatag', () => {
287 type MyQueryKey = DataTag<
288 [Array<string>, { type: 'foo' }],
289 number,
290 Error & { myMessage: string }
291 >
292​
293 const options = queryOptions({
294 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
295 queryFn: () => Promise.resolve(1),
296 })
297​
298 expectTypeOf(options.queryKey).toEqualTypeOf<
299 DataTag<MyQueryKey, number, Error & { myMessage: string }>
300 >()
301 })
302​
303 it('should infer TQueryFnData from persister paired with a queryFn declaring a parameter (#7842)', () => {
304 const persister = undefined as unknown as QueryPersister<string, any>
305​
306 const options = queryOptions({
307 queryKey: createKey(),
308 queryFn: (_context) => 'hello',
309 persister,
310 })
311​
312 expectTypeOf(options.queryFn!).returns.toEqualTypeOf<
313 string | Promise<string>
314 >()
315 })
316​
317 it('should still error when persister and queryFn return types genuinely conflict', () => {
318 const persister = undefined as unknown as QueryPersister<string, any>
319​
320 assertType(
321 queryOptions({
322 queryKey: createKey(),
323 // @ts-expect-error persister expects string, queryFn returns number
324 queryFn: () => 42,
325 persister,
326 }),
327 )
328​
329 assertType(
330 queryOptions({
331 queryKey: createKey(),
332 // @ts-expect-error persister expects string, queryFn with arg returns number
333 queryFn: (_context) => 42,
334 persister,
335 }),
336 )
337 })
338})