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

queryOptions.test-d.tsx

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