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

query-options.test-d.ts

263 lines7.4 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { createKey } 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 = createKey()
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 = createKey()
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: createKey(),
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 it('should work when passed to injectQuery', () => {
74 const key = createKey()
75 const options = queryOptions({
76 queryKey: key,
77 queryFn: () => Promise.resolve(5),
78 })
79​
80 const { data } = injectQuery(() => options)
81 expectTypeOf(data).toEqualTypeOf<Signal<number | undefined>>()
82 })
83​
84 it('should work when passed to fetchQuery', () => {
85 const key = createKey()
86 const options = queryOptions({
87 queryKey: key,
88 queryFn: () => Promise.resolve(5),
89 })
90​
91 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
92 const data = new QueryClient().fetchQuery(options)
93 assertType<Promise<number>>(data)
94 })
95​
96 it('should work when passed to query', () => {
97 const options = queryOptions({
98 queryKey: ['key'],
99 queryFn: () => Promise.resolve(5),
100 })
101​
102 const data = new QueryClient().query(options)
103 assertType<Promise<number>>(data)
104 })
105​
106 it('should work when passed to query with select', () => {
107 const options = queryOptions({
108 queryKey: ['key'],
109 queryFn: () => Promise.resolve(5),
110 select: (data) => data.toString(),
111 })
112​
113 const data = new QueryClient().query(options)
114 assertType<Promise<string>>(data)
115 })
116​
117 it('should work when passed to query with enabled: false', () => {
118 const options = queryOptions({
119 queryKey: ['key'],
120 queryFn: () => Promise.resolve(5),
121 enabled: false,
122 })
123​
124 const data = new QueryClient().query(options)
125 assertType<Promise<number>>(data)
126 })
127​
128 it('should work when passed to query with skipToken', () => {
129 const options = queryOptions({
130 queryKey: ['key'],
131 queryFn: skipToken,
132 })
133​
134 const data = new QueryClient().query(options)
135 assertType<Promise<unknown>>(data)
136 })
137​
138 it('should tag the queryKey with the result type of the QueryFn', () => {
139 const key = createKey()
140 const { queryKey: tagged } = queryOptions({
141 queryKey: key,
142 queryFn: () => Promise.resolve(5),
143 })
144 assertType<number>(tagged[dataTagSymbol])
145 })
146​
147 it('should tag the queryKey even if no promise is returned', () => {
148 const key = createKey()
149 const { queryKey: tagged } = queryOptions({
150 queryKey: key,
151 queryFn: () => 5,
152 })
153 assertType<number>(tagged[dataTagSymbol])
154 })
155​
156 it('should tag the queryKey with unknown if there is no queryFn', () => {
157 const key = createKey()
158 const { queryKey: tagged } = queryOptions({
159 queryKey: key,
160 })
161​
162 assertType<unknown>(tagged[dataTagSymbol])
163 })
164​
165 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
166 const key = createKey()
167 const { queryKey: tagged } = queryOptions({
168 queryKey: key,
169 queryFn: () => Promise.resolve(5),
170 select: (data) => data.toString(),
171 })
172​
173 assertType<number>(tagged[dataTagSymbol])
174 })
175​
176 it('should return the proper type when passed to getQueryData', () => {
177 const key = createKey()
178 const { queryKey: tagged } = queryOptions({
179 queryKey: key,
180 queryFn: () => Promise.resolve(5),
181 })
182​
183 const queryClient = new QueryClient()
184 const data = queryClient.getQueryData(tagged)
185​
186 expectTypeOf(data).toEqualTypeOf<number | undefined>()
187 })
188​
189 it('should return the proper type when passed to getQueryState', () => {
190 const key = createKey()
191 const { queryKey: tagged } = queryOptions({
192 queryKey: key,
193 queryFn: () => Promise.resolve(5),
194 })
195​
196 const queryClient = new QueryClient()
197 const state = queryClient.getQueryState(tagged)
198​
199 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
200 })
201​
202 it('should properly type updaterFn when passed to setQueryData', () => {
203 const key = createKey()
204 const { queryKey: tagged } = queryOptions({
205 queryKey: key,
206 queryFn: () => Promise.resolve(5),
207 })
208​
209 const queryClient = new QueryClient()
210 const data = queryClient.setQueryData(tagged, (prev) => {
211 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
212 return prev
213 })
214​
215 expectTypeOf(data).toEqualTypeOf<number | undefined>()
216 })
217​
218 it('should properly type value when passed to setQueryData', () => {
219 const key = createKey()
220 const { queryKey: tagged } = queryOptions({
221 queryKey: key,
222 queryFn: () => Promise.resolve(5),
223 })
224​
225 const queryClient = new QueryClient()
226​
227 // @ts-expect-error value should be a number
228 queryClient.setQueryData(tagged, '5')
229 // @ts-expect-error value should be a number
230 queryClient.setQueryData(tagged, () => '5')
231​
232 const data = queryClient.setQueryData(tagged, 5)
233​
234 expectTypeOf(data).toEqualTypeOf<number | undefined>()
235 })
236​
237 it('should infer even if there is a conditional skipToken', () => {
238 const key = createKey()
239 const options = queryOptions({
240 queryKey: key,
241 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
242 })
243​
244 const queryClient = new QueryClient()
245 const data = queryClient.getQueryData(options.queryKey)
246​
247 expectTypeOf(data).toEqualTypeOf<number | undefined>()
248 })
249​
250 it('should infer to unknown if we disable a query with just a skipToken', () => {
251 const key = createKey()
252 const options = queryOptions({
253 queryKey: key,
254 queryFn: skipToken,
255 })
256​
257 const queryClient = new QueryClient()
258 const data = queryClient.getQueryData(options.queryKey)
259​
260 expectTypeOf(data).toEqualTypeOf<unknown>()
261 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
262 })
263})