Open History to see the last commit
1import { describe, expect, test, vi } from 'vitest'
2import { isVue2, isVue3, ref } from 'vue-demi'
3import { QueryClient } from '../queryClient'
4import { VueQueryPlugin } from '../vueQueryPlugin'
5import { VUE_QUERY_CLIENT } from '../utils'
6import { setupDevtools } from '../devtools/devtools'
7import { useQuery } from '../useQuery'
8import { useQueries } from '../useQueries'
9import { flushPromises } from './test-utils'
10import type { App, ComponentOptions } from 'vue'
11import type { Mock } from 'vitest'
12β
13vi.mock('../devtools/devtools')
14vi.mock('../useQueryClient')
15vi.mock('../useBaseQuery')
16β
17interface TestApp extends App {
18 onUnmount: Function
19 _unmount: Function
20 _mixin: ComponentOptions
21 _provided: Record<string, any>
22 $root: TestApp
23}
24β
25const testIf = (condition: boolean) => (condition ? test : test.skip)
26β
27function getAppMock(withUnmountHook = false): TestApp {
28 const mock = {
29 provide: vi.fn(),
30 unmount: vi.fn(),
31 onUnmount: withUnmountHook
32 ? vi.fn((u: Function) => {
33 mock._unmount = u
34 })
35 : undefined,
36 mixin: (m: ComponentOptions): any => {
37 mock._mixin = m
38 },
39 } as unknown as TestApp
40β
41 return mock
42}
43β
44describe('VueQueryPlugin', () => {
45 describe('devtools', () => {
46 test('should NOT setup devtools', () => {
47 const setupDevtoolsMock = setupDevtools as Mock
48 const appMock = getAppMock()
49 VueQueryPlugin.install(appMock)
50β
51 expect(setupDevtoolsMock).toHaveBeenCalledTimes(0)
52 })
53β
54 testIf(isVue2)('should NOT setup devtools by default', () => {
55 const envCopy = process.env.NODE_ENV
56 process.env.NODE_ENV = 'development'
57 const setupDevtoolsMock = setupDevtools as Mock
58 const appMock = getAppMock()
59 VueQueryPlugin.install(appMock)
60β
61 appMock.$root = appMock
62 appMock._mixin.beforeCreate?.call(appMock)
63 process.env.NODE_ENV = envCopy
64β
65 expect(setupDevtoolsMock).toHaveBeenCalledTimes(0)
66 })
67β
68 testIf(isVue2)('should setup devtools', () => {
69 const envCopy = process.env.NODE_ENV
70 process.env.NODE_ENV = 'development'
71 const setupDevtoolsMock = setupDevtools as Mock
72 const appMock = getAppMock()
73 VueQueryPlugin.install(appMock, { enableDevtoolsV6Plugin: true })
74β
75 appMock.$root = appMock
76 appMock._mixin.beforeCreate?.call(appMock)
77 process.env.NODE_ENV = envCopy
78β
79 expect(setupDevtoolsMock).toHaveBeenCalledTimes(1)
80 })
81β
82 testIf(isVue3)('should NOT setup devtools by default', () => {
83 const envCopy = process.env.NODE_ENV
84 process.env.NODE_ENV = 'development'
85 const setupDevtoolsMock = setupDevtools as Mock
86 const appMock = getAppMock()
87 VueQueryPlugin.install(appMock)
88 process.env.NODE_ENV = envCopy
89β
90 expect(setupDevtoolsMock).toHaveBeenCalledTimes(0)
91 })
92β
93 testIf(isVue3)('should setup devtools', () => {
94 const envCopy = process.env.NODE_ENV
95 process.env.NODE_ENV = 'development'
96 const setupDevtoolsMock = setupDevtools as Mock
97 const appMock = getAppMock()
98 VueQueryPlugin.install(appMock, { enableDevtoolsV6Plugin: true })
99 process.env.NODE_ENV = envCopy
100β
101 expect(setupDevtoolsMock).toHaveBeenCalledTimes(1)
102 })
103 })
104β
105 describe('when app unmounts', () => {
106 test('should call unmount on each client when onUnmount is missing', () => {
107 const appMock = getAppMock()
108 const customClient = {
109 mount: vi.fn(),
110 unmount: vi.fn(),
111 } as unknown as QueryClient
112 const originalUnmount = appMock.unmount
113 VueQueryPlugin.install(appMock, {
114 queryClient: customClient,
115 })
116β
117 appMock.unmount()
118β
119 expect(appMock.unmount).not.toEqual(originalUnmount)
120 expect(customClient.unmount).toHaveBeenCalledTimes(1)
121 expect(originalUnmount).toHaveBeenCalledTimes(1)
122 })
123β
124 test('should call onUnmount if present', () => {
125 const appMock = getAppMock(true)
126 const customClient = {
127 mount: vi.fn(),
128 unmount: vi.fn(),
129 } as unknown as QueryClient
130 const originalUnmount = appMock.unmount
131 VueQueryPlugin.install(appMock, { queryClient: customClient })
132β
133 appMock._unmount()
134β
135 expect(appMock.unmount).toEqual(originalUnmount)
136 expect(customClient.unmount).toHaveBeenCalledTimes(1)
137 })
138 })
139β
140 describe('when called without additional options', () => {
141 testIf(isVue2)('should provide a client with default clientKey', () => {
142 const appMock = getAppMock()
143 VueQueryPlugin.install(appMock)
144β
145 appMock._mixin.beforeCreate?.call(appMock)
146β
147 expect(appMock._provided).toMatchObject({
148 VUE_QUERY_CLIENT: expect.any(QueryClient),
149 })
150 })
151β
152 testIf(isVue3)('should provide a client with default clientKey', () => {
153 const appMock = getAppMock()
154 VueQueryPlugin.install(appMock)
155β
156 expect(appMock.provide).toHaveBeenCalledWith(
157 VUE_QUERY_CLIENT,
158 expect.any(QueryClient),
159 )
160 })
161 })
162β
163 describe('when called with custom clientKey', () => {
164 testIf(isVue2)('should provide a client with customized clientKey', () => {
165 const appMock = getAppMock()
166 VueQueryPlugin.install(appMock, { queryClientKey: 'CUSTOM' })
167β
168 appMock._mixin.beforeCreate?.call(appMock)
169β
170 expect(appMock._provided).toMatchObject({
171 [VUE_QUERY_CLIENT + ':CUSTOM']: expect.any(QueryClient),
172 })
173 })
174β
175 testIf(isVue3)('should provide a client with customized clientKey', () => {
176 const appMock = getAppMock()
177 VueQueryPlugin.install(appMock, { queryClientKey: 'CUSTOM' })
178β
179 expect(appMock.provide).toHaveBeenCalledWith(
180 VUE_QUERY_CLIENT + ':CUSTOM',
181 expect.any(QueryClient),
182 )
183 })
184 })
185β
186 describe('when called with custom client', () => {
187 testIf(isVue2)('should provide that custom client', () => {
188 const appMock = getAppMock()
189 const customClient = { mount: vi.fn() } as unknown as QueryClient
190 VueQueryPlugin.install(appMock, { queryClient: customClient })
191β
192 appMock._mixin.beforeCreate?.call(appMock)
193β
194 expect(customClient.mount).toHaveBeenCalled()
195 expect(appMock._provided).toMatchObject({
196 VUE_QUERY_CLIENT: customClient,
197 })
198 })
199β
200 testIf(isVue3)('should provide that custom client', () => {
201 const appMock = getAppMock()
202 const customClient = { mount: vi.fn() } as unknown as QueryClient
203 VueQueryPlugin.install(appMock, { queryClient: customClient })
204β
205 expect(customClient.mount).toHaveBeenCalled()
206 expect(appMock.provide).toHaveBeenCalledWith(
207 VUE_QUERY_CLIENT,
208 customClient,
209 )
210 })
211 })
212β
213 describe('when called with custom client config', () => {
214 testIf(isVue2)(
215 'should instantiate a client with the provided config',
216 () => {
217 const appMock = getAppMock()
218 const config = {
219 defaultOptions: { queries: { enabled: true } },
220 }
221 VueQueryPlugin.install(appMock, {
222 queryClientConfig: config,
223 })
224β
225 appMock._mixin.beforeCreate?.call(appMock)
226 const client = appMock._provided.VUE_QUERY_CLIENT as QueryClient
227 const defaultOptions = client.getDefaultOptions()
228β
229 expect(defaultOptions).toEqual(config.defaultOptions)
230 },
231 )
232β
233 testIf(isVue3)(
234 'should instantiate a client with the provided config',
235 () => {
236 const appMock = getAppMock()
237 const config = {
238 defaultOptions: { queries: { enabled: true } },
239 }
240 VueQueryPlugin.install(appMock, {
241 queryClientConfig: config,
242 })
243β
244 const client = (appMock.provide as Mock).mock.calls[0][1]
245 const defaultOptions = client.getDefaultOptions()
246β
247 expect(defaultOptions).toEqual(config.defaultOptions)
248 },
249 )
250 })
251β
252 describe('when persister is provided', () => {
253 test('should properly modify isRestoring flag on queryClient', async () => {
254 const appMock = getAppMock()
255 const customClient = {
256 mount: vi.fn(),
257 isRestoring: ref(false),
258 } as unknown as QueryClient
259β
260 VueQueryPlugin.install(appMock, {
261 queryClient: customClient,
262 clientPersister: () => [
263 vi.fn(),
264 new Promise((resolve) => {
265 resolve()
266 }),
267 ],
268 })
269β
270 expect(customClient.isRestoring.value).toBeTruthy()
271β
272 await flushPromises()
273β
274 expect(customClient.isRestoring.value).toBeFalsy()
275 })
276β
277 test('should delay useQuery subscription and not call fetcher if data is not stale', async () => {
278 const appMock = getAppMock()
279 const customClient = new QueryClient({
280 defaultOptions: {
281 queries: {
282 staleTime: 1000 * 60 * 60,
283 },
284 },
285 })
286β
287 VueQueryPlugin.install(appMock, {
288 queryClient: customClient,
289 clientPersister: (client) => [
290 vi.fn(),
291 new Promise((resolve) => {
292 setTimeout(() => {
293 client.setQueryData(['persist'], () => ({
294 foo: 'bar',
295 }))
296 resolve()
297 }, 0)
298 }),
299 ],
300 })
301β
302 const fnSpy = vi.fn()
303β
304 const query = useQuery(
305 {
306 queryKey: ['persist'],
307 queryFn: fnSpy,
308 },
309 customClient,
310 )
311β
312 expect(customClient.isRestoring.value).toBeTruthy()
313 expect(query.isFetching.value).toBeFalsy()
314 expect(query.data.value).toStrictEqual(undefined)
315 expect(fnSpy).toHaveBeenCalledTimes(0)
316β
317 await flushPromises()
318β
319 expect(customClient.isRestoring.value).toBeFalsy()
320 expect(query.data.value).toStrictEqual({ foo: 'bar' })
321 expect(fnSpy).toHaveBeenCalledTimes(0)
322 })
323β
324 test('should delay useQueries subscription and not call fetcher if data is not stale', async () => {
325 const appMock = getAppMock()
326 const customClient = new QueryClient({
327 defaultOptions: {
328 queries: {
329 staleTime: 1000 * 60 * 60,
330 },
331 },
332 })
333β
334 VueQueryPlugin.install(appMock, {
335 queryClient: customClient,
336 clientPersister: (client) => [
337 vi.fn(),
338 new Promise((resolve) => {
339 setTimeout(() => {
340 client.setQueryData(['persist1'], () => ({
341 foo1: 'bar1',
342 }))
343 client.setQueryData(['persist2'], () => ({
344 foo2: 'bar2',
345 }))
346 resolve()
347 }, 0)
348 }),
349 ],
350 })
351β
352 const fnSpy = vi.fn()
353β
354 const query = useQuery(
355 {
356 queryKey: ['persist1'],
357 queryFn: fnSpy,
358 },
359 customClient,
360 )
361β
362 const queries = useQueries(
363 {
364 queries: [
365 {
366 queryKey: ['persist2'],
367 queryFn: fnSpy,
368 },
369 ],
370 },
371 customClient,
372 )
373β
374 expect(customClient.isRestoring.value).toBeTruthy()
375β
376 expect(query.isFetching.value).toBeFalsy()
377 expect(query.data.value).toStrictEqual(undefined)
378β
379 expect(queries.value[0].isFetching).toBeFalsy()
380 expect(queries.value[0].data).toStrictEqual(undefined)
381 expect(fnSpy).toHaveBeenCalledTimes(0)
382β
383 await flushPromises()
384β
385 expect(customClient.isRestoring.value).toBeFalsy()
386 expect(query.data.value).toStrictEqual({ foo1: 'bar1' })
387 expect(queries.value[0].data).toStrictEqual({ foo2: 'bar2' })
388 expect(fnSpy).toHaveBeenCalledTimes(0)
389 })
390 })
391})