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