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

query-options.test-d.ts

262 lines7.0 KB

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