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

query-options.test-d.ts

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