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