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

useQuery.test.ts

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