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

queryOptions.test-d.ts

270 lines7.8 KB

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