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

useQuery.test.ts

309 lines7.6 KB

1import {
2 computed,
3 reactive,
4 ref,
5 onScopeDispose,
6 getCurrentInstance,
7} from 'vue-demi'
8import { QueryObserver } from '@tanstack/query-core'
9​
10import {
11 flushPromises,
12 rejectFetcher,
13 simpleFetcher,
14 getSimpleFetcherWithReturnData,
15} from './test-utils'
16import { useQuery } from '../useQuery'
17import { useBaseQuery } from '../useBaseQuery'
18​
19jest.mock('../useQueryClient')
20jest.mock('../useBaseQuery')
21​
22describe('useQuery', () => {
23 test('should properly execute query', () => {
24 useQuery({ queryKey: ['key0'], queryFn: simpleFetcher, staleTime: 1000 })
25​
26 expect(useBaseQuery).toBeCalledWith(
27 QueryObserver,
28 {
29 queryKey: ['key0'],
30 queryFn: simpleFetcher,
31 staleTime: 1000,
32 },
33 undefined,
34 )
35 })
36​
37 test('should return pending status initially', () => {
38 const query = useQuery({ queryKey: ['key1'], queryFn: simpleFetcher })
39​
40 expect(query).toMatchObject({
41 status: { value: 'pending' },
42 isPending: { value: true },
43 isFetching: { value: true },
44 isStale: { value: true },
45 })
46 })
47​
48 test('should resolve to success and update reactive state: useQuery(key, dataFn)', async () => {
49 const query = useQuery({
50 queryKey: ['key2'],
51 queryFn: getSimpleFetcherWithReturnData('result2'),
52 })
53​
54 await flushPromises()
55​
56 expect(query).toMatchObject({
57 status: { value: 'success' },
58 data: { value: 'result2' },
59 isPending: { value: false },
60 isFetching: { value: false },
61 isFetched: { value: true },
62 isSuccess: { value: true },
63 })
64 })
65​
66 test('should resolve to success and update reactive state: useQuery(optionsObj)', async () => {
67 const query = useQuery({
68 queryKey: ['key31'],
69 queryFn: getSimpleFetcherWithReturnData('result31'),
70 enabled: true,
71 })
72​
73 await flushPromises()
74​
75 expect(query).toMatchObject({
76 status: { value: 'success' },
77 data: { value: 'result31' },
78 isPending: { value: false },
79 isFetching: { value: false },
80 isFetched: { value: true },
81 isSuccess: { value: true },
82 })
83 })
84​
85 test('should resolve to success and update reactive state: useQuery(key, optionsObj)', async () => {
86 const query = useQuery({
87 queryKey: ['key32'],
88 queryFn: getSimpleFetcherWithReturnData('result32'),
89 enabled: true,
90 })
91​
92 await flushPromises()
93​
94 expect(query).toMatchObject({
95 status: { value: 'success' },
96 data: { value: 'result32' },
97 isPending: { value: false },
98 isFetching: { value: false },
99 isFetched: { value: true },
100 isSuccess: { value: true },
101 })
102 })
103​
104 test('should reject and update reactive state', async () => {
105 const query = useQuery({
106 queryKey: ['key3'],
107 queryFn: rejectFetcher,
108 })
109​
110 await flushPromises()
111​
112 expect(query).toMatchObject({
113 status: { value: 'error' },
114 data: { value: undefined },
115 error: { value: { message: 'Some error' } },
116 isPending: { value: false },
117 isFetching: { value: false },
118 isFetched: { value: true },
119 isError: { value: true },
120 failureCount: { value: 1 },
121 failureReason: { value: { message: 'Some error' } },
122 })
123 })
124​
125 test('should update query on reactive options object change', async () => {
126 const spy = jest.fn()
127 const onSuccess = ref(() => {
128 // Noop
129 })
130 useQuery(
131 reactive({
132 queryKey: ['key6'],
133 queryFn: simpleFetcher,
134 onSuccess,
135 staleTime: 1000,
136 }),
137 )
138​
139 onSuccess.value = spy
140​
141 await flushPromises()
142​
143 expect(spy).toBeCalledTimes(1)
144 })
145​
146 test('should update query on reactive (Ref) key change', async () => {
147 const secondKeyRef = ref('key7')
148 const query = useQuery({
149 queryKey: ['key6', secondKeyRef],
150 queryFn: simpleFetcher,
151 })
152​
153 await flushPromises()
154​
155 expect(query).toMatchObject({
156 status: { value: 'success' },
157 })
158​
159 secondKeyRef.value = 'key8'
160 await flushPromises()
161​
162 expect(query).toMatchObject({
163 status: { value: 'pending' },
164 data: { value: undefined },
165 })
166​
167 await flushPromises()
168​
169 expect(query).toMatchObject({
170 status: { value: 'success' },
171 })
172 })
173​
174 test("should update query when an option is passed as Ref and it's changed", async () => {
175 const enabled = ref(false)
176 const query = useQuery({
177 queryKey: ['key9'],
178 queryFn: simpleFetcher,
179 enabled,
180 })
181​
182 await flushPromises()
183​
184 expect(query).toMatchObject({
185 fetchStatus: { value: 'idle' },
186 data: { value: undefined },
187 })
188​
189 enabled.value = true
190​
191 await flushPromises()
192​
193 expect(query).toMatchObject({
194 fetchStatus: { value: 'fetching' },
195 data: { value: undefined },
196 })
197​
198 await flushPromises()
199​
200 expect(query).toMatchObject({
201 status: { value: 'success' },
202 })
203 })
204​
205 test('should properly execute dependant queries', async () => {
206 const { data } = useQuery({
207 queryKey: ['dependant1'],
208 queryFn: simpleFetcher,
209 })
210​
211 const enabled = computed(() => !!data.value)
212​
213 const { fetchStatus, status } = useQuery(
214 reactive({
215 queryKey: ['dependant2'],
216 queryFn: simpleFetcher,
217 enabled,
218 }),
219 )
220​
221 expect(data.value).toStrictEqual(undefined)
222 expect(fetchStatus.value).toStrictEqual('idle')
223​
224 await flushPromises()
225​
226 expect(data.value).toStrictEqual('Some data')
227 expect(fetchStatus.value).toStrictEqual('fetching')
228​
229 await flushPromises()
230​
231 expect(fetchStatus.value).toStrictEqual('idle')
232 expect(status.value).toStrictEqual('success')
233 })
234​
235 test('should stop listening to changes on onScopeDispose', async () => {
236 const onScopeDisposeMock = onScopeDispose as jest.MockedFunction<
237 typeof onScopeDispose
238 >
239 onScopeDisposeMock.mockImplementationOnce((fn) => fn())
240​
241 const { status } = useQuery({
242 queryKey: ['onScopeDispose'],
243 queryFn: simpleFetcher,
244 })
245​
246 expect(status.value).toStrictEqual('pending')
247​
248 await flushPromises()
249​
250 expect(status.value).toStrictEqual('pending')
251​
252 await flushPromises()
253​
254 expect(status.value).toStrictEqual('pending')
255 })
256​
257 describe('suspense', () => {
258 test('should return a Promise', () => {
259 const getCurrentInstanceSpy = getCurrentInstance as jest.Mock
260 getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
261​
262 const query = useQuery({ queryKey: ['suspense'], queryFn: simpleFetcher })
263 const result = query.suspense()
264​
265 expect(result).toBeInstanceOf(Promise)
266 })
267​
268 test('should resolve after being enabled', () => {
269 const getCurrentInstanceSpy = getCurrentInstance as jest.Mock
270 getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
271​
272 let afterTimeout = false
273 const isEnabeld = ref(false)
274 const query = useQuery({
275 queryKey: ['suspense'],
276 queryFn: simpleFetcher,
277 enabled: isEnabeld,
278 })
279​
280 setTimeout(() => {
281 afterTimeout = true
282 isEnabeld.value = true
283 }, 200)
284​
285 return query.suspense().then(() => {
286 expect(afterTimeout).toBe(true)
287 })
288 })
289​
290 test('should resolve immidiately when stale without refetching', () => {
291 const getCurrentInstanceSpy = getCurrentInstance as jest.Mock
292 getCurrentInstanceSpy.mockImplementation(() => ({ suspense: {} }))
293​
294 const fetcherSpy = jest.fn(() => simpleFetcher())
295​
296 // let afterTimeout = false;
297 const query = useQuery({
298 queryKey: ['suspense'],
299 queryFn: simpleFetcher,
300 staleTime: 10000,
301 initialData: 'foo',
302 })
303​
304 return query.suspense().then(() => {
305 expect(fetcherSpy).toHaveBeenCalledTimes(0)
306 })
307 })
308 })
309})