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