Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { reactive, ref } from 'vue-demi'
3import { dataTagSymbol } from '@tanstack/query-core'
4import { queryKey } from '@tanstack/query-test-utils'
5import { QueryClient } from '../queryClient'
6import { queryOptions } from '../queryOptions'
7import { useQuery } from '../useQuery'
8β
9describe('queryOptions', () => {
10 it('should not allow excess properties', () => {
11 const key = queryKey()
12 assertType(
13 queryOptions({
14 // @ts-expect-error this is a good error, because stallTime does not exist!
15 queryKey: key,
16 queryFn: () => Promise.resolve(5),
17 stallTime: 1000,
18 }),
19 )
20 })
21 it('should infer types for callbacks', () => {
22 const key = queryKey()
23 queryOptions({
24 queryKey: key,
25 queryFn: () => Promise.resolve(5),
26 staleTime: 1000,
27 select: (data) => {
28 expectTypeOf(data).toEqualTypeOf<number>()
29 },
30 })
31 })
32 it('should work when passed to useQuery', () => {
33 const key = queryKey()
34 const options = queryOptions({
35 queryKey: key,
36 queryFn: () => Promise.resolve(5),
37 })
38β
39 const { data } = reactive(useQuery(options))
40 expectTypeOf(data).toEqualTypeOf<number | undefined>()
41 })
42 it('should tag the queryKey with the result type of the QueryFn', () => {
43 const key = queryKey()
44 const { queryKey: tagged } = queryOptions({
45 queryKey: key,
46 queryFn: () => Promise.resolve(5),
47 })
48β
49 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
50 })
51 it('should tag the queryKey even if no promise is returned', () => {
52 const key = queryKey()
53 const { queryKey: tagged } = queryOptions({
54 queryKey: key,
55 queryFn: () => 5,
56 })
57β
58 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
59 })
60 it('should tag the queryKey with unknown if there is no queryFn', () => {
61 const key = queryKey()
62 const { queryKey: tagged } = queryOptions({
63 queryKey: key,
64 })
65β
66 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<unknown>()
67 })
68 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
69 const key = queryKey()
70 const { queryKey: tagged } = queryOptions({
71 queryKey: key,
72 queryFn: () => Promise.resolve(5),
73 select: (data) => data.toString(),
74 })
75β
76 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<number>()
77 })
78 it('should return the proper type when passed to getQueryData', () => {
79 const key = queryKey()
80 const { queryKey: tagged } = queryOptions({
81 queryKey: key,
82 queryFn: () => Promise.resolve(5),
83 })
84β
85 const queryClient = new QueryClient()
86 const data = queryClient.getQueryData(tagged)
87β
88 expectTypeOf(data).toEqualTypeOf<number | undefined>()
89 })
90 it('should properly type updaterFn when passed to setQueryData', () => {
91 const key = queryKey()
92 const { queryKey: tagged } = queryOptions({
93 queryKey: key,
94 queryFn: () => Promise.resolve(5),
95 })
96β
97 const queryClient = new QueryClient()
98 const data = queryClient.setQueryData(tagged, (prev) => {
99 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
100 return prev
101 })
102 expectTypeOf(data).toEqualTypeOf<number | undefined>()
103 })
104 it('should properly type value when passed to setQueryData', () => {
105 const key = queryKey()
106 const { queryKey: tagged } = queryOptions({
107 queryKey: key,
108 queryFn: () => Promise.resolve(5),
109 })
110β
111 const queryClient = new QueryClient()
112β
113 // @ts-expect-error value should be a number
114 queryClient.setQueryData(tagged, '5')
115 // @ts-expect-error value should be a number
116 queryClient.setQueryData(tagged, () => '5')
117β
118 const data = queryClient.setQueryData(tagged, 5)
119β
120 expectTypeOf(data).toEqualTypeOf<number | undefined>()
121 })
122 it('should allow to be passed to QueryClient methods while containing getter', () => {
123 const ref1 = ref(1)
124 const ref2 = ref(2)
125 const options = queryOptions(() => ({
126 queryKey: ['key', ref1.value, { nested: ref2.value }],
127 queryFn: () => Promise.resolve(5),
128 }))
129β
130 const queryClient = new QueryClient()
131β
132 // Should not error
133 const data = queryClient.invalidateQueries(options)
134 // Should not error
135 const data2 = queryClient.fetchQuery(options)
136β
137 expectTypeOf(data).toEqualTypeOf<Promise<void>>()
138 expectTypeOf(data2).toEqualTypeOf<Promise<number>>()
139 })
140β
141 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
142 const key = queryKey()
143 const { data } = reactive(
144 useQuery(
145 queryOptions({
146 queryKey: key,
147 queryFn: () => {
148 return {
149 wow: true,
150 }
151 },
152 initialData: () => ({
153 wow: true,
154 }),
155 }),
156 ),
157 )
158β
159 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
160 })
161β
162 it('TData should have undefined in the union when initialData is NOT provided', () => {
163 const key = queryKey()
164 const { data } = reactive(
165 useQuery(
166 queryOptions({
167 queryKey: key,
168 queryFn: () => {
169 return {
170 wow: true,
171 }
172 },
173 }),
174 ),
175 )
176β
177 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
178 })
179β
180 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
181 const key = queryKey()
182 const { data } = reactive(
183 useQuery(
184 queryOptions({
185 queryKey: key,
186 queryFn: () => {
187 return {
188 wow: true,
189 }
190 },
191 initialData: () => undefined as { wow: boolean } | undefined,
192 }),
193 ),
194 )
195β
196 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
197 })
198β
199 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
200 const key = queryKey()
201 const { data, isSuccess } = reactive(
202 useQuery(
203 queryOptions({
204 queryKey: key,
205 queryFn: () => {
206 return {
207 wow: true,
208 }
209 },
210 initialData: () => undefined as { wow: boolean } | undefined,
211 }),
212 ),
213 )
214β
215 if (isSuccess) {
216 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
217 }
218 })
219β
220 it('data should not have undefined when initialData is provided', () => {
221 const key = queryKey()
222 const { data } = reactive(
223 useQuery(
224 queryOptions({
225 queryKey: key,
226 initialData: 42,
227 }),
228 ),
229 )
230β
231 expectTypeOf(data).toEqualTypeOf<number>()
232 })
233β
234 it('should allow accessing queryFn and other properties on the returned options object', () => {
235 const options = queryOptions({
236 queryKey: ['groups'],
237 queryFn: () => Promise.resolve([]),
238 })
239β
240 expectTypeOf(options.queryFn).not.toBeUndefined()
241 expectTypeOf(options.queryKey).not.toBeUndefined()
242 expectTypeOf(options.staleTime).not.toBeUndefined()
243 })
244β
245 it('should allow accessing queryFn and other properties on the returned options when used with getter', () => {
246 const options = queryOptions(() => ({
247 queryKey: ['groups'],
248 queryFn: () => Promise.resolve([]),
249 }))
250β
251 const resolvedGetter = options()
252β
253 expectTypeOf(resolvedGetter.queryFn).not.toBeUndefined()
254 expectTypeOf(resolvedGetter.queryKey).not.toBeUndefined()
255 })
256})