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