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