@tanstack/solid-query 4.39.0 → 4.39.2
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.
- package/build/lib/QueryClientProvider.esm.js +74 -0
- package/build/lib/QueryClientProvider.esm.js.map +1 -0
- package/build/lib/QueryClientProvider.js +80 -0
- package/build/lib/QueryClientProvider.js.map +1 -0
- package/build/lib/QueryClientProvider.mjs +74 -0
- package/build/lib/QueryClientProvider.mjs.map +1 -0
- package/build/lib/createBaseQuery.esm.js +94 -0
- package/build/lib/createBaseQuery.esm.js.map +1 -0
- package/build/lib/createBaseQuery.js +98 -0
- package/build/lib/createBaseQuery.js.map +1 -0
- package/build/lib/createBaseQuery.mjs +94 -0
- package/build/lib/createBaseQuery.mjs.map +1 -0
- package/build/lib/createInfiniteQuery.esm.js +20 -0
- package/build/lib/createInfiniteQuery.esm.js.map +1 -0
- package/build/lib/createInfiniteQuery.js +24 -0
- package/build/lib/createInfiniteQuery.js.map +1 -0
- package/build/lib/createInfiniteQuery.mjs +20 -0
- package/build/lib/createInfiniteQuery.mjs.map +1 -0
- package/build/lib/createMutation.esm.js +45 -0
- package/build/lib/createMutation.esm.js.map +1 -0
- package/build/lib/createMutation.js +49 -0
- package/build/lib/createMutation.js.map +1 -0
- package/build/lib/createMutation.mjs +45 -0
- package/build/lib/createMutation.mjs.map +1 -0
- package/build/lib/createQueries.esm.js +52 -0
- package/build/lib/createQueries.esm.js.map +1 -0
- package/build/lib/createQueries.js +56 -0
- package/build/lib/createQueries.js.map +1 -0
- package/build/lib/createQueries.mjs +52 -0
- package/build/lib/createQueries.mjs.map +1 -0
- package/build/lib/createQuery.esm.js +20 -0
- package/build/lib/createQuery.esm.js.map +1 -0
- package/build/lib/createQuery.js +24 -0
- package/build/lib/createQuery.js.map +1 -0
- package/build/lib/createQuery.mjs +20 -0
- package/build/lib/createQuery.mjs.map +1 -0
- package/build/lib/index.esm.js +9 -0
- package/build/lib/index.esm.js.map +1 -0
- package/build/lib/index.js +31 -0
- package/build/lib/index.js.map +1 -0
- package/build/lib/index.mjs +9 -0
- package/build/lib/index.mjs.map +1 -0
- package/build/lib/useIsFetching.esm.js +29 -0
- package/build/lib/useIsFetching.esm.js.map +1 -0
- package/build/lib/useIsFetching.js +33 -0
- package/build/lib/useIsFetching.js.map +1 -0
- package/build/lib/useIsFetching.mjs +29 -0
- package/build/lib/useIsFetching.mjs.map +1 -0
- package/build/lib/useIsMutating.esm.js +22 -0
- package/build/lib/useIsMutating.esm.js.map +1 -0
- package/build/lib/useIsMutating.js +26 -0
- package/build/lib/useIsMutating.js.map +1 -0
- package/build/lib/useIsMutating.mjs +22 -0
- package/build/lib/useIsMutating.mjs.map +1 -0
- package/build/lib/utils.esm.js +63 -0
- package/build/lib/utils.esm.js.map +1 -0
- package/build/lib/utils.js +72 -0
- package/build/lib/utils.js.map +1 -0
- package/build/lib/utils.mjs +63 -0
- package/build/lib/utils.mjs.map +1 -0
- package/build/solid/QueryClientProvider.jsx +49 -0
- package/build/solid/__tests__/QueryClientProvider.test.jsx +185 -0
- package/build/solid/__tests__/createInfiniteQuery.test.jsx +1458 -0
- package/build/solid/__tests__/createMutation.test.jsx +857 -0
- package/build/solid/__tests__/createQueries.test.jsx +958 -0
- package/build/solid/__tests__/createQuery.test.jsx +4559 -0
- package/build/solid/__tests__/createQuery.types.test.jsx +124 -0
- package/build/solid/__tests__/suspense.test.jsx +691 -0
- package/build/solid/__tests__/transition.test.jsx +39 -0
- package/build/solid/__tests__/useIsFetching.test.jsx +209 -0
- package/build/solid/__tests__/useIsMutating.test.jsx +216 -0
- package/build/solid/__tests__/utils.jsx +55 -0
- package/build/solid/createBaseQuery.js +81 -0
- package/build/solid/createInfiniteQuery.js +16 -0
- package/build/solid/createMutation.js +40 -0
- package/build/solid/createQueries.js +39 -0
- package/build/solid/createQuery.js +16 -0
- package/build/solid/index.js +12 -0
- package/build/solid/types.js +2 -0
- package/build/solid/useIsFetching.js +23 -0
- package/build/solid/useIsMutating.js +16 -0
- package/build/solid/utils.js +45 -0
- package/build/umd/index.development.js +3732 -0
- package/build/umd/index.development.js.map +1 -0
- package/build/umd/index.production.js +2 -0
- package/build/umd/index.production.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,958 @@
|
|
|
1
|
+
import { fireEvent, render, screen, waitFor } from 'solid-testing-library';
|
|
2
|
+
import { ErrorBoundary, createContext, createMemo, createRenderEffect, createSignal, } from 'solid-js';
|
|
3
|
+
import * as QueriesObserverModule from '../../../query-core/src/queriesObserver';
|
|
4
|
+
import { QueriesObserver, QueryCache, QueryClientProvider, createQueries, } from '..';
|
|
5
|
+
import { createQueryClient, expectType, expectTypeNotAny, queryKey, sleep, } from './utils';
|
|
6
|
+
describe('useQueries', () => {
|
|
7
|
+
const queryCache = new QueryCache();
|
|
8
|
+
const queryClient = createQueryClient({ queryCache });
|
|
9
|
+
it('should return the correct states', async () => {
|
|
10
|
+
const key1 = queryKey();
|
|
11
|
+
const key2 = queryKey();
|
|
12
|
+
const results = [];
|
|
13
|
+
function Page() {
|
|
14
|
+
const result = createQueries({
|
|
15
|
+
queries: [
|
|
16
|
+
{
|
|
17
|
+
queryKey: key1,
|
|
18
|
+
queryFn: async () => {
|
|
19
|
+
await sleep(10);
|
|
20
|
+
return 1;
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
queryKey: key2,
|
|
25
|
+
queryFn: async () => {
|
|
26
|
+
await sleep(100);
|
|
27
|
+
return 2;
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
});
|
|
32
|
+
createRenderEffect(() => {
|
|
33
|
+
results.push([...result]);
|
|
34
|
+
});
|
|
35
|
+
return (<div>
|
|
36
|
+
<div>
|
|
37
|
+
data1: {String(result[0].data ?? 'null')}, data2:{' '}
|
|
38
|
+
{String(result[1].data ?? 'null')}
|
|
39
|
+
</div>
|
|
40
|
+
</div>);
|
|
41
|
+
}
|
|
42
|
+
render(() => (<QueryClientProvider client={queryClient}>
|
|
43
|
+
<Page />
|
|
44
|
+
</QueryClientProvider>));
|
|
45
|
+
await waitFor(() => screen.getByText('data1: 1, data2: 2'));
|
|
46
|
+
expect(results.length).toBe(3);
|
|
47
|
+
expect(results[0]).toMatchObject([{ data: undefined }, { data: undefined }]);
|
|
48
|
+
expect(results[1]).toMatchObject([{ data: 1 }, { data: undefined }]);
|
|
49
|
+
expect(results[2]).toMatchObject([{ data: 1 }, { data: 2 }]);
|
|
50
|
+
});
|
|
51
|
+
it('should keep previous data if amount of queries is the same', async () => {
|
|
52
|
+
const key1 = queryKey();
|
|
53
|
+
const key2 = queryKey();
|
|
54
|
+
const states = [];
|
|
55
|
+
function Page() {
|
|
56
|
+
const [count, setCount] = createSignal(1);
|
|
57
|
+
const result = createQueries({
|
|
58
|
+
queries: [
|
|
59
|
+
{
|
|
60
|
+
queryKey: () => [key1(), count()],
|
|
61
|
+
keepPreviousData: true,
|
|
62
|
+
queryFn: async () => {
|
|
63
|
+
await sleep(10);
|
|
64
|
+
return count() * 2;
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
queryKey: () => [key2(), count()],
|
|
69
|
+
keepPreviousData: true,
|
|
70
|
+
queryFn: async () => {
|
|
71
|
+
await sleep(35);
|
|
72
|
+
return count() * 5;
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
createRenderEffect(() => {
|
|
78
|
+
states.push([...result]);
|
|
79
|
+
});
|
|
80
|
+
const isFetching = createMemo(() => result.some((r) => r.isFetching));
|
|
81
|
+
return (<div>
|
|
82
|
+
<div>
|
|
83
|
+
data1: {String(result[0].data ?? 'null')}, data2:{' '}
|
|
84
|
+
{String(result[1].data ?? 'null')}
|
|
85
|
+
</div>
|
|
86
|
+
<div>isFetching: {String(isFetching())}</div>
|
|
87
|
+
<button onClick={() => setCount((prev) => prev + 1)}>inc</button>
|
|
88
|
+
</div>);
|
|
89
|
+
}
|
|
90
|
+
render(() => (<QueryClientProvider client={queryClient}>
|
|
91
|
+
<Page />
|
|
92
|
+
</QueryClientProvider>));
|
|
93
|
+
await waitFor(() => screen.getByText('data1: 2, data2: 5'));
|
|
94
|
+
fireEvent.click(screen.getByRole('button', { name: /inc/i }));
|
|
95
|
+
await waitFor(() => screen.getByText('data1: 4, data2: 10'));
|
|
96
|
+
await waitFor(() => screen.getByText('isFetching: false'));
|
|
97
|
+
expect(states[states.length - 1]).toMatchObject([
|
|
98
|
+
{ status: 'success', data: 4, isPreviousData: false, isFetching: false },
|
|
99
|
+
{ status: 'success', data: 10, isPreviousData: false, isFetching: false },
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
it('should keep previous data for variable amounts of useQueries', async () => {
|
|
103
|
+
const key = queryKey();
|
|
104
|
+
const states = [];
|
|
105
|
+
function Page() {
|
|
106
|
+
const [count, setCount] = createSignal(2);
|
|
107
|
+
const result = createQueries({
|
|
108
|
+
// TODO(lukemurray): reactive queries doesn't appear to work
|
|
109
|
+
get queries() {
|
|
110
|
+
return Array.from({ length: count() }, (_, i) => ({
|
|
111
|
+
queryKey: () => [key(), count(), i + 1],
|
|
112
|
+
keepPreviousData: true,
|
|
113
|
+
queryFn: async () => {
|
|
114
|
+
await sleep(35 * (i + 1));
|
|
115
|
+
return (i + 1) * count() * 2;
|
|
116
|
+
},
|
|
117
|
+
}));
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
createRenderEffect(() => {
|
|
121
|
+
states.push([...result]);
|
|
122
|
+
});
|
|
123
|
+
const isFetching = createMemo(() => result.some((r) => r.isFetching));
|
|
124
|
+
return (<div>
|
|
125
|
+
<div>data: {result.map((it) => it.data).join(',')}</div>
|
|
126
|
+
<div>isFetching: {String(isFetching())}</div>
|
|
127
|
+
<button onClick={() => setCount((prev) => prev + 1)}>inc</button>
|
|
128
|
+
</div>);
|
|
129
|
+
}
|
|
130
|
+
render(() => (<QueryClientProvider client={queryClient}>
|
|
131
|
+
<Page />
|
|
132
|
+
</QueryClientProvider>));
|
|
133
|
+
await waitFor(() => screen.getByText('data: 4,8'));
|
|
134
|
+
fireEvent.click(screen.getByRole('button', { name: /inc/i }));
|
|
135
|
+
await waitFor(() => screen.getByText('data: 6,12,18'));
|
|
136
|
+
await waitFor(() => screen.getByText('isFetching: false'));
|
|
137
|
+
expect(states[states.length - 1]).toMatchObject([
|
|
138
|
+
{ status: 'success', data: 6, isPreviousData: false, isFetching: false },
|
|
139
|
+
{ status: 'success', data: 12, isPreviousData: false, isFetching: false },
|
|
140
|
+
{ status: 'success', data: 18, isPreviousData: false, isFetching: false },
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
it('should keep previous data when switching between queries', async () => {
|
|
144
|
+
const key = queryKey();
|
|
145
|
+
const states = [];
|
|
146
|
+
function Page() {
|
|
147
|
+
const [series1, setSeries1] = createSignal(1);
|
|
148
|
+
const [series2, setSeries2] = createSignal(2);
|
|
149
|
+
const ids = [series1, series2];
|
|
150
|
+
const result = createQueries({
|
|
151
|
+
queries: ids.map((id) => {
|
|
152
|
+
return {
|
|
153
|
+
queryKey: () => [key(), id()],
|
|
154
|
+
queryFn: async () => {
|
|
155
|
+
await sleep(5);
|
|
156
|
+
return id() * 5;
|
|
157
|
+
},
|
|
158
|
+
keepPreviousData: true,
|
|
159
|
+
};
|
|
160
|
+
}),
|
|
161
|
+
});
|
|
162
|
+
createRenderEffect(() => {
|
|
163
|
+
states.push([...result]);
|
|
164
|
+
});
|
|
165
|
+
const isFetching = createMemo(() => result.some((r) => r.isFetching));
|
|
166
|
+
return (<div>
|
|
167
|
+
<div>
|
|
168
|
+
data1: {String(result[0]?.data ?? 'null')}, data2:{' '}
|
|
169
|
+
{String(result[1]?.data ?? 'null')}
|
|
170
|
+
</div>
|
|
171
|
+
<div>isFetching: {String(isFetching())}</div>
|
|
172
|
+
<button onClick={() => setSeries2(3)}>setSeries2</button>
|
|
173
|
+
<button onClick={() => setSeries1(2)}>setSeries1</button>
|
|
174
|
+
</div>);
|
|
175
|
+
}
|
|
176
|
+
render(() => (<QueryClientProvider client={queryClient}>
|
|
177
|
+
<Page />
|
|
178
|
+
</QueryClientProvider>));
|
|
179
|
+
await waitFor(() => screen.getByText('data1: 5, data2: 10'));
|
|
180
|
+
fireEvent.click(screen.getByRole('button', { name: /setSeries2/i }));
|
|
181
|
+
await waitFor(() => screen.getByText('data1: 5, data2: 15'));
|
|
182
|
+
fireEvent.click(screen.getByRole('button', { name: /setSeries1/i }));
|
|
183
|
+
await waitFor(() => screen.getByText('data1: 10, data2: 15'));
|
|
184
|
+
await waitFor(() => screen.getByText('isFetching: false'));
|
|
185
|
+
expect(states[states.length - 1]).toMatchObject([
|
|
186
|
+
{ status: 'success', data: 10, isPreviousData: false, isFetching: false },
|
|
187
|
+
{ status: 'success', data: 15, isPreviousData: false, isFetching: false },
|
|
188
|
+
]);
|
|
189
|
+
});
|
|
190
|
+
it('should not go to infinite render loop with previous data when toggling queries', async () => {
|
|
191
|
+
const key = queryKey();
|
|
192
|
+
const states = [];
|
|
193
|
+
function Page() {
|
|
194
|
+
const [enableId1, setEnableId1] = createSignal(true);
|
|
195
|
+
const ids = createMemo(() => (enableId1() ? [1, 2] : [2]));
|
|
196
|
+
const result = createQueries({
|
|
197
|
+
// TODO(lukemurray): same issue queries should be reactive
|
|
198
|
+
get queries() {
|
|
199
|
+
return ids().map((id) => {
|
|
200
|
+
return {
|
|
201
|
+
queryKey: () => [key(), id],
|
|
202
|
+
queryFn: async () => {
|
|
203
|
+
await sleep(5);
|
|
204
|
+
return id * 5;
|
|
205
|
+
},
|
|
206
|
+
keepPreviousData: true,
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
createRenderEffect(() => {
|
|
212
|
+
states.push([...result]);
|
|
213
|
+
});
|
|
214
|
+
const text = createMemo(() => {
|
|
215
|
+
return result
|
|
216
|
+
.map((r, idx) => `data${idx + 1}: ${r.data ?? 'null'}`)
|
|
217
|
+
.join(' ');
|
|
218
|
+
});
|
|
219
|
+
const isFetching = createMemo(() => result.some((r) => r.isFetching));
|
|
220
|
+
return (<div>
|
|
221
|
+
<div>{text()}</div>
|
|
222
|
+
<div>isFetching: {String(isFetching())}</div>
|
|
223
|
+
<button onClick={() => setEnableId1(false)}>set1Disabled</button>
|
|
224
|
+
<button onClick={() => setEnableId1(true)}>set2Enabled</button>
|
|
225
|
+
</div>);
|
|
226
|
+
}
|
|
227
|
+
render(() => (<QueryClientProvider client={queryClient}>
|
|
228
|
+
<Page />
|
|
229
|
+
</QueryClientProvider>));
|
|
230
|
+
await waitFor(() => screen.getByText('data1: 5 data2: 10'));
|
|
231
|
+
fireEvent.click(screen.getByRole('button', { name: /set1Disabled/i }));
|
|
232
|
+
await waitFor(() => screen.getByText('data1: 10'));
|
|
233
|
+
await waitFor(() => screen.getByText('isFetching: false'));
|
|
234
|
+
fireEvent.click(screen.getByRole('button', { name: /set2Enabled/i }));
|
|
235
|
+
await waitFor(() => screen.getByText('data1: 5 data2: 10'));
|
|
236
|
+
await waitFor(() => screen.getByText('isFetching: false'));
|
|
237
|
+
await waitFor(() => expect(states.length).toBe(5));
|
|
238
|
+
expect(states[0]).toMatchObject([
|
|
239
|
+
{
|
|
240
|
+
status: 'loading',
|
|
241
|
+
data: undefined,
|
|
242
|
+
isPreviousData: false,
|
|
243
|
+
isFetching: true,
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
status: 'loading',
|
|
247
|
+
data: undefined,
|
|
248
|
+
isPreviousData: false,
|
|
249
|
+
isFetching: true,
|
|
250
|
+
},
|
|
251
|
+
]);
|
|
252
|
+
expect(states[1]).toMatchObject([
|
|
253
|
+
{ status: 'success', data: 5, isPreviousData: false, isFetching: false },
|
|
254
|
+
{ status: 'success', data: 10, isPreviousData: false, isFetching: false },
|
|
255
|
+
]);
|
|
256
|
+
expect(states[2]).toMatchObject([
|
|
257
|
+
{ status: 'success', data: 10, isPreviousData: false, isFetching: false },
|
|
258
|
+
]);
|
|
259
|
+
expect(states[3]).toMatchObject([
|
|
260
|
+
{ status: 'success', data: 5, isPreviousData: false, isFetching: true },
|
|
261
|
+
{ status: 'success', data: 10, isPreviousData: false, isFetching: false },
|
|
262
|
+
]);
|
|
263
|
+
expect(states[4]).toMatchObject([
|
|
264
|
+
{ status: 'success', data: 5, isPreviousData: false, isFetching: false },
|
|
265
|
+
{ status: 'success', data: 10, isPreviousData: false, isFetching: false },
|
|
266
|
+
]);
|
|
267
|
+
});
|
|
268
|
+
it('handles type parameter - tuple of tuples', async () => {
|
|
269
|
+
const key1 = queryKey();
|
|
270
|
+
const key2 = queryKey();
|
|
271
|
+
const key3 = queryKey();
|
|
272
|
+
// @ts-expect-error (Page component is not rendered)
|
|
273
|
+
// eslint-disable-next-line
|
|
274
|
+
function Page() {
|
|
275
|
+
const result1 = createQueries({
|
|
276
|
+
queries: [
|
|
277
|
+
{
|
|
278
|
+
queryKey: key1,
|
|
279
|
+
queryFn: () => 1,
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
queryKey: key2,
|
|
283
|
+
queryFn: () => 'string',
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
queryKey: key3,
|
|
287
|
+
queryFn: () => ['string[]'],
|
|
288
|
+
},
|
|
289
|
+
],
|
|
290
|
+
});
|
|
291
|
+
expectType(result1[0]);
|
|
292
|
+
expectType(result1[1]);
|
|
293
|
+
expectType(result1[2]);
|
|
294
|
+
expectType(result1[0].data);
|
|
295
|
+
expectType(result1[1].data);
|
|
296
|
+
expectType(result1[2].data);
|
|
297
|
+
expectType(result1[2].error);
|
|
298
|
+
// TData (3rd element) takes precedence over TQueryFnData (1st element)
|
|
299
|
+
const result2 = createQueries({
|
|
300
|
+
queries: [
|
|
301
|
+
{
|
|
302
|
+
queryKey: key1,
|
|
303
|
+
queryFn: () => 'string',
|
|
304
|
+
select: (a) => {
|
|
305
|
+
expectType(a);
|
|
306
|
+
expectTypeNotAny(a);
|
|
307
|
+
return a.toLowerCase();
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
queryKey: key2,
|
|
312
|
+
queryFn: () => 'string',
|
|
313
|
+
select: (a) => {
|
|
314
|
+
expectType(a);
|
|
315
|
+
expectTypeNotAny(a);
|
|
316
|
+
return parseInt(a);
|
|
317
|
+
},
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
});
|
|
321
|
+
expectType(result2[0]);
|
|
322
|
+
expectType(result2[1]);
|
|
323
|
+
expectType(result2[0].data);
|
|
324
|
+
expectType(result2[1].data);
|
|
325
|
+
// types should be enforced
|
|
326
|
+
createQueries({
|
|
327
|
+
queries: [
|
|
328
|
+
{
|
|
329
|
+
queryKey: key1,
|
|
330
|
+
queryFn: () => 'string',
|
|
331
|
+
select: (a) => {
|
|
332
|
+
expectType(a);
|
|
333
|
+
expectTypeNotAny(a);
|
|
334
|
+
return a.toLowerCase();
|
|
335
|
+
},
|
|
336
|
+
onSuccess: (a) => {
|
|
337
|
+
expectType(a);
|
|
338
|
+
expectTypeNotAny(a);
|
|
339
|
+
},
|
|
340
|
+
placeholderData: 'string',
|
|
341
|
+
// @ts-expect-error (initialData: string)
|
|
342
|
+
initialData: 123,
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
queryKey: key2,
|
|
346
|
+
queryFn: () => 'string',
|
|
347
|
+
select: (a) => {
|
|
348
|
+
expectType(a);
|
|
349
|
+
expectTypeNotAny(a);
|
|
350
|
+
return parseInt(a);
|
|
351
|
+
},
|
|
352
|
+
onSuccess: (a) => {
|
|
353
|
+
expectType(a);
|
|
354
|
+
expectTypeNotAny(a);
|
|
355
|
+
},
|
|
356
|
+
onError: (e) => {
|
|
357
|
+
expectType(e);
|
|
358
|
+
expectTypeNotAny(e);
|
|
359
|
+
},
|
|
360
|
+
placeholderData: 'string',
|
|
361
|
+
// @ts-expect-error (initialData: string)
|
|
362
|
+
initialData: 123,
|
|
363
|
+
},
|
|
364
|
+
],
|
|
365
|
+
});
|
|
366
|
+
// field names should be enforced
|
|
367
|
+
createQueries({
|
|
368
|
+
queries: [
|
|
369
|
+
{
|
|
370
|
+
queryKey: key1,
|
|
371
|
+
queryFn: () => 'string',
|
|
372
|
+
// @ts-expect-error (invalidField)
|
|
373
|
+
someInvalidField: [],
|
|
374
|
+
},
|
|
375
|
+
],
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
it('handles type parameter - tuple of objects', async () => {
|
|
380
|
+
const key1 = queryKey();
|
|
381
|
+
const key2 = queryKey();
|
|
382
|
+
const key3 = queryKey();
|
|
383
|
+
// @ts-expect-error (Page component is not rendered)
|
|
384
|
+
// eslint-disable-next-line
|
|
385
|
+
function Page() {
|
|
386
|
+
const result1 = createQueries({
|
|
387
|
+
queries: [
|
|
388
|
+
{
|
|
389
|
+
queryKey: key1,
|
|
390
|
+
queryFn: () => 1,
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
queryKey: key2,
|
|
394
|
+
queryFn: () => 'string',
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
queryKey: key3,
|
|
398
|
+
queryFn: () => ['string[]'],
|
|
399
|
+
},
|
|
400
|
+
],
|
|
401
|
+
});
|
|
402
|
+
expectType(result1[0]);
|
|
403
|
+
expectType(result1[1]);
|
|
404
|
+
expectType(result1[2]);
|
|
405
|
+
expectType(result1[0].data);
|
|
406
|
+
expectType(result1[1].data);
|
|
407
|
+
expectType(result1[2].data);
|
|
408
|
+
expectType(result1[2].error);
|
|
409
|
+
// TData (data prop) takes precedence over TQueryFnData (queryFnData prop)
|
|
410
|
+
const result2 = createQueries({
|
|
411
|
+
queries: [
|
|
412
|
+
{
|
|
413
|
+
queryKey: key1,
|
|
414
|
+
queryFn: () => 'string',
|
|
415
|
+
select: (a) => {
|
|
416
|
+
expectType(a);
|
|
417
|
+
expectTypeNotAny(a);
|
|
418
|
+
return a.toLowerCase();
|
|
419
|
+
},
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
queryKey: key2,
|
|
423
|
+
queryFn: () => 'string',
|
|
424
|
+
select: (a) => {
|
|
425
|
+
expectType(a);
|
|
426
|
+
expectTypeNotAny(a);
|
|
427
|
+
return parseInt(a);
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
],
|
|
431
|
+
});
|
|
432
|
+
expectType(result2[0]);
|
|
433
|
+
expectType(result2[1]);
|
|
434
|
+
expectType(result2[0].data);
|
|
435
|
+
expectType(result2[1].data);
|
|
436
|
+
// can pass only TData (data prop) although TQueryFnData will be left unknown
|
|
437
|
+
const result3 = createQueries({
|
|
438
|
+
queries: [
|
|
439
|
+
{
|
|
440
|
+
queryKey: key1,
|
|
441
|
+
queryFn: () => 'string',
|
|
442
|
+
select: (a) => {
|
|
443
|
+
expectType(a);
|
|
444
|
+
expectTypeNotAny(a);
|
|
445
|
+
return a;
|
|
446
|
+
},
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
queryKey: key2,
|
|
450
|
+
queryFn: () => 'string',
|
|
451
|
+
select: (a) => {
|
|
452
|
+
expectType(a);
|
|
453
|
+
expectTypeNotAny(a);
|
|
454
|
+
return a;
|
|
455
|
+
},
|
|
456
|
+
},
|
|
457
|
+
],
|
|
458
|
+
});
|
|
459
|
+
expectType(result3[0]);
|
|
460
|
+
expectType(result3[1]);
|
|
461
|
+
expectType(result3[0].data);
|
|
462
|
+
expectType(result3[1].data);
|
|
463
|
+
// types should be enforced
|
|
464
|
+
createQueries({
|
|
465
|
+
queries: [
|
|
466
|
+
{
|
|
467
|
+
queryKey: key1,
|
|
468
|
+
queryFn: () => 'string',
|
|
469
|
+
select: (a) => {
|
|
470
|
+
expectType(a);
|
|
471
|
+
expectTypeNotAny(a);
|
|
472
|
+
return a.toLowerCase();
|
|
473
|
+
},
|
|
474
|
+
onSuccess: (a) => {
|
|
475
|
+
expectType(a);
|
|
476
|
+
expectTypeNotAny(a);
|
|
477
|
+
},
|
|
478
|
+
placeholderData: 'string',
|
|
479
|
+
// @ts-expect-error (initialData: string)
|
|
480
|
+
initialData: 123,
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
queryKey: key2,
|
|
484
|
+
queryFn: () => 'string',
|
|
485
|
+
select: (a) => {
|
|
486
|
+
expectType(a);
|
|
487
|
+
expectTypeNotAny(a);
|
|
488
|
+
return parseInt(a);
|
|
489
|
+
},
|
|
490
|
+
onSuccess: (a) => {
|
|
491
|
+
expectType(a);
|
|
492
|
+
expectTypeNotAny(a);
|
|
493
|
+
},
|
|
494
|
+
onError: (e) => {
|
|
495
|
+
expectType(e);
|
|
496
|
+
expectTypeNotAny(e);
|
|
497
|
+
},
|
|
498
|
+
placeholderData: 'string',
|
|
499
|
+
// @ts-expect-error (initialData: string)
|
|
500
|
+
initialData: 123,
|
|
501
|
+
},
|
|
502
|
+
],
|
|
503
|
+
});
|
|
504
|
+
// field names should be enforced
|
|
505
|
+
createQueries({
|
|
506
|
+
queries: [
|
|
507
|
+
{
|
|
508
|
+
queryKey: key1,
|
|
509
|
+
queryFn: () => 'string',
|
|
510
|
+
// @ts-expect-error (invalidField)
|
|
511
|
+
someInvalidField: [],
|
|
512
|
+
},
|
|
513
|
+
],
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
it('handles array literal without type parameter to infer result type', async () => {
|
|
518
|
+
const key1 = queryKey();
|
|
519
|
+
const key2 = queryKey();
|
|
520
|
+
const key3 = queryKey();
|
|
521
|
+
const key4 = queryKey();
|
|
522
|
+
// @ts-expect-error (Page component is not rendered)
|
|
523
|
+
// eslint-disable-next-line
|
|
524
|
+
function Page() {
|
|
525
|
+
// Array.map preserves TQueryFnData
|
|
526
|
+
const result1 = createQueries({
|
|
527
|
+
queries: Array(50).map((_, i) => ({
|
|
528
|
+
queryKey: () => ['key', i],
|
|
529
|
+
queryFn: () => i + 10,
|
|
530
|
+
})),
|
|
531
|
+
});
|
|
532
|
+
expectType(result1);
|
|
533
|
+
expectType(result1[0]?.data);
|
|
534
|
+
// Array.map preserves TData
|
|
535
|
+
const result2 = createQueries({
|
|
536
|
+
queries: Array(50).map((_, i) => ({
|
|
537
|
+
queryKey: () => ['key', i],
|
|
538
|
+
queryFn: () => i + 10,
|
|
539
|
+
select: (data) => data.toString(),
|
|
540
|
+
})),
|
|
541
|
+
});
|
|
542
|
+
expectType(result2);
|
|
543
|
+
const result3 = createQueries({
|
|
544
|
+
queries: [
|
|
545
|
+
{
|
|
546
|
+
queryKey: key1,
|
|
547
|
+
queryFn: () => 1,
|
|
548
|
+
},
|
|
549
|
+
{
|
|
550
|
+
queryKey: key2,
|
|
551
|
+
queryFn: () => 'string',
|
|
552
|
+
},
|
|
553
|
+
{
|
|
554
|
+
queryKey: key3,
|
|
555
|
+
queryFn: () => ['string[]'],
|
|
556
|
+
select: () => 123,
|
|
557
|
+
},
|
|
558
|
+
],
|
|
559
|
+
});
|
|
560
|
+
expectType(result3[0]);
|
|
561
|
+
expectType(result3[1]);
|
|
562
|
+
expectType(result3[2]);
|
|
563
|
+
expectType(result3[0].data);
|
|
564
|
+
expectType(result3[1].data);
|
|
565
|
+
// select takes precedence over queryFn
|
|
566
|
+
expectType(result3[2].data);
|
|
567
|
+
// initialData/placeholderData are enforced
|
|
568
|
+
createQueries({
|
|
569
|
+
queries: [
|
|
570
|
+
{
|
|
571
|
+
queryKey: key1,
|
|
572
|
+
queryFn: () => 'string',
|
|
573
|
+
placeholderData: 'string',
|
|
574
|
+
// @ts-expect-error (initialData: string)
|
|
575
|
+
initialData: 123,
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
queryKey: key2,
|
|
579
|
+
queryFn: () => 123,
|
|
580
|
+
// @ts-expect-error (placeholderData: number)
|
|
581
|
+
placeholderData: 'string',
|
|
582
|
+
initialData: 123,
|
|
583
|
+
},
|
|
584
|
+
],
|
|
585
|
+
});
|
|
586
|
+
// select / onSuccess / onSettled params are "indirectly" enforced
|
|
587
|
+
createQueries({
|
|
588
|
+
queries: [
|
|
589
|
+
// unfortunately TS will not suggest the type for you
|
|
590
|
+
{
|
|
591
|
+
queryKey: key1,
|
|
592
|
+
queryFn: () => 'string',
|
|
593
|
+
// @ts-expect-error (noImplicitAny)
|
|
594
|
+
onSuccess: (a) => null,
|
|
595
|
+
// @ts-expect-error (noImplicitAny)
|
|
596
|
+
onSettled: (a) => null,
|
|
597
|
+
},
|
|
598
|
+
// however you can add a type to the callback
|
|
599
|
+
{
|
|
600
|
+
queryKey: key2,
|
|
601
|
+
queryFn: () => 'string',
|
|
602
|
+
onSuccess: (a) => {
|
|
603
|
+
expectType(a);
|
|
604
|
+
expectTypeNotAny(a);
|
|
605
|
+
},
|
|
606
|
+
onSettled: (a) => {
|
|
607
|
+
expectType(a);
|
|
608
|
+
expectTypeNotAny(a);
|
|
609
|
+
},
|
|
610
|
+
},
|
|
611
|
+
// the type you do pass is enforced
|
|
612
|
+
{
|
|
613
|
+
queryKey: key3,
|
|
614
|
+
queryFn: () => 'string',
|
|
615
|
+
// @ts-expect-error (only accepts string)
|
|
616
|
+
onSuccess: (a) => null,
|
|
617
|
+
},
|
|
618
|
+
{
|
|
619
|
+
queryKey: key4,
|
|
620
|
+
queryFn: () => 'string',
|
|
621
|
+
select: (a) => parseInt(a),
|
|
622
|
+
// @ts-expect-error (select is defined => only accepts number)
|
|
623
|
+
onSuccess: (a) => null,
|
|
624
|
+
onSettled: (a) => {
|
|
625
|
+
expectType(a);
|
|
626
|
+
expectTypeNotAny(a);
|
|
627
|
+
},
|
|
628
|
+
},
|
|
629
|
+
],
|
|
630
|
+
});
|
|
631
|
+
// callbacks are also indirectly enforced with Array.map
|
|
632
|
+
createQueries({
|
|
633
|
+
// @ts-expect-error (onSuccess only accepts string)
|
|
634
|
+
queries: Array(50).map((_, i) => ({
|
|
635
|
+
queryKey: ['key', i],
|
|
636
|
+
queryFn: () => i + 10,
|
|
637
|
+
select: (data) => data.toString(),
|
|
638
|
+
onSuccess: (_data) => null,
|
|
639
|
+
})),
|
|
640
|
+
});
|
|
641
|
+
createQueries({
|
|
642
|
+
queries: Array(50).map((_, i) => ({
|
|
643
|
+
queryKey: () => ['key', i],
|
|
644
|
+
queryFn: () => i + 10,
|
|
645
|
+
select: (data) => data.toString(),
|
|
646
|
+
onSuccess: (_data) => null,
|
|
647
|
+
})),
|
|
648
|
+
});
|
|
649
|
+
// results inference works when all the handlers are defined
|
|
650
|
+
const result4 = createQueries({
|
|
651
|
+
queries: [
|
|
652
|
+
{
|
|
653
|
+
queryKey: key1,
|
|
654
|
+
queryFn: () => 'string',
|
|
655
|
+
// @ts-expect-error (noImplicitAny)
|
|
656
|
+
onSuccess: (a) => null,
|
|
657
|
+
// @ts-expect-error (noImplicitAny)
|
|
658
|
+
onSettled: (a) => null,
|
|
659
|
+
},
|
|
660
|
+
{
|
|
661
|
+
queryKey: key2,
|
|
662
|
+
queryFn: () => 'string',
|
|
663
|
+
onSuccess: (a) => {
|
|
664
|
+
expectType(a);
|
|
665
|
+
expectTypeNotAny(a);
|
|
666
|
+
},
|
|
667
|
+
onSettled: (a) => {
|
|
668
|
+
expectType(a);
|
|
669
|
+
expectTypeNotAny(a);
|
|
670
|
+
},
|
|
671
|
+
},
|
|
672
|
+
{
|
|
673
|
+
queryKey: key4,
|
|
674
|
+
queryFn: () => 'string',
|
|
675
|
+
select: (a) => parseInt(a),
|
|
676
|
+
onSuccess: (_a) => null,
|
|
677
|
+
onSettled: (a) => {
|
|
678
|
+
expectType(a);
|
|
679
|
+
expectTypeNotAny(a);
|
|
680
|
+
},
|
|
681
|
+
},
|
|
682
|
+
],
|
|
683
|
+
});
|
|
684
|
+
expectType(result4[0]);
|
|
685
|
+
expectType(result4[1]);
|
|
686
|
+
expectType(result4[2]);
|
|
687
|
+
// handles when queryFn returns a Promise
|
|
688
|
+
const result5 = createQueries({
|
|
689
|
+
queries: [
|
|
690
|
+
{
|
|
691
|
+
queryKey: key1,
|
|
692
|
+
queryFn: () => Promise.resolve('string'),
|
|
693
|
+
onSuccess: (a) => {
|
|
694
|
+
expectType(a);
|
|
695
|
+
expectTypeNotAny(a);
|
|
696
|
+
},
|
|
697
|
+
// @ts-expect-error (refuses to accept a Promise)
|
|
698
|
+
onSettled: (a) => null,
|
|
699
|
+
},
|
|
700
|
+
],
|
|
701
|
+
});
|
|
702
|
+
expectType(result5[0]);
|
|
703
|
+
// Array as const does not throw error
|
|
704
|
+
const result6 = createQueries({
|
|
705
|
+
queries: [
|
|
706
|
+
{
|
|
707
|
+
queryKey: () => ['key1'],
|
|
708
|
+
queryFn: () => 'string',
|
|
709
|
+
},
|
|
710
|
+
{
|
|
711
|
+
queryKey: () => ['key1'],
|
|
712
|
+
queryFn: () => 123,
|
|
713
|
+
},
|
|
714
|
+
],
|
|
715
|
+
});
|
|
716
|
+
expectType(result6[0]);
|
|
717
|
+
expectType(result6[1]);
|
|
718
|
+
// field names should be enforced - array literal
|
|
719
|
+
createQueries({
|
|
720
|
+
queries: [
|
|
721
|
+
{
|
|
722
|
+
queryKey: key1,
|
|
723
|
+
queryFn: () => 'string',
|
|
724
|
+
// @ts-expect-error (invalidField)
|
|
725
|
+
someInvalidField: [],
|
|
726
|
+
},
|
|
727
|
+
],
|
|
728
|
+
});
|
|
729
|
+
// field names should be enforced - Array.map() result
|
|
730
|
+
createQueries({
|
|
731
|
+
// @ts-expect-error (invalidField)
|
|
732
|
+
queries: Array(10).map(() => ({
|
|
733
|
+
someInvalidField: '',
|
|
734
|
+
})),
|
|
735
|
+
});
|
|
736
|
+
// field names should be enforced - array literal
|
|
737
|
+
createQueries({
|
|
738
|
+
queries: [
|
|
739
|
+
{
|
|
740
|
+
queryKey: key1,
|
|
741
|
+
queryFn: () => 'string',
|
|
742
|
+
// @ts-expect-error (invalidField)
|
|
743
|
+
someInvalidField: [],
|
|
744
|
+
},
|
|
745
|
+
],
|
|
746
|
+
});
|
|
747
|
+
// supports queryFn using fetch() to return Promise<any> - Array.map() result
|
|
748
|
+
createQueries({
|
|
749
|
+
queries: Array(50).map((_, i) => ({
|
|
750
|
+
queryKey: () => ['key', i],
|
|
751
|
+
queryFn: () => fetch('return Promise<any>').then((resp) => resp.json()),
|
|
752
|
+
})),
|
|
753
|
+
});
|
|
754
|
+
// supports queryFn using fetch() to return Promise<any> - array literal
|
|
755
|
+
createQueries({
|
|
756
|
+
queries: [
|
|
757
|
+
{
|
|
758
|
+
queryKey: key1,
|
|
759
|
+
queryFn: () => fetch('return Promise<any>').then((resp) => resp.json()),
|
|
760
|
+
},
|
|
761
|
+
],
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
it('handles strongly typed queryFn factories and useQueries wrappers', () => {
|
|
766
|
+
const getQueryKeyA = () => ['queryA'];
|
|
767
|
+
const getQueryFunctionA = () => async () => {
|
|
768
|
+
return 1;
|
|
769
|
+
};
|
|
770
|
+
const getSelectorA = () => (data) => [data, data.toString()];
|
|
771
|
+
const getQueryKeyB = (id) => ['queryB', id];
|
|
772
|
+
const getQueryFunctionB = () => async () => {
|
|
773
|
+
return '1';
|
|
774
|
+
};
|
|
775
|
+
const getSelectorB = () => (data) => [data, +data];
|
|
776
|
+
// Wrapper with strongly typed array-parameter
|
|
777
|
+
function useWrappedQueries(queries) {
|
|
778
|
+
return createQueries({
|
|
779
|
+
queries: queries.map(
|
|
780
|
+
// no need to type the mapped query
|
|
781
|
+
(query) => {
|
|
782
|
+
const { queryFn: fn, queryKey: key, onError: err } = query;
|
|
783
|
+
expectType(fn);
|
|
784
|
+
return {
|
|
785
|
+
queryKey: key,
|
|
786
|
+
onError: err,
|
|
787
|
+
queryFn: fn
|
|
788
|
+
? (ctx) => {
|
|
789
|
+
expectType(ctx.queryKey);
|
|
790
|
+
return fn.call({}, ctx);
|
|
791
|
+
}
|
|
792
|
+
: undefined,
|
|
793
|
+
};
|
|
794
|
+
}),
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
// @ts-expect-error (Page component is not rendered)
|
|
798
|
+
// eslint-disable-next-line
|
|
799
|
+
function Page() {
|
|
800
|
+
const result = createQueries({
|
|
801
|
+
queries: [
|
|
802
|
+
{
|
|
803
|
+
queryKey: () => getQueryKeyA(),
|
|
804
|
+
queryFn: getQueryFunctionA(),
|
|
805
|
+
},
|
|
806
|
+
{
|
|
807
|
+
queryKey: () => getQueryKeyB('id'),
|
|
808
|
+
queryFn: getQueryFunctionB(),
|
|
809
|
+
},
|
|
810
|
+
],
|
|
811
|
+
});
|
|
812
|
+
expectType(result[0]);
|
|
813
|
+
expectType(result[1]);
|
|
814
|
+
const withSelector = createQueries({
|
|
815
|
+
queries: [
|
|
816
|
+
{
|
|
817
|
+
queryKey: () => getQueryKeyA(),
|
|
818
|
+
queryFn: getQueryFunctionA(),
|
|
819
|
+
select: getSelectorA(),
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
queryKey: () => getQueryKeyB('id'),
|
|
823
|
+
queryFn: getQueryFunctionB(),
|
|
824
|
+
select: getSelectorB(),
|
|
825
|
+
},
|
|
826
|
+
],
|
|
827
|
+
});
|
|
828
|
+
expectType(withSelector[0]);
|
|
829
|
+
expectType(withSelector[1]);
|
|
830
|
+
const withWrappedQueries = useWrappedQueries(Array(10).map(() => ({
|
|
831
|
+
queryKey: () => getQueryKeyA(),
|
|
832
|
+
queryFn: getQueryFunctionA(),
|
|
833
|
+
select: getSelectorA(),
|
|
834
|
+
})));
|
|
835
|
+
expectType(withWrappedQueries);
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
it('should not change state if unmounted', async () => {
|
|
839
|
+
const key1 = queryKey();
|
|
840
|
+
// We have to mock the QueriesObserver to not unsubscribe
|
|
841
|
+
// the listener when the component is unmounted
|
|
842
|
+
class QueriesObserverMock extends QueriesObserver {
|
|
843
|
+
subscribe(listener) {
|
|
844
|
+
super.subscribe(listener);
|
|
845
|
+
return () => void 0;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
const QueriesObserverSpy = jest
|
|
849
|
+
.spyOn(QueriesObserverModule, 'QueriesObserver')
|
|
850
|
+
.mockImplementation((fn) => {
|
|
851
|
+
return new QueriesObserverMock(fn);
|
|
852
|
+
});
|
|
853
|
+
function Queries() {
|
|
854
|
+
createQueries({
|
|
855
|
+
queries: [
|
|
856
|
+
{
|
|
857
|
+
queryKey: key1,
|
|
858
|
+
queryFn: async () => {
|
|
859
|
+
await sleep(10);
|
|
860
|
+
return 1;
|
|
861
|
+
},
|
|
862
|
+
},
|
|
863
|
+
],
|
|
864
|
+
});
|
|
865
|
+
return (<div>
|
|
866
|
+
<span>queries</span>
|
|
867
|
+
</div>);
|
|
868
|
+
}
|
|
869
|
+
function Page() {
|
|
870
|
+
const [mounted, setMounted] = createSignal(true);
|
|
871
|
+
return (<div>
|
|
872
|
+
<button onClick={() => setMounted(false)}>unmount</button>
|
|
873
|
+
{mounted() && <Queries />}
|
|
874
|
+
</div>);
|
|
875
|
+
}
|
|
876
|
+
render(() => (<QueryClientProvider client={queryClient}>
|
|
877
|
+
<Page />
|
|
878
|
+
</QueryClientProvider>));
|
|
879
|
+
fireEvent.click(screen.getByText('unmount'));
|
|
880
|
+
// Should not display the console error
|
|
881
|
+
// "Warning: Can't perform a React state update on an unmounted component"
|
|
882
|
+
await sleep(20);
|
|
883
|
+
QueriesObserverSpy.mockRestore();
|
|
884
|
+
});
|
|
885
|
+
describe('with custom context', () => {
|
|
886
|
+
it('should return the correct states', async () => {
|
|
887
|
+
const context = createContext(undefined);
|
|
888
|
+
const key1 = queryKey();
|
|
889
|
+
const key2 = queryKey();
|
|
890
|
+
const results = [];
|
|
891
|
+
function Page() {
|
|
892
|
+
const result = createQueries({
|
|
893
|
+
context,
|
|
894
|
+
queries: [
|
|
895
|
+
{
|
|
896
|
+
queryKey: key1,
|
|
897
|
+
queryFn: async () => {
|
|
898
|
+
await sleep(5);
|
|
899
|
+
return 1;
|
|
900
|
+
},
|
|
901
|
+
},
|
|
902
|
+
{
|
|
903
|
+
queryKey: key2,
|
|
904
|
+
queryFn: async () => {
|
|
905
|
+
await sleep(10);
|
|
906
|
+
return 2;
|
|
907
|
+
},
|
|
908
|
+
},
|
|
909
|
+
],
|
|
910
|
+
});
|
|
911
|
+
createRenderEffect(() => {
|
|
912
|
+
results.push([...result]);
|
|
913
|
+
});
|
|
914
|
+
return null;
|
|
915
|
+
}
|
|
916
|
+
render(() => (<QueryClientProvider client={queryClient} context={context}>
|
|
917
|
+
<Page />
|
|
918
|
+
</QueryClientProvider>));
|
|
919
|
+
await sleep(30);
|
|
920
|
+
expect(results[0]).toMatchObject([
|
|
921
|
+
{ data: undefined },
|
|
922
|
+
{ data: undefined },
|
|
923
|
+
]);
|
|
924
|
+
expect(results[results.length - 1]).toMatchObject([
|
|
925
|
+
{ data: 1 },
|
|
926
|
+
{ data: 2 },
|
|
927
|
+
]);
|
|
928
|
+
});
|
|
929
|
+
it('should throw if the context is necessary and is not passed to useQueries', async () => {
|
|
930
|
+
const context = createContext(undefined);
|
|
931
|
+
const key1 = queryKey();
|
|
932
|
+
const key2 = queryKey();
|
|
933
|
+
const results = [];
|
|
934
|
+
function Page() {
|
|
935
|
+
const result = createQueries({
|
|
936
|
+
queries: [
|
|
937
|
+
{
|
|
938
|
+
queryKey: key1,
|
|
939
|
+
queryFn: async () => 1,
|
|
940
|
+
},
|
|
941
|
+
{
|
|
942
|
+
queryKey: key2,
|
|
943
|
+
queryFn: async () => 2,
|
|
944
|
+
},
|
|
945
|
+
],
|
|
946
|
+
});
|
|
947
|
+
results.push(result);
|
|
948
|
+
return null;
|
|
949
|
+
}
|
|
950
|
+
render(() => (<QueryClientProvider client={queryClient} context={context}>
|
|
951
|
+
<ErrorBoundary fallback={() => <div>error boundary</div>}>
|
|
952
|
+
<Page />
|
|
953
|
+
</ErrorBoundary>
|
|
954
|
+
</QueryClientProvider>));
|
|
955
|
+
await waitFor(() => screen.getByText('error boundary'));
|
|
956
|
+
});
|
|
957
|
+
});
|
|
958
|
+
});
|