tsdf 7.1.0 → 8.0.0-beta.1
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/.eslintrc.cjs +79 -16
- package/.vscode/tasks.json +11 -1
- package/fetch-orquestrator-scenarios.excalidraw +10115 -0
- package/package.json +21 -13
- package/src/collectionFetchOrquestrator.ts +23 -0
- package/src/collectionStore.ts +608 -0
- package/src/documentStore.ts +272 -0
- package/src/fetchOrquestrator.ts +335 -0
- package/src/listQueryStore.ts +1190 -0
- package/src/main.ts +22 -1
- package/src/storeShared.ts +5 -0
- package/src/useEnsureIsLoaded.ts +50 -0
- package/src/utils/filterAndMap.ts +20 -0
- package/src/utils/getCacheId.ts +19 -0
- package/src/utils/hooks.ts +110 -0
- package/src/utils/isObject.ts +3 -0
- package/src/utils/math.ts +7 -0
- package/src/utils/serializableClone.ts +3 -0
- package/src/utils/useLatestValue.ts +16 -0
- package/test/__snapshots__/listQueryStore.test.ts.snap +146 -0
- package/test/__snapshots__/listQueryStoreHooks.test.ts.snap +9 -0
- package/test/collectionStore.test.ts +523 -0
- package/test/collectionStoreHooks.test.tsx +651 -0
- package/test/documentStore.test.ts +183 -0
- package/test/documentStoreHooks.test.tsx +857 -0
- package/test/fetchOrquestrator.test.ts +913 -0
- package/test/listQueryStore.test.ts +1158 -0
- package/test/listQueryStoreHooks.test.tsx +662 -0
- package/test/listQueryStoreHooks2.test.tsx +483 -0
- package/test/listQueryStoreHooksMutations.test.tsx +764 -0
- package/test/mocks/fetchMock.ts +296 -0
- package/test/mocks/fetchOrquestratorEnv.ts +370 -0
- package/test/setup/setup.ts +95 -1
- package/test/testHelpers.test.ts +88 -0
- package/test/utils/RenderObjValues.tsx +23 -0
- package/test/utils/arrayUtils.ts +13 -0
- package/test/utils/createDefaultListQueryStore.ts +275 -0
- package/test/utils/createOrVariations.ts +48 -0
- package/test/utils/dedent.ts +50 -0
- package/test/utils/delayCall.ts +25 -0
- package/test/utils/math.ts +3 -0
- package/test/utils/objectUtils.ts +18 -0
- package/test/utils/range.ts +19 -0
- package/test/utils/sleep.ts +3 -0
- package/test/utils/storeUtils.ts +460 -0
- package/tsconfig.prod.json +1 -1
- package/tsup.config.ts +1 -1
- package/vitest.config.ts +2 -1
- package/test/test.test.ts +0 -5
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
import { cleanup, render, renderHook } from '@testing-library/react';
|
|
2
|
+
import { afterAll, describe, expect, test } from 'vitest';
|
|
3
|
+
import {
|
|
4
|
+
createDefaultListQueryStore,
|
|
5
|
+
Tables,
|
|
6
|
+
} from './utils/createDefaultListQueryStore';
|
|
7
|
+
import { pick } from './utils/objectUtils';
|
|
8
|
+
import { range } from './utils/range';
|
|
9
|
+
import { sleep } from './utils/sleep';
|
|
10
|
+
import {
|
|
11
|
+
createRenderStore,
|
|
12
|
+
createValueStore,
|
|
13
|
+
shouldNotSkip,
|
|
14
|
+
} from './utils/storeUtils';
|
|
15
|
+
|
|
16
|
+
const initialServerData: Tables = {
|
|
17
|
+
users: range(1, 5).map((id) => ({ id, name: `User ${id}` })),
|
|
18
|
+
products: range(1, 50).map((id) => ({ id, name: `Product ${id}` })),
|
|
19
|
+
orders: range(1, 50).map((id) => ({ id, name: `Order ${id}` })),
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
afterAll(() => {
|
|
23
|
+
cleanup();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
type FetchQueryParams = { tableId: string };
|
|
27
|
+
|
|
28
|
+
function getFetchQueryForTable(tableId: string): FetchQueryParams {
|
|
29
|
+
return { tableId };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('useMultipleItemsQuery sequential tests', () => {
|
|
33
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
34
|
+
initialServerData,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const usersRender = createRenderStore();
|
|
38
|
+
const productsRender = createRenderStore();
|
|
39
|
+
|
|
40
|
+
const { result } = renderHook(() => {
|
|
41
|
+
const queryResult = listQueryStore.useMultipleListQueries(
|
|
42
|
+
[getFetchQueryForTable('users'), getFetchQueryForTable('products')],
|
|
43
|
+
{ returnRefetchingStatus: true },
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const [users, products] = queryResult;
|
|
47
|
+
|
|
48
|
+
usersRender.add(pick(users, ['status', 'payload', 'items']));
|
|
49
|
+
productsRender.add(pick(products, ['status', 'payload', 'items']));
|
|
50
|
+
|
|
51
|
+
return { users, products };
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('load the queries', async () => {
|
|
55
|
+
await serverMock.waitFetchIdle();
|
|
56
|
+
|
|
57
|
+
expect(serverMock.fetchsCount).toBe(2);
|
|
58
|
+
|
|
59
|
+
expect(usersRender.getSnapshot()).toMatchInlineSnapshot(`
|
|
60
|
+
"
|
|
61
|
+
status: loading -- payload: {tableId:users} -- items: []
|
|
62
|
+
status: success -- payload: {tableId:users} -- items: [{id:users||1, data:{id:1, name:User 1}}, ...(4 more)]
|
|
63
|
+
"
|
|
64
|
+
`);
|
|
65
|
+
|
|
66
|
+
expect(productsRender.getSnapshot()).toMatchInlineSnapshot(`
|
|
67
|
+
"
|
|
68
|
+
status: loading -- payload: {tableId:products} -- items: []
|
|
69
|
+
status: success -- payload: {tableId:products} -- items: [{id:products||1, data:{id:1, name:Product 1}}, ...(49 more)]
|
|
70
|
+
"
|
|
71
|
+
`);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('invalidate one query', async () => {
|
|
75
|
+
usersRender.reset();
|
|
76
|
+
productsRender.reset();
|
|
77
|
+
|
|
78
|
+
serverMock.produceData((draft) => {
|
|
79
|
+
draft.users![0]!.name = 'Updated User 1';
|
|
80
|
+
});
|
|
81
|
+
listQueryStore.invalidateQuery(getFetchQueryForTable('users'));
|
|
82
|
+
|
|
83
|
+
await serverMock.waitFetchIdle();
|
|
84
|
+
|
|
85
|
+
expect(usersRender.renderCount()).toBeGreaterThan(0);
|
|
86
|
+
|
|
87
|
+
expect(usersRender.getSnapshot()).toMatchInlineSnapshot(`
|
|
88
|
+
"
|
|
89
|
+
status: refetching -- payload: {tableId:users} -- items: [{id:users||1, data:{id:1, name:User 1}}, ...(4 more)]
|
|
90
|
+
status: success -- payload: {tableId:users} -- items: [{id:users||1, data:{id:1, name:Updated User 1}}, ...(4 more)]
|
|
91
|
+
"
|
|
92
|
+
`);
|
|
93
|
+
expect(result.current.users?.items[0]!.data.name).toBe('Updated User 1');
|
|
94
|
+
expect(productsRender.getSnapshot()).toMatchInlineSnapshot(`
|
|
95
|
+
"
|
|
96
|
+
status: success -- payload: {tableId:products} -- items: [{id:products||1, data:{id:1, name:Product 1}}, ...(49 more)]
|
|
97
|
+
"
|
|
98
|
+
`);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('invalidate all queries', () => {
|
|
102
|
+
const extraComponentMounted = createRenderStore();
|
|
103
|
+
|
|
104
|
+
let getFetchCount: () => number;
|
|
105
|
+
|
|
106
|
+
// eslint-disable-next-line vitest/expect-expect
|
|
107
|
+
test('setup block', async () => {
|
|
108
|
+
getFetchCount = serverMock.numOfFetchsFromHere();
|
|
109
|
+
|
|
110
|
+
// mount a new hook to check if there are more fetchs than expected
|
|
111
|
+
const { unmount } = renderHook(() => {
|
|
112
|
+
const selectionResult = listQueryStore.useMultipleListQueries(
|
|
113
|
+
[getFetchQueryForTable('users'), getFetchQueryForTable('products')],
|
|
114
|
+
{
|
|
115
|
+
itemSelector(data) {
|
|
116
|
+
return data.name;
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
extraComponentMounted.add({
|
|
122
|
+
status: selectionResult[0]?.status,
|
|
123
|
+
payload: selectionResult[0]?.payload,
|
|
124
|
+
items: selectionResult[0]?.items,
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
productsRender.reset();
|
|
129
|
+
usersRender.reset();
|
|
130
|
+
|
|
131
|
+
serverMock.setFetchDuration((param) => {
|
|
132
|
+
return param === '1' ? 20 : 40;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
serverMock.produceData((draft) => {
|
|
136
|
+
draft.users![0]!.name = 'Updated User 1 again';
|
|
137
|
+
draft.products![0]!.name = 'Updated Product 1';
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
listQueryStore.invalidateQuery(() => true);
|
|
141
|
+
|
|
142
|
+
await serverMock.waitFetchIdle();
|
|
143
|
+
|
|
144
|
+
serverMock.undoTimeoutChange();
|
|
145
|
+
|
|
146
|
+
unmount();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('do not fetch more than expected with multiple components connected to the same items', () => {
|
|
150
|
+
expect(getFetchCount()).toBe(2);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('refetch data after invalidations', () => {
|
|
154
|
+
expect(usersRender.getSnapshot()).toMatchInlineSnapshot(`
|
|
155
|
+
"
|
|
156
|
+
status: refetching -- payload: {tableId:users} -- items: [{id:users||1, data:{id:1, name:Updated User 1}}, ...(4 more)]
|
|
157
|
+
status: success -- payload: {tableId:users} -- items: [{id:users||1, data:{id:1, name:Updated User 1 again}}, ...(4 more)]
|
|
158
|
+
"
|
|
159
|
+
`);
|
|
160
|
+
|
|
161
|
+
expect(productsRender.getSnapshot()).toMatchInlineSnapshot(`
|
|
162
|
+
"
|
|
163
|
+
status: refetching -- payload: {tableId:products} -- items: [{id:products||1, data:{id:1, name:Product 1}}, ...(49 more)]
|
|
164
|
+
status: success -- payload: {tableId:products} -- items: [{id:products||1, data:{id:1, name:Updated Product 1}}, ...(49 more)]
|
|
165
|
+
"
|
|
166
|
+
`);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('data selector', () => {
|
|
170
|
+
expect(extraComponentMounted.getSnapshot()).toMatchInlineSnapshot(`
|
|
171
|
+
"
|
|
172
|
+
status: success -- payload: {tableId:users} -- items: [Updated User 1, ...(4 more)]
|
|
173
|
+
status: success -- payload: {tableId:users} -- items: [Updated User 1 again, ...(4 more)]
|
|
174
|
+
"
|
|
175
|
+
`);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('useMultipleItemsQuery isolated tests', () => {
|
|
181
|
+
test('rerender when payload changes', async () => {
|
|
182
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
183
|
+
initialServerData,
|
|
184
|
+
useLoadedSnapshot: { tables: ['users', 'products'] },
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const payload = createValueStore([
|
|
188
|
+
getFetchQueryForTable('users'),
|
|
189
|
+
getFetchQueryForTable('products'),
|
|
190
|
+
]);
|
|
191
|
+
|
|
192
|
+
const usersRenders = createRenderStore();
|
|
193
|
+
const productsRenders = createRenderStore();
|
|
194
|
+
|
|
195
|
+
renderHook(() => {
|
|
196
|
+
const [users, products] = listQueryStore.useMultipleListQueries(
|
|
197
|
+
payload.useValue(),
|
|
198
|
+
{ itemSelector: (data) => data.name },
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
usersRenders.add(pick(users, ['status', 'payload', 'items']));
|
|
202
|
+
productsRenders.add(pick(products, ['status', 'payload', 'items']));
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
payload.set([
|
|
206
|
+
getFetchQueryForTable('users'),
|
|
207
|
+
getFetchQueryForTable('not-found'),
|
|
208
|
+
]);
|
|
209
|
+
|
|
210
|
+
await serverMock.waitFetchIdle();
|
|
211
|
+
|
|
212
|
+
expect(usersRenders.getSnapshot()).toMatchInlineSnapshot(`
|
|
213
|
+
"
|
|
214
|
+
status: success -- payload: {tableId:users} -- items: [User 1, ...(4 more)]
|
|
215
|
+
"
|
|
216
|
+
`);
|
|
217
|
+
expect(productsRenders.getSnapshot()).toMatchInlineSnapshot(`
|
|
218
|
+
"
|
|
219
|
+
status: success -- payload: {tableId:products} -- items: [Product 1, ...(49 more)]
|
|
220
|
+
status: loading -- payload: {tableId:not-found} -- items: []
|
|
221
|
+
status: error -- payload: {tableId:not-found} -- items: []
|
|
222
|
+
"
|
|
223
|
+
`);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('useQuery', () => {
|
|
228
|
+
test('disable then enable the initial fetch', async () => {
|
|
229
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
230
|
+
initialServerData,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const renders = createRenderStore();
|
|
234
|
+
|
|
235
|
+
type Props = {
|
|
236
|
+
payload: FetchQueryParams | false | undefined | null;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const { rerender } = renderHook(
|
|
240
|
+
({ payload }) => {
|
|
241
|
+
const queryResult = listQueryStore.useListQuery(payload);
|
|
242
|
+
|
|
243
|
+
renders.add(pick(queryResult, ['status', 'payload', 'items']));
|
|
244
|
+
|
|
245
|
+
return queryResult;
|
|
246
|
+
},
|
|
247
|
+
{ initialProps: { payload: false } as Props },
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
expect(renders.getSnapshot()).toMatchInlineSnapshot(`
|
|
251
|
+
"
|
|
252
|
+
status: idle -- payload: undefined -- items: []
|
|
253
|
+
"
|
|
254
|
+
`);
|
|
255
|
+
|
|
256
|
+
expect(serverMock.fetchsCount).toBe(0);
|
|
257
|
+
|
|
258
|
+
rerender({ payload: { tableId: 'users' } });
|
|
259
|
+
|
|
260
|
+
await serverMock.waitFetchIdle();
|
|
261
|
+
|
|
262
|
+
expect(serverMock.fetchsCount).toBe(1);
|
|
263
|
+
|
|
264
|
+
expect(renders.getSnapshot()).toMatchInlineSnapshot(`
|
|
265
|
+
"
|
|
266
|
+
status: idle -- payload: undefined -- items: []
|
|
267
|
+
---
|
|
268
|
+
status: loading -- payload: {tableId:users} -- items: []
|
|
269
|
+
status: success -- payload: {tableId:users} -- items: [{id:users||1, data:{id:1, name:User 1}}, ...(4 more)]
|
|
270
|
+
"
|
|
271
|
+
`);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test('use ensureIsLoaded prop', async () => {
|
|
275
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
276
|
+
initialServerData,
|
|
277
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const renders = createRenderStore();
|
|
281
|
+
|
|
282
|
+
renderHook(() => {
|
|
283
|
+
const selectionResult = listQueryStore.useListQuery(
|
|
284
|
+
{ tableId: 'users' },
|
|
285
|
+
{ ensureIsLoaded: true, itemSelector: (data) => data.name },
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
renders.add(pick(selectionResult, ['status', 'isLoading', 'items']));
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
await serverMock.waitFetchIdle();
|
|
292
|
+
|
|
293
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
294
|
+
"
|
|
295
|
+
status: loading -- isLoading: true -- items: [User 1, ...(4 more)]
|
|
296
|
+
status: success -- isLoading: false -- items: [User 1, ...(4 more)]
|
|
297
|
+
"
|
|
298
|
+
`);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('ignore refetchingStatus by default', async () => {
|
|
302
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
303
|
+
initialServerData,
|
|
304
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const renders = createRenderStore();
|
|
308
|
+
|
|
309
|
+
renderHook(() => {
|
|
310
|
+
const selectionResult = listQueryStore.useListQuery(
|
|
311
|
+
{ tableId: 'users' },
|
|
312
|
+
{ itemSelector: (data) => data.name },
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
renders.add(pick(selectionResult, ['status', 'isLoading', 'items']));
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
await serverMock.waitFetchIdle();
|
|
319
|
+
|
|
320
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
321
|
+
"
|
|
322
|
+
status: success -- isLoading: false -- items: [User 1, ...(4 more)]
|
|
323
|
+
"
|
|
324
|
+
`);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test('use ensureIsLoaded prop with disabled', async () => {
|
|
328
|
+
const { store: listQueryStore, serverMock } = createDefaultListQueryStore({
|
|
329
|
+
initialServerData,
|
|
330
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
const renders = createRenderStore();
|
|
334
|
+
|
|
335
|
+
const Comp = ({ payload }: { payload?: FetchQueryParams }) => {
|
|
336
|
+
const selectionResult = listQueryStore.useListQuery(payload, {
|
|
337
|
+
ensureIsLoaded: true,
|
|
338
|
+
itemSelector: (data) => data.name,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
renders.add(
|
|
342
|
+
pick(selectionResult, ['status', 'payload', 'isLoading', 'items']),
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
return <div />;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const { rerender } = render(<Comp />);
|
|
349
|
+
|
|
350
|
+
expect(renders.snapshot).toMatchSnapshotString(`
|
|
351
|
+
"
|
|
352
|
+
status: idle -- payload: undefined -- isLoading: false -- items: []
|
|
353
|
+
"
|
|
354
|
+
`);
|
|
355
|
+
|
|
356
|
+
rerender(<Comp payload={{ tableId: 'users' }} />);
|
|
357
|
+
|
|
358
|
+
await serverMock.waitFetchIdle();
|
|
359
|
+
|
|
360
|
+
expect(renders.snapshot).toMatchSnapshotString(`
|
|
361
|
+
"
|
|
362
|
+
status: idle -- payload: undefined -- isLoading: false -- items: []
|
|
363
|
+
---
|
|
364
|
+
status: loading -- payload: {tableId:users} -- isLoading: true -- items: [User 1, ...(4 more)]
|
|
365
|
+
status: success -- payload: {tableId:users} -- isLoading: false -- items: [User 1, ...(4 more)]
|
|
366
|
+
"
|
|
367
|
+
`);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
test('disableRefetchOnMount', async () => {
|
|
371
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
372
|
+
initialServerData,
|
|
373
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
const compRenders = createRenderStore();
|
|
377
|
+
|
|
378
|
+
renderHook(() => {
|
|
379
|
+
const data = listQueryStore.useListQuery(
|
|
380
|
+
{ tableId: 'users' },
|
|
381
|
+
{ disableRefetchOnMount: true },
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
compRenders.add({ status: data.status, items: data.items });
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// wait some time to make sure the query is not refetched
|
|
388
|
+
await sleep(200);
|
|
389
|
+
|
|
390
|
+
expect(compRenders.snapshot).toMatchInlineSnapshot(`
|
|
391
|
+
"
|
|
392
|
+
status: success -- items: [{id:users||1, data:{id:1, name:User 1}}, ...(4 more)]
|
|
393
|
+
"
|
|
394
|
+
`);
|
|
395
|
+
|
|
396
|
+
expect(serverMock.fetchsCount).toBe(0);
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
describe('useItem', () => {
|
|
401
|
+
test('disable then enable the initial fetch', async () => {
|
|
402
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
403
|
+
initialServerData,
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
const renders = createRenderStore();
|
|
407
|
+
|
|
408
|
+
const Comp = ({
|
|
409
|
+
payload,
|
|
410
|
+
}: {
|
|
411
|
+
payload: string | false | undefined | null;
|
|
412
|
+
}) => {
|
|
413
|
+
const queryResult = listQueryStore.useItem(payload);
|
|
414
|
+
|
|
415
|
+
renders.add(pick(queryResult, ['status', 'itemId', 'data']));
|
|
416
|
+
|
|
417
|
+
return <div />;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const { rerender } = render(<Comp payload={false} />);
|
|
421
|
+
|
|
422
|
+
expect(renders.getSnapshot()).toMatchInlineSnapshot(`
|
|
423
|
+
"
|
|
424
|
+
status: idle -- itemId: '' -- data: null
|
|
425
|
+
"
|
|
426
|
+
`);
|
|
427
|
+
|
|
428
|
+
expect(serverMock.fetchsCount).toBe(0);
|
|
429
|
+
|
|
430
|
+
rerender(<Comp payload="users||1" />);
|
|
431
|
+
|
|
432
|
+
await serverMock.waitFetchIdle();
|
|
433
|
+
|
|
434
|
+
expect(serverMock.fetchsCount).toBe(1);
|
|
435
|
+
|
|
436
|
+
expect(renders.getSnapshot()).toMatchInlineSnapshot(`
|
|
437
|
+
"
|
|
438
|
+
status: idle -- itemId: '' -- data: null
|
|
439
|
+
---
|
|
440
|
+
status: loading -- itemId: users||1 -- data: null
|
|
441
|
+
status: success -- itemId: users||1 -- data: {id:1, name:User 1}
|
|
442
|
+
"
|
|
443
|
+
`);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
test('ignore refetchingStatus by default', async () => {
|
|
447
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
448
|
+
initialServerData,
|
|
449
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
const renders = createRenderStore();
|
|
453
|
+
|
|
454
|
+
renderHook(() => {
|
|
455
|
+
const selectionResult = listQueryStore.useItem('users||1', {
|
|
456
|
+
selector: (data) => data?.name,
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
renders.add(pick(selectionResult, ['status', 'isLoading', 'data']));
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
await serverMock.waitFetchIdle();
|
|
463
|
+
|
|
464
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
465
|
+
"
|
|
466
|
+
status: success -- isLoading: false -- data: User 1
|
|
467
|
+
"
|
|
468
|
+
`);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
test('use ensureIsLoaded prop', async () => {
|
|
472
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
473
|
+
initialServerData,
|
|
474
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
const renders = createRenderStore();
|
|
478
|
+
|
|
479
|
+
renderHook(() => {
|
|
480
|
+
const selectionResult = listQueryStore.useItem('users||1', {
|
|
481
|
+
ensureIsLoaded: true,
|
|
482
|
+
selector: (data) => data?.name,
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
renders.add(pick(selectionResult, ['status', 'isLoading', 'data']));
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
await serverMock.waitFetchIdle();
|
|
489
|
+
|
|
490
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
491
|
+
"
|
|
492
|
+
status: loading -- isLoading: true -- data: User 1
|
|
493
|
+
status: success -- isLoading: false -- data: User 1
|
|
494
|
+
"
|
|
495
|
+
`);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
test('use ensureIsLoaded prop with disabled', async () => {
|
|
499
|
+
const { store: listQueryStore, serverMock } = createDefaultListQueryStore({
|
|
500
|
+
initialServerData,
|
|
501
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
const renders = createRenderStore();
|
|
505
|
+
|
|
506
|
+
const Comp = ({ payload }: { payload?: string }) => {
|
|
507
|
+
const selectionResult = listQueryStore.useItem(payload, {
|
|
508
|
+
ensureIsLoaded: true,
|
|
509
|
+
selector: (data) => data?.name ?? null,
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
renders.add(
|
|
513
|
+
pick(selectionResult, ['status', 'itemId', 'isLoading', 'data']),
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
return <div />;
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const { rerender } = render(<Comp />);
|
|
520
|
+
|
|
521
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
522
|
+
"
|
|
523
|
+
status: idle -- itemId: '' -- isLoading: false -- data: null
|
|
524
|
+
"
|
|
525
|
+
`);
|
|
526
|
+
|
|
527
|
+
rerender(<Comp payload="users||1" />);
|
|
528
|
+
|
|
529
|
+
await serverMock.waitFetchIdle();
|
|
530
|
+
|
|
531
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
532
|
+
"
|
|
533
|
+
status: idle -- itemId: '' -- isLoading: false -- data: null
|
|
534
|
+
---
|
|
535
|
+
status: loading -- itemId: users||1 -- isLoading: true -- data: User 1
|
|
536
|
+
status: success -- itemId: users||1 -- isLoading: false -- data: User 1
|
|
537
|
+
"
|
|
538
|
+
`);
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
test('disableRefetchOnMount', async () => {
|
|
542
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
543
|
+
initialServerData,
|
|
544
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
const compRenders = createRenderStore();
|
|
548
|
+
|
|
549
|
+
renderHook(() => {
|
|
550
|
+
const data = listQueryStore.useItem('users||1', {
|
|
551
|
+
disableRefetchOnMount: true,
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
compRenders.add({ status: data.status, data: data.data });
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
// wait some time to make sure the query is not refetched
|
|
558
|
+
await sleep(200);
|
|
559
|
+
|
|
560
|
+
expect(compRenders.snapshot).toMatchInlineSnapshot(`
|
|
561
|
+
"
|
|
562
|
+
status: success -- data: {id:1, name:User 1}
|
|
563
|
+
"
|
|
564
|
+
`);
|
|
565
|
+
|
|
566
|
+
expect(serverMock.fetchsCount).toBe(0);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
test('use deleted item', async () => {
|
|
570
|
+
const { serverMock, store } = createDefaultListQueryStore({
|
|
571
|
+
initialServerData,
|
|
572
|
+
useLoadedSnapshot: { tables: ['users'] },
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
const renders = createRenderStore();
|
|
576
|
+
|
|
577
|
+
renderHook(() => {
|
|
578
|
+
const selectionResult = store.useItem('users||2', {
|
|
579
|
+
returnRefetchingStatus: true,
|
|
580
|
+
disableRefetchOnMount: true,
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
renders.add(pick(selectionResult, ['status', 'itemId', 'data']));
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
store.deleteItemState('users||2');
|
|
587
|
+
serverMock.produceData((draft) => {
|
|
588
|
+
draft.users!.splice(1, 1);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
await renders.waitNextRender();
|
|
592
|
+
|
|
593
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
594
|
+
"
|
|
595
|
+
status: success -- itemId: users||2 -- data: {id:2, name:User 2}
|
|
596
|
+
status: deleted -- itemId: users||2 -- data: null
|
|
597
|
+
"
|
|
598
|
+
`);
|
|
599
|
+
|
|
600
|
+
shouldNotSkip(store.scheduleItemFetch('highPriority', 'users||2'));
|
|
601
|
+
|
|
602
|
+
await serverMock.waitFetchIdle();
|
|
603
|
+
|
|
604
|
+
expect(renders.snapshot).toMatchInlineSnapshot(`
|
|
605
|
+
"
|
|
606
|
+
status: success -- itemId: users||2 -- data: {id:2, name:User 2}
|
|
607
|
+
status: deleted -- itemId: users||2 -- data: null
|
|
608
|
+
---
|
|
609
|
+
status: loading -- itemId: users||2 -- data: null
|
|
610
|
+
status: error -- itemId: users||2 -- data: null
|
|
611
|
+
"
|
|
612
|
+
`);
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
test('invalidate one item', async () => {
|
|
616
|
+
const { serverMock, store: listQueryStore } = createDefaultListQueryStore({
|
|
617
|
+
initialServerData,
|
|
618
|
+
useLoadedSnapshot: { tables: ['users', 'products'] },
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
const users2 = createRenderStore();
|
|
622
|
+
const products1 = createRenderStore();
|
|
623
|
+
|
|
624
|
+
const { result } = renderHook(() => {
|
|
625
|
+
const [usersResult, productsResult] = listQueryStore.useMultipleItems(
|
|
626
|
+
['users||2', 'products||1'],
|
|
627
|
+
{
|
|
628
|
+
returnRefetchingStatus: true,
|
|
629
|
+
disableRefetchOnMount: true,
|
|
630
|
+
},
|
|
631
|
+
);
|
|
632
|
+
|
|
633
|
+
users2.add(pick(usersResult, ['status', 'itemId', 'data']));
|
|
634
|
+
products1.add(pick(productsResult, ['status', 'itemId', 'data']));
|
|
635
|
+
|
|
636
|
+
return { usersResult, productsResult };
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
serverMock.produceData((draft) => {
|
|
640
|
+
draft.users![1]!.name = 'Updated User 2';
|
|
641
|
+
});
|
|
642
|
+
listQueryStore.invalidateItem('users||2');
|
|
643
|
+
|
|
644
|
+
await serverMock.waitFetchIdle();
|
|
645
|
+
|
|
646
|
+
expect(users2.renderCount()).toBeGreaterThan(0);
|
|
647
|
+
|
|
648
|
+
expect(users2.changesSnapshot).toMatchInlineSnapshot(`
|
|
649
|
+
"
|
|
650
|
+
status: success -- itemId: users||2 -- data: {id:2, name:User 2}
|
|
651
|
+
status: refetching -- itemId: users||2 -- data: {id:2, name:User 2}
|
|
652
|
+
status: success -- itemId: users||2 -- data: {id:2, name:Updated User 2}
|
|
653
|
+
"
|
|
654
|
+
`);
|
|
655
|
+
expect(result.current.usersResult?.data.name).toBe('Updated User 2');
|
|
656
|
+
expect(products1.changesSnapshot).toMatchInlineSnapshot(`
|
|
657
|
+
"
|
|
658
|
+
status: success -- itemId: products||1 -- data: {id:1, name:Product 1}
|
|
659
|
+
"
|
|
660
|
+
`);
|
|
661
|
+
});
|
|
662
|
+
});
|