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

queryOptions.test-d.tsx

326 lines9.6 KB

1import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
2import { createKey } from '@tanstack/query-test-utils'
3import { assertType, describe, expectTypeOf, it } from 'vitest'
4​
5import { queryOptions } from '../queryOptions'
6import { useQueries } from '../useQueries'
7import { useQuery } from '../useQuery'
8import { useSuspenseQuery } from '../useSuspenseQuery'
9import type { AnyUseQueryOptions } from '../types'
10import type { DataTag, InitialDataFunction } from '@tanstack/query-core'
11​
12// Regression test for exported queryOptions inference under declaration emit.
13// TypeScript should be able to name the return type without expanding the
14// internal data tag symbols into the consumer's .d.ts output.
15export const exportedQueryOptions = queryOptions({
16 queryKey: ['invalid'],
17})
18​
19describe('queryOptions', () => {
20 it('should not allow excess properties', () => {
21 assertType(
22 queryOptions({
23 queryKey: createKey(),
24 queryFn: () => Promise.resolve(5),
25 // @ts-expect-error this is a good error, because stallTime does not exist!
26 stallTime: 1000,
27 }),
28 )
29 })
30​
31 it('should infer types for callbacks', () => {
32 queryOptions({
33 queryKey: createKey(),
34 queryFn: () => Promise.resolve(5),
35 staleTime: 1000,
36 select: (data) => {
37 expectTypeOf(data).toEqualTypeOf<number>()
38 },
39 })
40 })
41​
42 it('should work when passed to useQuery', () => {
43 const options = queryOptions({
44 queryKey: createKey(),
45 queryFn: () => Promise.resolve(5),
46 })
47​
48 const { data } = useQuery(options)
49 expectTypeOf(data).toEqualTypeOf<number | undefined>()
50 })
51​
52 it('should work when passed to useSuspenseQuery', () => {
53 const options = queryOptions({
54 queryKey: createKey(),
55 queryFn: () => Promise.resolve(5),
56 })
57​
58 const { data } = useSuspenseQuery(options)
59 expectTypeOf(data).toEqualTypeOf<number>()
60 })
61​
62 it('should work when passed to query', async () => {
63 const options = queryOptions({
64 queryKey: ['key'],
65 queryFn: () => Promise.resolve(5),
66 })
67​
68 const data = await new QueryClient().query(options)
69 expectTypeOf(data).toEqualTypeOf<number>()
70 })
71​
72 it('should work when passed to query with select', async () => {
73 const options = queryOptions({
74 queryKey: ['key'],
75 queryFn: () => Promise.resolve(5),
76 select: (data) => data.toString(),
77 })
78​
79 const data = await new QueryClient().query(options)
80 expectTypeOf(data).toEqualTypeOf<string>()
81 })
82​
83 it('should work when passed to query with enabled: false', async () => {
84 const options = queryOptions({
85 queryKey: ['key'],
86 queryFn: () => Promise.resolve(5),
87 enabled: false,
88 })
89​
90 const data = await new QueryClient().query(options)
91 expectTypeOf(data).toEqualTypeOf<number>()
92 })
93​
94 it('should work when passed to query with skipToken', async () => {
95 const options = queryOptions({
96 queryKey: ['key'],
97 queryFn: skipToken,
98 })
99​
100 const data = await new QueryClient().query(options)
101 expectTypeOf(data).toEqualTypeOf<unknown>()
102 })
103​
104 it('should work when passed to fetchQuery', async () => {
105 const options = queryOptions({
106 queryKey: createKey(),
107 queryFn: () => Promise.resolve(5),
108 })
109​
110 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
111 const data = await new QueryClient().fetchQuery(options)
112 expectTypeOf(data).toEqualTypeOf<number>()
113 })
114​
115 it('should work when passed to useQueries', () => {
116 const options = queryOptions({
117 queryKey: createKey(),
118 queryFn: () => Promise.resolve(5),
119 })
120​
121 const [{ data }] = useQueries({
122 queries: [options],
123 })
124​
125 expectTypeOf(data).toEqualTypeOf<number | undefined>()
126 })
127​
128 it('should tag the queryKey with the result type of the QueryFn', () => {
129 const { queryKey: tagged } = queryOptions({
130 queryKey: createKey(),
131 queryFn: () => Promise.resolve(5),
132 })
133​
134 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
135 })
136​
137 it('should tag the queryKey even if no promise is returned', () => {
138 const { queryKey: tagged } = queryOptions({
139 queryKey: createKey(),
140 queryFn: () => 5,
141 })
142​
143 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
144 })
145​
146 it('should tag the queryKey with unknown if there is no queryFn', () => {
147 const { queryKey: tagged } = queryOptions({
148 queryKey: createKey(),
149 })
150​
151 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
152 })
153​
154 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
155 const { queryKey: tagged } = queryOptions({
156 queryKey: createKey(),
157 queryFn: () => Promise.resolve(5),
158 select: (data) => data.toString(),
159 })
160​
161 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
162 })
163​
164 it('should return the proper type when passed to getQueryData', () => {
165 const { queryKey: tagged } = queryOptions({
166 queryKey: createKey(),
167 queryFn: () => Promise.resolve(5),
168 })
169​
170 const queryClient = new QueryClient()
171 const data = queryClient.getQueryData(tagged)
172 expectTypeOf(data).toEqualTypeOf<number | undefined>()
173 })
174​
175 it('should return the proper type when passed to getQueryState', () => {
176 const { queryKey: tagged } = queryOptions({
177 queryKey: createKey(),
178 queryFn: () => Promise.resolve(5),
179 })
180​
181 const queryClient = new QueryClient()
182 const state = queryClient.getQueryState(tagged)
183 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
184 })
185​
186 it('should properly type updaterFn when passed to setQueryData', () => {
187 const { queryKey: tagged } = queryOptions({
188 queryKey: createKey(),
189 queryFn: () => Promise.resolve(5),
190 })
191​
192 const queryClient = new QueryClient()
193 const data = queryClient.setQueryData(tagged, (prev) => {
194 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
195 return prev
196 })
197 expectTypeOf(data).toEqualTypeOf<number | undefined>()
198 })
199​
200 it('should properly type value when passed to setQueryData', () => {
201 const { queryKey: tagged } = queryOptions({
202 queryKey: createKey(),
203 queryFn: () => Promise.resolve(5),
204 })
205​
206 const queryClient = new QueryClient()
207​
208 // @ts-expect-error value should be a number
209 queryClient.setQueryData(tagged, '5')
210 // @ts-expect-error value should be a number
211 queryClient.setQueryData(tagged, () => '5')
212​
213 const data = queryClient.setQueryData(tagged, 5)
214 expectTypeOf(data).toEqualTypeOf<number | undefined>()
215 })
216​
217 it('should infer even if there is a conditional skipToken', () => {
218 const options = queryOptions({
219 queryKey: createKey(),
220 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
221 })
222​
223 const queryClient = new QueryClient()
224 const data = queryClient.getQueryData(options.queryKey)
225 expectTypeOf(data).toEqualTypeOf<number | undefined>()
226 })
227​
228 it('should infer to unknown if we disable a query with just a skipToken', () => {
229 const options = queryOptions({
230 queryKey: createKey(),
231 queryFn: skipToken,
232 })
233​
234 const queryClient = new QueryClient()
235 const data = queryClient.getQueryData(options.queryKey)
236 expectTypeOf(data).toEqualTypeOf<unknown>()
237 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
238 })
239​
240 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
241 const options = queryOptions({
242 queryKey: createKey(),
243 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
244 })
245 // @ts-expect-error TS2345
246 const { data } = useSuspenseQuery(options)
247 expectTypeOf(data).toEqualTypeOf<number>()
248 })
249​
250 it('should allow undefined response in initialData', () => {
251 assertType((id: string | null) =>
252 queryOptions({
253 queryKey: [...createKey(), id],
254 queryFn: () =>
255 Promise.resolve({
256 id: '1',
257 title: 'Do Laundry',
258 }),
259 initialData: () =>
260 !id
261 ? undefined
262 : {
263 id,
264 title: 'Initial Data',
265 },
266 }),
267 )
268 })
269​
270 it('should allow optional initialData object', () => {
271 const options = queryOptions({
272 queryKey: createKey(),
273 queryFn: () => Promise.resolve('something string'),
274 initialData: Math.random() > 0.5 ? 'initial string' : undefined,
275 })
276​
277 expectTypeOf(options.initialData).toExtend<
278 InitialDataFunction<string> | string | undefined
279 >()
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: createKey(),
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})