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