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