Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import {
3 QueriesObserver,
4 QueryClient,
5 dataTagSymbol,
6 skipToken,
7} from '@tanstack/query-core'
8import { queryOptions } from '../queryOptions'
9import { useQuery } from '../useQuery'
10import { useQueries } from '../useQueries'
11import { useSuspenseQuery } from '../useSuspenseQuery'
12import type { UseQueryOptions } from '../types'
13import type {
14 InitialDataFunction,
15 QueryObserverResult,
16} from '@tanstack/query-core'
17β
18describe('queryOptions', () => {
19 it('should not allow excess properties', () => {
20 queryOptions({
21 queryKey: ['key'],
22 queryFn: () => Promise.resolve(5),
23 // @ts-expect-error this is a good error, because stallTime does not exist!
24 stallTime: 1000,
25 })
26 })
27 it('should infer types for callbacks', () => {
28 queryOptions({
29 queryKey: ['key'],
30 queryFn: () => Promise.resolve(5),
31 staleTime: 1000,
32 select: (data) => {
33 expectTypeOf(data).toEqualTypeOf<number>()
34 },
35 })
36 })
37 it('should work when passed to useQuery', () => {
38 const options = queryOptions({
39 queryKey: ['key'],
40 queryFn: () => Promise.resolve(5),
41 })
42β
43 const { data } = useQuery(options)
44 expectTypeOf(data).toEqualTypeOf<number | undefined>()
45 })
46 it('should work when passed to useSuspenseQuery', () => {
47 const options = queryOptions({
48 queryKey: ['key'],
49 queryFn: () => Promise.resolve(5),
50 })
51β
52 const { data } = useSuspenseQuery(options)
53 expectTypeOf(data).toEqualTypeOf<number>()
54 })
55β
56 it('should work when passed to fetchQuery', async () => {
57 const options = queryOptions({
58 queryKey: ['key'],
59 queryFn: () => Promise.resolve(5),
60 })
61β
62 const data = await new QueryClient().fetchQuery(options)
63 expectTypeOf(data).toEqualTypeOf<number>()
64 })
65 it('should work when passed to useQueries', () => {
66 const options = queryOptions({
67 queryKey: ['key'],
68 queryFn: () => Promise.resolve(5),
69 })
70β
71 const [{ data }] = useQueries({
72 queries: [options],
73 })
74β
75 expectTypeOf(data).toEqualTypeOf<number | undefined>()
76 })
77 it('should tag the queryKey with the result type of the QueryFn', () => {
78 const { queryKey } = queryOptions({
79 queryKey: ['key'],
80 queryFn: () => Promise.resolve(5),
81 })
82β
83 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
84 })
85 it('should tag the queryKey even if no promise is returned', () => {
86 const { queryKey } = queryOptions({
87 queryKey: ['key'],
88 queryFn: () => 5,
89 })
90β
91 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
92 })
93 it('should tag the queryKey with unknown if there is no queryFn', () => {
94 const { queryKey } = queryOptions({
95 queryKey: ['key'],
96 })
97β
98 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<unknown>()
99 })
100 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
101 const { queryKey } = queryOptions({
102 queryKey: ['key'],
103 queryFn: () => Promise.resolve(5),
104 select: (data) => data.toString(),
105 })
106β
107 expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<number>()
108 })
109 it('should return the proper type when passed to getQueryData', () => {
110 const { queryKey } = queryOptions({
111 queryKey: ['key'],
112 queryFn: () => Promise.resolve(5),
113 })
114β
115 const queryClient = new QueryClient()
116 const data = queryClient.getQueryData(queryKey)
117 expectTypeOf(data).toEqualTypeOf<number | undefined>()
118 })
119 it('should return the proper type when passed to getQueryState', () => {
120 const { queryKey } = queryOptions({
121 queryKey: ['key'],
122 queryFn: () => Promise.resolve(5),
123 })
124β
125 const queryClient = new QueryClient()
126 const state = queryClient.getQueryState(queryKey)
127 expectTypeOf(state?.data).toEqualTypeOf<number | undefined>()
128 })
129 it('should properly type updaterFn when passed to setQueryData', () => {
130 const { queryKey } = queryOptions({
131 queryKey: ['key'],
132 queryFn: () => Promise.resolve(5),
133 })
134β
135 const queryClient = new QueryClient()
136 const data = queryClient.setQueryData(queryKey, (prev) => {
137 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
138 return prev
139 })
140 expectTypeOf(data).toEqualTypeOf<number | undefined>()
141 })
142 it('should properly type value when passed to setQueryData', () => {
143 const { queryKey } = queryOptions({
144 queryKey: ['key'],
145 queryFn: () => Promise.resolve(5),
146 })
147β
148 const queryClient = new QueryClient()
149β
150 // @ts-expect-error value should be a number
151 queryClient.setQueryData(queryKey, '5')
152 // @ts-expect-error value should be a number
153 queryClient.setQueryData(queryKey, () => '5')
154β
155 const data = queryClient.setQueryData(queryKey, 5)
156 expectTypeOf(data).toEqualTypeOf<number | undefined>()
157 })
158β
159 it('should infer even if there is a conditional skipToken', () => {
160 const options = queryOptions({
161 queryKey: ['key'],
162 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
163 })
164β
165 const queryClient = new QueryClient()
166 const data = queryClient.getQueryData(options.queryKey)
167 expectTypeOf(data).toEqualTypeOf<number | undefined>()
168 })
169β
170 it('should infer to unknown if we disable a query with just a skipToken', () => {
171 const options = queryOptions({
172 queryKey: ['key'],
173 queryFn: skipToken,
174 })
175β
176 const queryClient = new QueryClient()
177 const data = queryClient.getQueryData(options.queryKey)
178 expectTypeOf(data).toEqualTypeOf<unknown>()
179 })
180β
181 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
182 const options = queryOptions({
183 queryKey: ['key'],
184 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
185 })
186 // @ts-expect-error TS2345
187 const { data } = useSuspenseQuery(options)
188 expectTypeOf(data).toEqualTypeOf<number>()
189 })
190β
191 it('should return the proper type when passed to QueriesObserver', () => {
192 const options = queryOptions({
193 queryKey: ['key'],
194 queryFn: () => Promise.resolve(5),
195 })
196β
197 const queryClient = new QueryClient()
198 const queriesObserver = new QueriesObserver(queryClient, [options])
199 expectTypeOf(queriesObserver).toEqualTypeOf<
200 QueriesObserver<Array<QueryObserverResult>>
201 >()
202 })
203β
204 it('should allow undefined response in initialData', () => {
205 return (id: string | null) =>
206 queryOptions({
207 queryKey: ['todo', id],
208 queryFn: () =>
209 Promise.resolve({
210 id: '1',
211 title: 'Do Laundry',
212 }),
213 initialData: () =>
214 !id
215 ? undefined
216 : {
217 id,
218 title: 'Initial Data',
219 },
220 })
221 })
222β
223 it('should allow optional initialData object', () => {
224 const testFn = (id?: string) => {
225 const options = queryOptions({
226 queryKey: ['test'],
227 queryFn: async () => 'something string',
228 initialData: id ? 'initial string' : undefined,
229 })
230 expectTypeOf(options.initialData).toMatchTypeOf<
231 InitialDataFunction<string> | string | undefined
232 >()
233 }
234 testFn('id')
235 testFn()
236 })
237β
238 it('should be passable to UseQueryOptions', () => {
239 function somethingWithQueryOptions(options: UseQueryOptions<number>) {
240 return options.queryKey
241 }
242β
243 const options = queryOptions({
244 queryKey: ['key'],
245 queryFn: () => Promise.resolve(1),
246 })
247β
248 somethingWithQueryOptions(options)
249 })
250})