-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathusePrefetchInfiniteQuery.test.ts
More file actions
157 lines (137 loc) · 4.48 KB
/
usePrefetchInfiniteQuery.test.ts
File metadata and controls
157 lines (137 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { nextTick, ref } from 'vue-demi'
import { QueryClient } from '../queryClient'
import { usePrefetchInfiniteQuery } from '../usePrefetchInfiniteQuery'
describe('usePrefetchInfiniteQuery', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
test('should prefetch infinite query if query state does not exist', () => {
const queryClient = new QueryClient()
const prefetchInfiniteQuerySpy = vi.spyOn(
queryClient,
'prefetchInfiniteQuery',
)
const queryFn = vi.fn(() =>
Promise.resolve({ data: 'prefetched', currentPage: 1 }),
)
usePrefetchInfiniteQuery(
{
queryKey: ['prefetch-infinite-query'],
queryFn,
initialPageParam: 1,
getNextPageParam: () => undefined,
},
queryClient,
)
expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1)
expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith({
queryKey: ['prefetch-infinite-query'],
queryFn,
initialPageParam: 1,
getNextPageParam: expect.any(Function),
})
})
test('should not prefetch infinite query if query state exists', () => {
const queryClient = new QueryClient()
const prefetchInfiniteQuerySpy = vi.spyOn(
queryClient,
'prefetchInfiniteQuery',
)
const queryFn = vi.fn(() =>
Promise.resolve({ data: 'prefetched', currentPage: 1 }),
)
queryClient.setQueryData(['prefetch-infinite-query-existing'], {
pages: [{ data: 'existing', currentPage: 1 }],
pageParams: [1],
})
usePrefetchInfiniteQuery(
{
queryKey: ['prefetch-infinite-query-existing'],
queryFn,
initialPageParam: 1,
getNextPageParam: () => undefined,
},
queryClient,
)
expect(prefetchInfiniteQuerySpy).not.toHaveBeenCalled()
})
test('should unwrap refs in infinite query options', () => {
const queryClient = new QueryClient()
const prefetchInfiniteQuerySpy = vi.spyOn(
queryClient,
'prefetchInfiniteQuery',
)
const nestedRef = ref('value')
usePrefetchInfiniteQuery(
{
queryKey: ['prefetch-infinite-query-ref', nestedRef],
queryFn: () => Promise.resolve({ data: 'prefetched', currentPage: 1 }),
initialPageParam: 1,
getNextPageParam: () => undefined,
},
queryClient,
)
expect(prefetchInfiniteQuerySpy).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: ['prefetch-infinite-query-ref', 'value'],
}),
)
})
test('should prefetch infinite query again when query key changes reactively', async () => {
const queryClient = new QueryClient()
const prefetchInfiniteQuerySpy = vi.spyOn(
queryClient,
'prefetchInfiniteQuery',
)
const keyRef = ref('first')
usePrefetchInfiniteQuery(
() => ({
queryKey: ['prefetch-infinite-query-reactive', keyRef.value],
queryFn: () => Promise.resolve({ data: keyRef.value, currentPage: 1 }),
initialPageParam: 1,
getNextPageParam: () => undefined,
}),
queryClient,
)
expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(1)
expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith(
expect.objectContaining({
queryKey: ['prefetch-infinite-query-reactive', 'first'],
}),
)
keyRef.value = 'second'
await nextTick()
expect(prefetchInfiniteQuerySpy).toHaveBeenCalledTimes(2)
expect(prefetchInfiniteQuerySpy).toHaveBeenLastCalledWith(
expect.objectContaining({
queryKey: ['prefetch-infinite-query-reactive', 'second'],
}),
)
})
test('should warn when used outside of setup function in development mode', () => {
vi.stubEnv('NODE_ENV', 'development')
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
try {
usePrefetchInfiniteQuery(
{
queryKey: ['outside-scope-prefetch-infinite-query'],
queryFn: () =>
Promise.resolve({ data: 'prefetched', currentPage: 1 }),
initialPageParam: 1,
getNextPageParam: () => undefined,
},
new QueryClient(),
)
expect(warnSpy).toHaveBeenCalledWith(
'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.',
)
} finally {
warnSpy.mockRestore()
vi.unstubAllEnvs()
}
})
})