@tanstack/vue-query 5.59.17 → 5.59.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,393 +0,0 @@
1
- import { describe, expect, test, vi } from 'vitest'
2
- import { isVue2, isVue3, ref } from 'vue-demi'
3
- import { QueryClient } from '../queryClient'
4
- import { VueQueryPlugin } from '../vueQueryPlugin'
5
- import { VUE_QUERY_CLIENT } from '../utils'
6
- import { setupDevtools } from '../devtools/devtools'
7
- import { useQuery } from '../useQuery'
8
- import { useQueries } from '../useQueries'
9
- import { flushPromises } from './test-utils'
10
- import type { App, ComponentOptions } from 'vue'
11
- import type { Mock } from 'vitest'
12
-
13
- vi.mock('../devtools/devtools')
14
- vi.mock('../useQueryClient')
15
- vi.mock('../useBaseQuery')
16
-
17
- type UnmountCallback = () => void
18
-
19
- interface TestApp extends App {
20
- onUnmount: UnmountCallback
21
- _unmount: UnmountCallback
22
- _mixin: ComponentOptions
23
- _provided: Record<string, any>
24
- $root: TestApp
25
- }
26
-
27
- const testIf = (condition: boolean) => (condition ? test : test.skip)
28
-
29
- function 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
-
46
- describe('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
- })