Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useQuery.test-d.ts

271 lines6.7 KB

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​
130 describe('custom composable', () => {
131 it('should allow custom composable using UseQueryOptions', () => {
132 const useCustomQuery = (
133 options?: OmitKeyof<
134 UseQueryOptions<string>,
135 'queryKey' | 'queryFn',
136 'safely'
137 >,
138 ) => {
139 return useQuery({
140 ...options,
141 queryKey: ['todos-key'],
142 queryFn: () => Promise.resolve('data'),
143 })
144 }
145​
146 const { data } = reactive(useCustomQuery())
147​
148 expectTypeOf(data).toEqualTypeOf<string | undefined>()
149 })
150 })
151​
152 describe('structuralSharing', () => {
153 it('should restrict to same types', () => {
154 useQuery({
155 queryKey: ['key'],
156 queryFn: () => 5,
157 structuralSharing: (_oldData, newData) => {
158 return newData
159 },
160 })
161 })
162 })
163​
164 describe('Discriminated union return type', () => {
165 it('data should be possibly undefined by default', () => {
166 const query = reactive(
167 useQuery({
168 queryKey: ['key'],
169 queryFn: simpleFetcher,
170 }),
171 )
172​
173 expectTypeOf(query.data).toEqualTypeOf<string | undefined>()
174 })
175​
176 it('data should be defined when query is success', () => {
177 const query = reactive(
178 useQuery({
179 queryKey: ['key'],
180 queryFn: simpleFetcher,
181 }),
182 )
183​
184 if (query.isSuccess) {
185 expectTypeOf(query.data).toEqualTypeOf<string>()
186 }
187 })
188​
189 it('error should be null when query is success', () => {
190 const query = reactive(
191 useQuery({
192 queryKey: ['key'],
193 queryFn: simpleFetcher,
194 }),
195 )
196​
197 if (query.isSuccess) {
198 expectTypeOf(query.error).toEqualTypeOf<null>()
199 }
200 })
201​
202 it('data should be undefined when query is pending', () => {
203 const query = reactive(
204 useQuery({
205 queryKey: ['key'],
206 queryFn: simpleFetcher,
207 }),
208 )
209​
210 if (query.isPending) {
211 expectTypeOf(query.data).toEqualTypeOf<undefined>()
212 }
213 })
214​
215 it('error should be defined when query is error', () => {
216 const query = reactive(
217 useQuery({
218 queryKey: ['key'],
219 queryFn: simpleFetcher,
220 }),
221 )
222​
223 if (query.isError) {
224 expectTypeOf(query.error).toEqualTypeOf<Error>()
225 }
226 })
227 })
228​
229 describe('accept ref options', () => {
230 it('should accept ref options', () => {
231 const options = ref({
232 queryKey: ['key'],
233 queryFn: simpleFetcher,
234 })
235​
236 const query = reactive(useQuery(options))
237​
238 if (query.isSuccess) {
239 expectTypeOf(query.data).toEqualTypeOf<string>()
240 }
241 })
242​
243 it('should accept computed options', () => {
244 const options = computed(() => ({
245 queryKey: ['key'],
246 queryFn: simpleFetcher,
247 }))
248​
249 const query = reactive(useQuery(options))
250​
251 if (query.isSuccess) {
252 expectTypeOf(query.data).toEqualTypeOf<string>()
253 }
254 })
255​
256 it('should accept computed query options', () => {
257 const options = computed(() =>
258 queryOptions({
259 queryKey: ['key'],
260 queryFn: simpleFetcher,
261 }),
262 )
263​
264 const query = reactive(useQuery(options))
265​
266 if (query.isSuccess) {
267 expectTypeOf(query.data).toEqualTypeOf<string>()
268 }
269 })
270 })
271})