Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { computed, reactive, ref } from 'vue-demi'
3import { useQuery } from '../useQuery'
4import { queryOptions } from '../queryOptions'
5import { simpleFetcher } from './test-utils'
6import type { OmitKeyof } from '..'
7import type { UseQueryOptions } from '../useQuery'
8β
9describe('useQuery', () => {
10 describe('Config object overload', () => {
11 it('TData should always be defined when initialData is provided as an object', () => {
12 const { data } = reactive(
13 useQuery({
14 queryKey: ['key'],
15 queryFn: () => {
16 return {
17 wow: true,
18 }
19 },
20 initialData: {
21 wow: true,
22 },
23 }),
24 )
25β
26 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
27 })
28β
29 it('TData should be defined when passed through queryOptions', () => {
30 const options = queryOptions({
31 queryKey: ['key'],
32 queryFn: () => {
33 return {
34 wow: true,
35 }
36 },
37 initialData: {
38 wow: true,
39 },
40 })
41 const { data } = reactive(useQuery(options))
42β
43 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
44 })
45β
46 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
47 const options = queryOptions({
48 queryKey: ['key'],
49 queryFn: () => Promise.resolve(1),
50 })
51β
52 const query = reactive(
53 useQuery({
54 ...options,
55 select: (data) => data > 1,
56 }),
57 )
58β
59 expectTypeOf(query.data).toEqualTypeOf<boolean | undefined>()
60 })
61β
62 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
63 const { data } = reactive(
64 useQuery({
65 queryKey: ['key'],
66 queryFn: () => {
67 return {
68 wow: true,
69 }
70 },
71 initialData: () => ({
72 wow: true,
73 }),
74 }),
75 )
76β
77 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
78 })
79β
80 it('TData should have undefined in the union when initialData is NOT provided', () => {
81 const { data } = reactive(
82 useQuery({
83 queryKey: ['key'],
84 queryFn: () => {
85 return {
86 wow: true,
87 }
88 },
89 }),
90 )
91β
92 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
93 })
94β
95 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
96 const { data } = reactive(
97 useQuery({
98 queryKey: ['key'],
99 queryFn: () => {
100 return {
101 wow: true,
102 }
103 },
104 initialData: () => undefined as { wow: boolean } | undefined,
105 }),
106 )
107β
108 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
109 })
110β
111 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
112 const { data, isSuccess } = reactive(
113 useQuery({
114 queryKey: ['key'],
115 queryFn: () => {
116 return {
117 wow: true,
118 }
119 },
120 initialData: () => undefined as { wow: boolean } | undefined,
121 }),
122 )
123β
124 if (isSuccess) {
125 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
126 }
127 })
128β
129 it('data should not have undefined when initialData is provided', () => {
130 const { data } = reactive(
131 useQuery({
132 queryKey: ['query-key'],
133 initialData: 42,
134 }),
135 )
136β
137 expectTypeOf(data).toEqualTypeOf<number>()
138 })
139 })
140β
141 describe('custom composable', () => {
142 it('should allow custom composable using UseQueryOptions', () => {
143 const useCustomQuery = (
144 options?: OmitKeyof<
145 UseQueryOptions<string>,
146 'queryKey' | 'queryFn',
147 'safely'
148 >,
149 ) => {
150 return useQuery({
151 ...options,
152 queryKey: ['todos-key'],
153 queryFn: () => Promise.resolve('data'),
154 })
155 }
156β
157 const { data } = reactive(useCustomQuery())
158β
159 expectTypeOf(data).toEqualTypeOf<string | undefined>()
160 })
161 })
162β
163 describe('structuralSharing', () => {
164 it('should restrict to same types', () => {
165 useQuery({
166 queryKey: ['key'],
167 queryFn: () => 5,
168 structuralSharing: (_oldData, newData) => {
169 return newData
170 },
171 })
172 })
173 })
174β
175 describe('Discriminated union return type', () => {
176 it('data should be possibly undefined by default', () => {
177 const query = reactive(
178 useQuery({
179 queryKey: ['key'],
180 queryFn: simpleFetcher,
181 }),
182 )
183β
184 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
185 })
186β
187 it('data should be defined when query is success', () => {
188 const query = reactive(
189 useQuery({
190 queryKey: ['key'],
191 queryFn: simpleFetcher,
192 }),
193 )
194β
195 if (query.isSuccess) {
196 expectTypeOf(query.data).toEqualTypeOf<string>()
197 }
198 })
199β
200 it('error should be null when query is success', () => {
201 const query = reactive(
202 useQuery({
203 queryKey: ['key'],
204 queryFn: simpleFetcher,
205 }),
206 )
207β
208 if (query.isSuccess) {
209 expectTypeOf(query.error).toEqualTypeOf<null>()
210 }
211 })
212β
213 it('data should be undefined when query is pending', () => {
214 const query = reactive(
215 useQuery({
216 queryKey: ['key'],
217 queryFn: simpleFetcher,
218 }),
219 )
220β
221 if (query.isPending) {
222 expectTypeOf(query.data).toEqualTypeOf<undefined>()
223 }
224 })
225β
226 it('error should be defined when query is error', () => {
227 const query = reactive(
228 useQuery({
229 queryKey: ['key'],
230 queryFn: simpleFetcher,
231 }),
232 )
233β
234 if (query.isError) {
235 expectTypeOf(query.error).toEqualTypeOf<Error>()
236 }
237 })
238 })
239β
240 describe('accept ref options', () => {
241 it('should accept ref options', () => {
242 const options = ref({
243 queryKey: ['key'],
244 queryFn: simpleFetcher,
245 })
246β
247 const query = reactive(useQuery(options))
248β
249 if (query.isSuccess) {
250 expectTypeOf(query.data).toEqualTypeOf<string>()
251 }
252 })
253β
254 it('should accept computed options', () => {
255 const options = computed(() => ({
256 queryKey: ['key'],
257 queryFn: simpleFetcher,
258 }))
259β
260 const query = reactive(useQuery(options))
261β
262 if (query.isSuccess) {
263 expectTypeOf(query.data).toEqualTypeOf<string>()
264 }
265 })
266β
267 it('should accept computed query options', () => {
268 const options = computed(() =>
269 queryOptions({
270 queryKey: ['key'],
271 queryFn: simpleFetcher,
272 }),
273 )
274β
275 const query = reactive(useQuery(options))
276β
277 if (query.isSuccess) {
278 expectTypeOf(query.data).toEqualTypeOf<string>()
279 }
280 })
281 })
282})