Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import {
3 QueryClient,
4 dataTagErrorSymbol,
5 dataTagSymbol,
6 skipToken,
7} from '@tanstack/query-core'
8import { queryKey } from '@tanstack/query-test-utils'
9import { useQuery } from '../useQuery'
10import { queryOptions } from '../queryOptions'
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 assertType(
23 queryOptions({
24 queryKey: queryKey(),
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 queryOptions({
34 queryKey: queryKey(),
35 queryFn: () => Promise.resolve(5),
36 staleTime: 1000,
37 select: (data) => {
38 expectTypeOf(data).toEqualTypeOf<number>()
39 },
40 })
41 })
42β
43 it('should work when passed to useQuery', () => {
44 const options = queryOptions({
45 queryKey: queryKey(),
46 queryFn: () => Promise.resolve(5),
47 })
48β
49 const { data } = useQuery(() => options)
50 expectTypeOf(data).toEqualTypeOf<number | undefined>()
51 })
52β
53 it('should work when passed to query', async () => {
54 const options = queryOptions({
55 queryKey: ['key'],
56 queryFn: () => Promise.resolve(5),
57 })
58β
59 const data = await new QueryClient().query(options)
60 expectTypeOf(data).toEqualTypeOf<number>()
61 })
62β
63 it('should work when passed to query with select', async () => {
64 const options = queryOptions({
65 queryKey: ['key'],
66 queryFn: () => Promise.resolve(5),
67 select: (data) => data.toString(),
68 })
69β
70 const data = await new QueryClient().query(options)
71 expectTypeOf(data).toEqualTypeOf<string>()
72 })
73β
74 it('should work when passed to query with enabled: false', async () => {
75 const options = queryOptions({
76 queryKey: ['key'],
77 queryFn: () => Promise.resolve(5),
78 enabled: false,
79 })
80β
81 const data = await new QueryClient().query(options)
82 expectTypeOf(data).toEqualTypeOf<number>()
83 })
84β
85 it('should work when passed to query with skipToken', async () => {
86 const options = queryOptions({
87 queryKey: ['key'],
88 queryFn: skipToken,
89 })
90β
91 const data = await new QueryClient().query(options)
92 expectTypeOf(data).toEqualTypeOf<unknown>()
93 })
94β
95 it('should work when passed to fetchQuery', async () => {
96 const options = queryOptions({
97 queryKey: queryKey(),
98 queryFn: () => Promise.resolve(5),
99 })
100β
101 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
102 const data = await new QueryClient().fetchQuery(options)
103 expectTypeOf(data).toEqualTypeOf<number>()
104 })
105β
106 it('should tag the queryKey with the result type of the QueryFn', () => {
107 const { queryKey: tagged } = queryOptions({
108 queryKey: queryKey(),
109 queryFn: () => Promise.resolve(5),
110 })
111β
112 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
113 })
114β
115 it('should tag the queryKey even if no promise is returned', () => {
116 const { queryKey: tagged } = queryOptions({
117 queryKey: queryKey(),
118 queryFn: () => 5,
119 })
120β
121 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
122 })
123β
124 it('should tag the queryKey with unknown if there is no queryFn', () => {
125 const { queryKey: tagged } = queryOptions({
126 queryKey: queryKey(),
127 })
128β
129 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
130 })
131β
132 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
133 const { queryKey: tagged } = queryOptions({
134 queryKey: queryKey(),
135 queryFn: () => Promise.resolve(5),
136 select: (data) => data.toString(),
137 })
138β
139 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
140 })
141β
142 it('should tag the queryKey with the default error type', () => {
143 const { queryKey: tagged } = queryOptions({
144 queryKey: queryKey(),
145 queryFn: () => Promise.resolve(1),
146 })
147β
148 expectTypeOf(tagged[dataTagErrorSymbol]).toEqualTypeOf<Error>()
149 })
150β
151 it('should return the proper type when passed to getQueryData', () => {
152 const { queryKey: tagged } = queryOptions({
153 queryKey: queryKey(),
154 queryFn: () => Promise.resolve(5),
155 })
156β
157 const queryClient = new QueryClient()
158 const data = queryClient.getQueryData(tagged)
159 expectTypeOf(data).toEqualTypeOf<number | undefined>()
160 })
161β
162 it('should return the proper type when passed to getQueryState', () => {
163 const { queryKey: tagged } = queryOptions({
164 queryKey: queryKey(),
165 queryFn: () => Promise.resolve(5),
166 })
167β
168 const queryClient = new QueryClient()
169 const state = queryClient.getQueryState(tagged)
170 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
171 })
172β
173 it('should properly type updaterFn when passed to setQueryData', () => {
174 const { queryKey: tagged } = queryOptions({
175 queryKey: queryKey(),
176 queryFn: () => Promise.resolve(5),
177 })
178β
179 const queryClient = new QueryClient()
180 const data = queryClient.setQueryData(tagged, (prev) => {
181 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
182 return prev
183 })
184 expectTypeOf(data).toEqualTypeOf<number | undefined>()
185 })
186β
187 it('should properly type value when passed to setQueryData', () => {
188 const { queryKey: tagged } = queryOptions({
189 queryKey: queryKey(),
190 queryFn: () => Promise.resolve(5),
191 })
192β
193 const queryClient = new QueryClient()
194β
195 // @ts-expect-error value should be a number
196 queryClient.setQueryData(tagged, '5')
197 // @ts-expect-error value should be a number
198 queryClient.setQueryData(tagged, () => '5')
199β
200 const data = queryClient.setQueryData(tagged, 5)
201 expectTypeOf(data).toEqualTypeOf<number | undefined>()
202 })
203β
204 it('should infer even if there is a conditional skipToken', () => {
205 const options = queryOptions({
206 queryKey: queryKey(),
207 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
208 })
209β
210 const queryClient = new QueryClient()
211 const data = queryClient.getQueryData(options.queryKey)
212 expectTypeOf(data).toEqualTypeOf<number | undefined>()
213 })
214β
215 it('should infer to unknown if we disable a query with just a skipToken', () => {
216 const options = queryOptions({
217 queryKey: queryKey(),
218 queryFn: skipToken,
219 })
220β
221 const queryClient = new QueryClient()
222 const data = queryClient.getQueryData(options.queryKey)
223 expectTypeOf(data).toEqualTypeOf<unknown>()
224 expectTypeOf(options.queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
225 })
226β
227 it('should allow optional initialData object', () => {
228 const options = queryOptions({
229 queryKey: queryKey(),
230 queryFn: () => Promise.resolve('something string'),
231 initialData: Math.random() > 0.5 ? 'initial string' : undefined,
232 })
233β
234 expectTypeOf(options.initialData).toExtend<
235 InitialDataFunction<string> | string | undefined
236 >()
237 })
238})