Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

vueQueryPlugin.test.ts

404 lines11.5 KB

1import { afterEach, beforeEach, describe, expect, test, 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
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
45​
46describe('VueQueryPlugin', () => {
47 beforeEach(() => {
48 vi.useFakeTimers()
49 })
50​
51 afterEach(() => {
52 vi.useRealTimers()
53 })
54​
55 describe('devtools', () => {
56 test('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 testIf(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 testIf(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 testIf(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 testIf(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 test('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 test('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 testIf(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 testIf(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 testIf(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 testIf(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 testIf(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 testIf(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 testIf(isVue2)(
225 'should instantiate a client with the provided config',
226 () => {
227 const appMock = getAppMock()
228 const config = {
229 defaultOptions: { queries: { enabled: true } },
230 }
231 VueQueryPlugin.install(appMock, {
232 queryClientConfig: config,
233 })
234​
235 appMock._mixin.beforeCreate?.call(appMock)
236 const client = appMock._provided.VUE_QUERY_CLIENT as QueryClient
237 const defaultOptions = client.getDefaultOptions()
238​
239 expect(defaultOptions).toEqual(config.defaultOptions)
240 },
241 )
242​
243 testIf(isVue3)(
244 'should instantiate a client with the provided config',
245 () => {
246 const appMock = getAppMock()
247 const config = {
248 defaultOptions: { queries: { enabled: true } },
249 }
250 VueQueryPlugin.install(appMock, {
251 queryClientConfig: config,
252 })
253​
254 const client = (appMock.provide as Mock).mock.calls[0]?.[1]
255 const defaultOptions = client.getDefaultOptions()
256​
257 expect(defaultOptions).toEqual(config.defaultOptions)
258 },
259 )
260 })
261​
262 describe('when persister is provided', () => {
263 test('should properly modify isRestoring flag on queryClient', async () => {
264 const appMock = getAppMock()
265 const customClient = {
266 mount: vi.fn(),
267 isRestoring: ref(false),
268 } as unknown as QueryClient
269​
270 VueQueryPlugin.install(appMock, {
271 queryClient: customClient,
272 clientPersister: () => [
273 vi.fn(),
274 new Promise((resolve) => {
275 resolve()
276 }),
277 ],
278 })
279​
280 expect(customClient.isRestoring?.value).toBeTruthy()
281​
282 await vi.advanceTimersByTimeAsync(0)
283​
284 expect(customClient.isRestoring?.value).toBeFalsy()
285 })
286​
287 test('should delay useQuery subscription and not call fetcher if data is not stale', async () => {
288 const key = queryKey()
289 const appMock = getAppMock()
290 const customClient = new QueryClient({
291 defaultOptions: {
292 queries: {
293 staleTime: 1000 * 60 * 60,
294 },
295 },
296 })
297​
298 VueQueryPlugin.install(appMock, {
299 queryClient: customClient,
300 clientPersister: (client) => [
301 vi.fn(),
302 new Promise((resolve) => {
303 setTimeout(() => {
304 client.setQueryData(key, () => ({
305 foo: 'bar',
306 }))
307 resolve()
308 }, 0)
309 }),
310 ],
311 })
312​
313 const fnSpy = vi.fn()
314​
315 const query = useQuery(
316 {
317 queryKey: key,
318 queryFn: fnSpy,
319 },
320 customClient,
321 )
322​
323 expect(customClient.isRestoring?.value).toBeTruthy()
324 expect(query.isFetching.value).toBeFalsy()
325 expect(query.data.value).toStrictEqual(undefined)
326 expect(fnSpy).toHaveBeenCalledTimes(0)
327​
328 await vi.advanceTimersByTimeAsync(0)
329​
330 expect(customClient.isRestoring?.value).toBeFalsy()
331 expect(query.data.value).toStrictEqual({ foo: 'bar' })
332 expect(fnSpy).toHaveBeenCalledTimes(0)
333 })
334​
335 test('should delay useQueries subscription and not call fetcher if data is not stale', async () => {
336 const key1 = queryKey()
337 const key2 = queryKey()
338 const appMock = getAppMock()
339 const customClient = new QueryClient({
340 defaultOptions: {
341 queries: {
342 staleTime: 1000 * 60 * 60,
343 },
344 },
345 })
346​
347 VueQueryPlugin.install(appMock, {
348 queryClient: customClient,
349 clientPersister: (client) => [
350 vi.fn(),
351 new Promise((resolve) => {
352 setTimeout(() => {
353 client.setQueryData(key1, () => ({
354 foo1: 'bar1',
355 }))
356 client.setQueryData(key2, () => ({
357 foo2: 'bar2',
358 }))
359 resolve()
360 }, 0)
361 }),
362 ],
363 })
364​
365 const fnSpy = vi.fn()
366​
367 const query = useQuery(
368 {
369 queryKey: key1,
370 queryFn: fnSpy,
371 },
372 customClient,
373 )
374​
375 const queries = useQueries(
376 {
377 queries: [
378 {
379 queryKey: key2,
380 queryFn: fnSpy,
381 },
382 ],
383 },
384 customClient,
385 )
386​
387 expect(customClient.isRestoring?.value).toBeTruthy()
388​
389 expect(query.isFetching.value).toBeFalsy()
390 expect(query.data.value).toStrictEqual(undefined)
391​
392 expect(queries.value[0].isFetching).toBeFalsy()
393 expect(queries.value[0].data).toStrictEqual(undefined)
394 expect(fnSpy).toHaveBeenCalledTimes(0)
395​
396 await vi.advanceTimersByTimeAsync(0)
397​
398 expect(customClient.isRestoring?.value).toBeFalsy()
399 expect(query.data.value).toStrictEqual({ foo1: 'bar1' })
400 expect(queries.value[0].data).toStrictEqual({ foo2: 'bar2' })
401 expect(fnSpy).toHaveBeenCalledTimes(0)
402 })
403 })
404})