Open History to see the last commit
1import { createKey } from '@tanstack/query-test-utils'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3import { nextTick, ref } from 'vue-demi'
4import { QueryClient } from '../queryClient'
5import { usePrefetchQuery } from '../usePrefetchQuery'
6β
7describe('usePrefetchQuery', () => {
8 beforeEach(() => {
9 vi.useFakeTimers()
10 })
11β
12 afterEach(() => {
13 vi.useRealTimers()
14 })
15β
16 it('should prefetch query if query state does not exist', () => {
17 const queryClient = new QueryClient()
18 const querySpy = vi.spyOn(queryClient, 'query')
19 const queryFn = () => Promise.resolve('prefetched')
20 const key = createKey()
21β
22 usePrefetchQuery(
23 {
24 queryKey: key,
25 queryFn,
26 },
27 queryClient,
28 )
29β
30 expect(querySpy).toHaveBeenCalledTimes(1)
31 expect(querySpy).toHaveBeenCalledWith({
32 queryKey: key,
33 queryFn,
34 })
35 })
36β
37 it('should not prefetch query if query state exists', () => {
38 const queryClient = new QueryClient()
39 const querySpy = vi.spyOn(queryClient, 'query')
40 const queryFn = () => Promise.resolve('prefetched')
41 const key = createKey()
42 queryClient.setQueryData(key, 'existing')
43β
44 usePrefetchQuery(
45 {
46 queryKey: key,
47 queryFn,
48 },
49 queryClient,
50 )
51β
52 expect(querySpy).not.toHaveBeenCalled()
53 })
54β
55 it('should unwrap refs in query options', () => {
56 const queryClient = new QueryClient()
57 const querySpy = vi.spyOn(queryClient, 'query')
58 const nestedRef = ref('value')
59 const key = createKey()
60 const queryFn = () => Promise.resolve('prefetched')
61β
62 usePrefetchQuery(
63 {
64 queryKey: [...key, nestedRef],
65 queryFn,
66 },
67 queryClient,
68 )
69β
70 expect(querySpy).toHaveBeenCalledWith({
71 queryKey: [...key, 'value'],
72 queryFn,
73 })
74 })
75β
76 it('should prefetch again when query key changes reactively', async () => {
77 const queryClient = new QueryClient()
78 const querySpy = vi.spyOn(queryClient, 'query')
79 const keyRef = ref('first')
80 const key = createKey()
81 const queryFn = () => Promise.resolve(keyRef.value)
82β
83 usePrefetchQuery(
84 () => ({
85 queryKey: [...key, keyRef.value],
86 queryFn,
87 }),
88 queryClient,
89 )
90β
91 expect(querySpy).toHaveBeenCalledTimes(1)
92 expect(querySpy).toHaveBeenNthCalledWith(1, {
93 queryKey: [...key, 'first'],
94 queryFn,
95 })
96β
97 keyRef.value = 'second'
98 await nextTick()
99 expect(querySpy).toHaveBeenCalledTimes(2)
100 expect(querySpy).toHaveBeenNthCalledWith(2, {
101 queryKey: [...key, 'second'],
102 queryFn,
103 })
104 })
105β
106 it('should warn when used outside of setup function in development mode', () => {
107 vi.stubEnv('NODE_ENV', 'development')
108 const consoleWarnMock = vi
109 .spyOn(console, 'warn')
110 .mockImplementation(() => {})
111β
112 usePrefetchQuery(
113 {
114 queryKey: createKey(),
115 queryFn: () => Promise.resolve('prefetched'),
116 },
117 new QueryClient(),
118 )
119β
120 expect(consoleWarnMock).toHaveBeenCalledWith(
121 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
122 )
123 })
124})