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

queryOptions.test-d.tsx

286 lines8.3 KB

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