-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathusePrefetchQueries.test.tsx
196 lines (163 loc) · 5.31 KB
/
usePrefetchQueries.test.tsx
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { describe, expect, it, vi } from 'vitest'
import React from 'react'
import { waitFor } from '@testing-library/react'
import { QueryCache, usePrefetchQueries, useSuspenseQueries } from '..'
import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
import type { UseSuspenseQueryOptions } from '..'
const generateQueryFn = (data: string) =>
vi
.fn<(...args: Array<any>) => Promise<string>>()
.mockImplementation(async () => {
await sleep(10)
return data
})
describe('usePrefetchQueries', () => {
const queryCache = new QueryCache()
const queryClient = createQueryClient({ queryCache })
function Suspended<TData = unknown>(props: {
queriesOpts: Array<
UseSuspenseQueryOptions<TData, Error, TData, Array<string>>
>
children?: React.ReactNode
}) {
const state = useSuspenseQueries({
queries: props.queriesOpts,
combine: (results) => results.map((r) => r.data),
})
return (
<div>
<div>data: {state.map((data) => String(data)).join(', ')}</div>
{props.children}
</div>
)
}
it('should prefetch queries if query states do not exist', async () => {
const queryOpts1 = {
queryKey: queryKey(),
queryFn: generateQueryFn('prefetchQuery1'),
}
const queryOpts2 = {
queryKey: queryKey(),
queryFn: generateQueryFn('prefetchQuery2'),
}
const componentQueryOpts1 = {
...queryOpts1,
queryFn: generateQueryFn('useSuspenseQuery1'),
}
const componentQueryOpts2 = {
...queryOpts2,
queryFn: generateQueryFn('useSuspenseQuery2'),
}
function App() {
usePrefetchQueries({
queries: [queryOpts1, queryOpts2],
})
return (
<React.Suspense fallback="Loading...">
<Suspended queriesOpts={[componentQueryOpts1, componentQueryOpts2]} />
</React.Suspense>
)
}
const rendered = renderWithClient(queryClient, <App />)
await waitFor(() =>
rendered.getByText('data: prefetchQuery1, prefetchQuery2'),
)
expect(queryOpts1.queryFn).toHaveBeenCalledTimes(1)
expect(queryOpts2.queryFn).toHaveBeenCalledTimes(1)
})
it('should not prefetch queries if query states exist', async () => {
const queryOpts1 = {
queryKey: queryKey(),
queryFn: generateQueryFn('The usePrefetchQueries hook is smart! 1'),
}
const queryOpts2 = {
queryKey: queryKey(),
queryFn: generateQueryFn('The usePrefetchQueries hook is smart! 2'),
}
function App() {
usePrefetchQueries({
queries: [queryOpts1, queryOpts2],
})
return (
<React.Suspense fallback="Loading...">
<Suspended queriesOpts={[queryOpts1, queryOpts2]} />
</React.Suspense>
)
}
await queryClient.fetchQuery(queryOpts1)
await queryClient.fetchQuery(queryOpts2)
queryOpts1.queryFn.mockClear()
queryOpts2.queryFn.mockClear()
const rendered = renderWithClient(queryClient, <App />)
expect(rendered.queryByText('fetching: true')).not.toBeInTheDocument()
await waitFor(() =>
rendered.getByText(
'data: The usePrefetchQueries hook is smart! 1, The usePrefetchQueries hook is smart! 2',
),
)
expect(queryOpts1.queryFn).not.toHaveBeenCalled()
expect(queryOpts2.queryFn).not.toHaveBeenCalled()
})
it('should only prefetch queries that do not exist', async () => {
const queryOpts1 = {
queryKey: queryKey(),
queryFn: generateQueryFn('The usePrefetchQueries hook is smart! 1'),
}
const queryOpts2 = {
queryKey: queryKey(),
queryFn: generateQueryFn('The usePrefetchQueries hook is smart! 2'),
}
function App() {
usePrefetchQueries({
queries: [queryOpts1, queryOpts2],
})
return (
<React.Suspense fallback="Loading...">
<Suspended queriesOpts={[queryOpts1, queryOpts2]} />
</React.Suspense>
)
}
await queryClient.fetchQuery(queryOpts1)
queryOpts1.queryFn.mockClear()
queryOpts2.queryFn.mockClear()
const rendered = renderWithClient(queryClient, <App />)
await waitFor(() =>
rendered.getByText(
'data: The usePrefetchQueries hook is smart! 1, The usePrefetchQueries hook is smart! 2',
),
)
expect(queryOpts1.queryFn).not.toHaveBeenCalled()
expect(queryOpts2.queryFn).toHaveBeenCalledTimes(1)
})
it('should not create an endless loop when using inside a suspense boundary', async () => {
const queryOpts1 = {
queryKey: queryKey(),
queryFn: generateQueryFn('prefetchedQuery1'),
}
const queryOpts2 = {
queryKey: queryKey(),
queryFn: generateQueryFn('prefetchedQuery2'),
}
function Prefetch({ children }: { children: React.ReactNode }) {
usePrefetchQueries({
queries: [queryOpts1, queryOpts2],
})
return <>{children}</>
}
function App() {
return (
<React.Suspense>
<Prefetch>
<Suspended queriesOpts={[queryOpts1, queryOpts2]} />
</Prefetch>
</React.Suspense>
)
}
const rendered = renderWithClient(queryClient, <App />)
await waitFor(() =>
rendered.getByText('data: prefetchedQuery1, prefetchedQuery2'),
)
expect(queryOpts1.queryFn).toHaveBeenCalledTimes(1)
expect(queryOpts2.queryFn).toHaveBeenCalledTimes(1)
})
})