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.
Files changed (49) hide show
  1. package/.eslintrc.cjs +79 -16
  2. package/.vscode/tasks.json +11 -1
  3. package/fetch-orquestrator-scenarios.excalidraw +10115 -0
  4. package/package.json +21 -13
  5. package/src/collectionFetchOrquestrator.ts +23 -0
  6. package/src/collectionStore.ts +608 -0
  7. package/src/documentStore.ts +272 -0
  8. package/src/fetchOrquestrator.ts +335 -0
  9. package/src/listQueryStore.ts +1190 -0
  10. package/src/main.ts +22 -1
  11. package/src/storeShared.ts +5 -0
  12. package/src/useEnsureIsLoaded.ts +50 -0
  13. package/src/utils/filterAndMap.ts +20 -0
  14. package/src/utils/getCacheId.ts +19 -0
  15. package/src/utils/hooks.ts +110 -0
  16. package/src/utils/isObject.ts +3 -0
  17. package/src/utils/math.ts +7 -0
  18. package/src/utils/serializableClone.ts +3 -0
  19. package/src/utils/useLatestValue.ts +16 -0
  20. package/test/__snapshots__/listQueryStore.test.ts.snap +146 -0
  21. package/test/__snapshots__/listQueryStoreHooks.test.ts.snap +9 -0
  22. package/test/collectionStore.test.ts +523 -0
  23. package/test/collectionStoreHooks.test.tsx +651 -0
  24. package/test/documentStore.test.ts +183 -0
  25. package/test/documentStoreHooks.test.tsx +857 -0
  26. package/test/fetchOrquestrator.test.ts +913 -0
  27. package/test/listQueryStore.test.ts +1158 -0
  28. package/test/listQueryStoreHooks.test.tsx +662 -0
  29. package/test/listQueryStoreHooks2.test.tsx +483 -0
  30. package/test/listQueryStoreHooksMutations.test.tsx +764 -0
  31. package/test/mocks/fetchMock.ts +296 -0
  32. package/test/mocks/fetchOrquestratorEnv.ts +370 -0
  33. package/test/setup/setup.ts +95 -1
  34. package/test/testHelpers.test.ts +88 -0
  35. package/test/utils/RenderObjValues.tsx +23 -0
  36. package/test/utils/arrayUtils.ts +13 -0
  37. package/test/utils/createDefaultListQueryStore.ts +275 -0
  38. package/test/utils/createOrVariations.ts +48 -0
  39. package/test/utils/dedent.ts +50 -0
  40. package/test/utils/delayCall.ts +25 -0
  41. package/test/utils/math.ts +3 -0
  42. package/test/utils/objectUtils.ts +18 -0
  43. package/test/utils/range.ts +19 -0
  44. package/test/utils/sleep.ts +3 -0
  45. package/test/utils/storeUtils.ts +460 -0
  46. package/tsconfig.prod.json +1 -1
  47. package/tsup.config.ts +1 -1
  48. package/vitest.config.ts +2 -1
  49. package/test/test.test.ts +0 -5
@@ -0,0 +1,1158 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import {
3
+ createDefaultListQueryStore,
4
+ Tables,
5
+ } from './utils/createDefaultListQueryStore';
6
+ import { range } from './utils/range';
7
+ import { sleep } from './utils/sleep';
8
+ import { simplifyArraySnapshot } from './utils/storeUtils';
9
+
10
+ const initialServerData: Tables = {
11
+ users: range(1, 5).map((id) => ({ id, name: `User ${id}` })),
12
+ products: range(1, 50).map((id) => ({ id, name: `Product ${id}` })),
13
+ orders: range(1, 50).map((id) => ({ id, name: `Order ${id}` })),
14
+ };
15
+
16
+ const createTestEnv = createDefaultListQueryStore;
17
+
18
+ const usersQueryParams = { tableId: 'users' };
19
+
20
+ describe.concurrent('test helpers', () => {
21
+ test('snapshot is equal to loaded state', async () => {
22
+ const loaded = createTestEnv({
23
+ initialServerData,
24
+ });
25
+
26
+ loaded.forceListUpdate(usersQueryParams);
27
+
28
+ await loaded.serverMock.waitFetchIdle();
29
+
30
+ const withUserSnapshot = createTestEnv({
31
+ initialServerData,
32
+ useLoadedSnapshot: { tables: ['users'] },
33
+ });
34
+
35
+ expect(loaded.store.store.state).toEqual(
36
+ withUserSnapshot.store.store.state,
37
+ );
38
+
39
+ loaded.forceListUpdate({ tableId: 'products' });
40
+
41
+ await loaded.serverMock.waitFetchIdle();
42
+
43
+ const withSnapshot = createTestEnv({
44
+ initialServerData,
45
+ useLoadedSnapshot: { tables: ['users', 'products'] },
46
+ });
47
+
48
+ expect(loaded.store.store.state).toEqual(withSnapshot.store.store.state);
49
+ });
50
+
51
+ test('snapshot with filter is equal to loaded state', async () => {
52
+ const loaded = createTestEnv({
53
+ initialServerData,
54
+ });
55
+
56
+ loaded.forceListUpdate({
57
+ tableId: 'users',
58
+ filters: { idIsGreaterThan: 2 },
59
+ });
60
+
61
+ await loaded.serverMock.waitFetchIdle();
62
+
63
+ const withUserSnapshot = createTestEnv({
64
+ initialServerData,
65
+ useLoadedSnapshot: {
66
+ queries: [{ tableId: 'users', filters: { idIsGreaterThan: 2 } }],
67
+ },
68
+ });
69
+
70
+ expect(loaded.store.store.state).toEqual(
71
+ withUserSnapshot.store.store.state,
72
+ );
73
+ });
74
+ });
75
+
76
+ describe.concurrent('fetch query', () => {
77
+ test('fetch query', async () => {
78
+ const { serverMock, store: listQueryStore } = createTestEnv({
79
+ initialServerData,
80
+ });
81
+
82
+ expect(listQueryStore.store.state).toEqual({
83
+ items: {},
84
+ queries: {},
85
+ itemQueries: {},
86
+ });
87
+
88
+ listQueryStore.scheduleListQueryFetch('lowPriority', usersQueryParams);
89
+
90
+ expect(listQueryStore.getQueryState(usersQueryParams)).toEqual({
91
+ error: null,
92
+ hasMore: false,
93
+ items: [],
94
+ refetchOnMount: false,
95
+ status: 'loading',
96
+ payload: { tableId: 'users' },
97
+ wasLoaded: false,
98
+ });
99
+ expect(listQueryStore.store.state.items).toEqual({});
100
+
101
+ await serverMock.waitFetchIdle();
102
+
103
+ expect(listQueryStore.getQueryState(usersQueryParams))
104
+ .toMatchSnapshotString(`
105
+ {
106
+ "error": null,
107
+ "hasMore": false,
108
+ "items": [
109
+ "users||1",
110
+ "users||2",
111
+ "users||3",
112
+ "users||4",
113
+ "users||5",
114
+ ],
115
+ "payload": {
116
+ "tableId": "users",
117
+ },
118
+ "refetchOnMount": false,
119
+ "status": "success",
120
+ "wasLoaded": true,
121
+ }
122
+ `);
123
+ expect(listQueryStore.store.state.items).toMatchSnapshot();
124
+
125
+ expect(serverMock.fetchsCount).toBe(1);
126
+ });
127
+
128
+ test('refetch list with updated data', async ({ expect }) => {
129
+ const { serverMock, store: listQueryStore } = createTestEnv({
130
+ initialServerData,
131
+ useLoadedSnapshot: { tables: ['users'] },
132
+ });
133
+
134
+ serverMock.produceData((draft) => {
135
+ draft['users']![0]!.name = 'Updated User 1';
136
+ });
137
+
138
+ listQueryStore.scheduleListQueryFetch('highPriority', usersQueryParams);
139
+
140
+ expect(listQueryStore.getQueryState(usersQueryParams))
141
+ .toMatchSnapshotString(`
142
+ {
143
+ "error": null,
144
+ "hasMore": false,
145
+ "items": [
146
+ "users||1",
147
+ "users||2",
148
+ "users||3",
149
+ "users||4",
150
+ "users||5",
151
+ ],
152
+ "payload": {
153
+ "tableId": "users",
154
+ },
155
+ "refetchOnMount": false,
156
+ "status": "refetching",
157
+ "wasLoaded": true,
158
+ }
159
+ `);
160
+ expect(listQueryStore.store.state.items).toMatchSnapshot();
161
+
162
+ await serverMock.waitFetchIdle();
163
+
164
+ expect(listQueryStore.getQueryState(usersQueryParams))
165
+ .toMatchSnapshotString(`
166
+ {
167
+ "error": null,
168
+ "hasMore": false,
169
+ "items": [
170
+ "users||1",
171
+ "users||2",
172
+ "users||3",
173
+ "users||4",
174
+ "users||5",
175
+ ],
176
+ "payload": {
177
+ "tableId": "users",
178
+ },
179
+ "refetchOnMount": false,
180
+ "status": "success",
181
+ "wasLoaded": true,
182
+ }
183
+ `);
184
+ expect(listQueryStore.getItemState('users||1')).toMatchSnapshotString(`
185
+ {
186
+ "id": 1,
187
+ "name": "Updated User 1",
188
+ }
189
+ `);
190
+ expect(listQueryStore.store.state.items).toMatchSnapshot();
191
+
192
+ expect(serverMock.fetchsCount).toBe(1);
193
+ });
194
+
195
+ test('refetch list with error', async () => {
196
+ const {
197
+ serverMock,
198
+ store: listQueryStore,
199
+ forceListUpdate,
200
+ } = createTestEnv({
201
+ initialServerData,
202
+ useLoadedSnapshot: { tables: ['users'] },
203
+ });
204
+
205
+ serverMock.setFetchError('error');
206
+
207
+ forceListUpdate(usersQueryParams);
208
+
209
+ await serverMock.waitFetchIdle();
210
+
211
+ expect(listQueryStore.getQueryState(usersQueryParams))
212
+ .toMatchSnapshotString(`
213
+ {
214
+ "error": {
215
+ "message": "error",
216
+ },
217
+ "hasMore": false,
218
+ "items": [
219
+ "users||1",
220
+ "users||2",
221
+ "users||3",
222
+ "users||4",
223
+ "users||5",
224
+ ],
225
+ "payload": {
226
+ "tableId": "users",
227
+ },
228
+ "refetchOnMount": false,
229
+ "status": "error",
230
+ "wasLoaded": true,
231
+ }
232
+ `);
233
+
234
+ // refetch with success
235
+ serverMock.setFetchError(null);
236
+
237
+ forceListUpdate(usersQueryParams);
238
+
239
+ expect(listQueryStore.getQueryState(usersQueryParams))
240
+ .toMatchSnapshotString(`
241
+ {
242
+ "error": null,
243
+ "hasMore": false,
244
+ "items": [
245
+ "users||1",
246
+ "users||2",
247
+ "users||3",
248
+ "users||4",
249
+ "users||5",
250
+ ],
251
+ "payload": {
252
+ "tableId": "users",
253
+ },
254
+ "refetchOnMount": false,
255
+ "status": "refetching",
256
+ "wasLoaded": true,
257
+ }
258
+ `);
259
+
260
+ await serverMock.waitFetchIdle();
261
+
262
+ expect(listQueryStore.getQueryState(usersQueryParams))
263
+ .toMatchSnapshotString(`
264
+ {
265
+ "error": null,
266
+ "hasMore": false,
267
+ "items": [
268
+ "users||1",
269
+ "users||2",
270
+ "users||3",
271
+ "users||4",
272
+ "users||5",
273
+ ],
274
+ "payload": {
275
+ "tableId": "users",
276
+ },
277
+ "refetchOnMount": false,
278
+ "status": "success",
279
+ "wasLoaded": true,
280
+ }
281
+ `);
282
+ });
283
+
284
+ test('load list with error', async () => {
285
+ const {
286
+ serverMock,
287
+ store: listQueryStore,
288
+ forceListUpdate,
289
+ } = createTestEnv({
290
+ initialServerData,
291
+ });
292
+
293
+ serverMock.setFetchError('error');
294
+
295
+ forceListUpdate(usersQueryParams);
296
+
297
+ await serverMock.waitFetchIdle();
298
+
299
+ expect(listQueryStore.getQueryState(usersQueryParams))
300
+ .toMatchSnapshotString(`
301
+ {
302
+ "error": {
303
+ "message": "error",
304
+ },
305
+ "hasMore": false,
306
+ "items": [],
307
+ "payload": {
308
+ "tableId": "users",
309
+ },
310
+ "refetchOnMount": false,
311
+ "status": "error",
312
+ "wasLoaded": false,
313
+ }
314
+ `);
315
+
316
+ // refetch with success
317
+ serverMock.setFetchError(null);
318
+
319
+ forceListUpdate(usersQueryParams);
320
+
321
+ expect(listQueryStore.getQueryState(usersQueryParams))
322
+ .toMatchSnapshotString(`
323
+ {
324
+ "error": null,
325
+ "hasMore": false,
326
+ "items": [],
327
+ "payload": {
328
+ "tableId": "users",
329
+ },
330
+ "refetchOnMount": false,
331
+ "status": "loading",
332
+ "wasLoaded": false,
333
+ }
334
+ `);
335
+
336
+ await serverMock.waitFetchIdle();
337
+
338
+ expect(listQueryStore.getQueryState(usersQueryParams))
339
+ .toMatchSnapshotString(`
340
+ {
341
+ "error": null,
342
+ "hasMore": false,
343
+ "items": [
344
+ "users||1",
345
+ "users||2",
346
+ "users||3",
347
+ "users||4",
348
+ "users||5",
349
+ ],
350
+ "payload": {
351
+ "tableId": "users",
352
+ },
353
+ "refetchOnMount": false,
354
+ "status": "success",
355
+ "wasLoaded": true,
356
+ }
357
+ `);
358
+ });
359
+
360
+ test('fetch with size', async ({ expect }) => {
361
+ const query = { tableId: 'products' as const };
362
+
363
+ const {
364
+ serverMock,
365
+ store: listQueryStore,
366
+ forceListUpdate,
367
+ } = createTestEnv({
368
+ initialServerData,
369
+ defaultQuerySize: 5,
370
+ });
371
+
372
+ listQueryStore.scheduleListQueryFetch('highPriority', query, 5);
373
+
374
+ await serverMock.waitFetchIdle();
375
+
376
+ expect(listQueryStore.getQueryState(query)).toMatchSnapshotString(`
377
+ {
378
+ "error": null,
379
+ "hasMore": true,
380
+ "items": [
381
+ "products||1",
382
+ "products||2",
383
+ "products||3",
384
+ "products||4",
385
+ "products||5",
386
+ ],
387
+ "payload": {
388
+ "tableId": "products",
389
+ },
390
+ "refetchOnMount": false,
391
+ "status": "success",
392
+ "wasLoaded": true,
393
+ }
394
+ `);
395
+ expect(listQueryStore.store.state.items).toMatchSnapshot();
396
+
397
+ expect(serverMock.fetchsCount).toBe(1);
398
+
399
+ // load more
400
+ listQueryStore.loadMore(query, 5);
401
+
402
+ expect(listQueryStore.getQueryState(query)).toMatchSnapshotString(`
403
+ {
404
+ "error": null,
405
+ "hasMore": true,
406
+ "items": [
407
+ "products||1",
408
+ "products||2",
409
+ "products||3",
410
+ "products||4",
411
+ "products||5",
412
+ ],
413
+ "payload": {
414
+ "tableId": "products",
415
+ },
416
+ "refetchOnMount": false,
417
+ "status": "loadingMore",
418
+ "wasLoaded": true,
419
+ }
420
+ `);
421
+
422
+ await serverMock.waitFetchIdle();
423
+
424
+ expect(listQueryStore.getQueryState(query)).toMatchSnapshotString(`
425
+ {
426
+ "error": null,
427
+ "hasMore": true,
428
+ "items": [
429
+ "products||1",
430
+ "products||2",
431
+ "products||3",
432
+ "products||4",
433
+ "products||5",
434
+ "products||6",
435
+ "products||7",
436
+ "products||8",
437
+ "products||9",
438
+ "products||10",
439
+ ],
440
+ "payload": {
441
+ "tableId": "products",
442
+ },
443
+ "refetchOnMount": false,
444
+ "status": "success",
445
+ "wasLoaded": true,
446
+ }
447
+ `);
448
+ expect(listQueryStore.store.state.items).toMatchSnapshot();
449
+
450
+ // refetch keep size
451
+ forceListUpdate(query);
452
+
453
+ await serverMock.waitFetchIdle();
454
+
455
+ expect(listQueryStore.getQueryState(query)).toMatchSnapshotString(`
456
+ {
457
+ "error": null,
458
+ "hasMore": true,
459
+ "items": [
460
+ "products||1",
461
+ "products||2",
462
+ "products||3",
463
+ "products||4",
464
+ "products||5",
465
+ "products||6",
466
+ "products||7",
467
+ "products||8",
468
+ "products||9",
469
+ "products||10",
470
+ ],
471
+ "payload": {
472
+ "tableId": "products",
473
+ },
474
+ "refetchOnMount": false,
475
+ "status": "success",
476
+ "wasLoaded": true,
477
+ }
478
+ `);
479
+ });
480
+
481
+ test('do not load more if the query not exists or hasMore === false', () => {
482
+ const { store: listQueryStore } = createTestEnv({
483
+ initialServerData,
484
+ useLoadedSnapshot: { tables: ['users'] },
485
+ });
486
+
487
+ expect(listQueryStore.loadMore(usersQueryParams, 10)).toBe('skipped');
488
+ expect(listQueryStore.loadMore({ tableId: 'not found' }, 10)).toBe(
489
+ 'skipped',
490
+ );
491
+ });
492
+
493
+ test.concurrent(
494
+ 'multiple fetchs with different payloads not cancel each other, but cancel same payload fetchs',
495
+ async () => {
496
+ const { serverMock, store: listQueryStore } = createTestEnv({
497
+ initialServerData,
498
+ });
499
+
500
+ const fetch = listQueryStore.scheduleListQueryFetch;
501
+
502
+ fetch('lowPriority', { tableId: 'users' });
503
+ fetch('lowPriority', { tableId: 'products' });
504
+ fetch('lowPriority', { tableId: 'orders' });
505
+
506
+ fetch('lowPriority', { tableId: 'users' });
507
+ fetch('highPriority', { tableId: 'products' });
508
+ fetch('lowPriority', { tableId: 'orders' });
509
+
510
+ await sleep(10);
511
+
512
+ fetch('lowPriority', { tableId: 'users' });
513
+ fetch('lowPriority', { tableId: 'products' });
514
+ fetch('lowPriority', { tableId: 'orders' });
515
+
516
+ await serverMock.waitFetchIdle(40);
517
+
518
+ expect(serverMock.fetchsCount).toBe(3);
519
+
520
+ fetch('lowPriority', { tableId: 'users' });
521
+ fetch('lowPriority', { tableId: 'products' });
522
+ fetch('lowPriority', { tableId: 'orders' });
523
+
524
+ fetch('highPriority', { tableId: 'users' });
525
+ fetch('highPriority', { tableId: 'products' });
526
+ fetch('highPriority', { tableId: 'orders' });
527
+
528
+ await serverMock.waitFetchIdle(40);
529
+
530
+ expect(serverMock.fetchsCount).toBe(6);
531
+ },
532
+ );
533
+ });
534
+
535
+ test.concurrent('await fetch', async () => {
536
+ const { serverMock, store: listQueryStore } = createTestEnv({
537
+ initialServerData,
538
+ useLoadedSnapshot: { tables: ['users'] },
539
+ });
540
+
541
+ serverMock.produceData((draft) => {
542
+ draft['users']![0]!.name = 'Updated User 1';
543
+ });
544
+
545
+ expect(listQueryStore.getItemState('users||1')).toMatchObject({
546
+ name: 'User 1',
547
+ });
548
+
549
+ expect(
550
+ await listQueryStore.awaitListQueryFetch({ tableId: 'users' }),
551
+ ).toEqual({
552
+ items: [
553
+ { id: 'users||1', data: { id: 1, name: 'Updated User 1' } },
554
+ { id: 'users||2', data: { id: 2, name: 'User 2' } },
555
+ { id: 'users||3', data: { id: 3, name: 'User 3' } },
556
+ { id: 'users||4', data: { id: 4, name: 'User 4' } },
557
+ { id: 'users||5', data: { id: 5, name: 'User 5' } },
558
+ ],
559
+ error: null,
560
+ hasMore: false,
561
+ });
562
+
563
+ serverMock.setFetchError('error');
564
+
565
+ expect(
566
+ await listQueryStore.awaitListQueryFetch({ tableId: 'users' }, 2),
567
+ ).toEqual({
568
+ items: [],
569
+ error: { message: 'error' },
570
+ hasMore: false,
571
+ });
572
+
573
+ expect(serverMock.fetchsCount).toEqual(2);
574
+ });
575
+
576
+ describe.concurrent('fetch item', () => {
577
+ test.concurrent('fetch item', async () => {
578
+ const { serverMock, store: listQueryStore } = createTestEnv({
579
+ initialServerData,
580
+ });
581
+
582
+ expect(
583
+ listQueryStore.store.state.itemQueries['users||1'],
584
+ ).toMatchSnapshotString('undefined');
585
+
586
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||1');
587
+
588
+ expect(listQueryStore.store.state.itemQueries['users||1'])
589
+ .toMatchSnapshotString(`
590
+ {
591
+ "error": null,
592
+ "refetchOnMount": false,
593
+ "status": "loading",
594
+ "wasLoaded": false,
595
+ }
596
+ `);
597
+ expect(listQueryStore.getItemState('users||1')).toMatchSnapshotString(
598
+ 'undefined',
599
+ );
600
+
601
+ await serverMock.waitFetchIdle();
602
+
603
+ expect(listQueryStore.store.state.itemQueries['users||1'])
604
+ .toMatchSnapshotString(`
605
+ {
606
+ "error": null,
607
+ "refetchOnMount": false,
608
+ "status": "success",
609
+ "wasLoaded": true,
610
+ }
611
+ `);
612
+ expect(listQueryStore.getItemState('users||1')).toMatchSnapshotString(`
613
+ {
614
+ "id": 1,
615
+ "name": "User 1",
616
+ }
617
+ `);
618
+
619
+ expect(serverMock.fetchsCount).toBe(1);
620
+ });
621
+
622
+ test.concurrent('await fetch item', async () => {
623
+ const { serverMock, store: listQueryStore } = createTestEnv({
624
+ initialServerData,
625
+ useLoadedSnapshot: { tables: ['users'] },
626
+ });
627
+
628
+ serverMock.produceData((draft) => {
629
+ draft['users']![0]!.name = 'Updated User 1';
630
+ });
631
+
632
+ expect(listQueryStore.getItemState('users||1')).toMatchObject({
633
+ name: 'User 1',
634
+ });
635
+
636
+ expect(await listQueryStore.awaitItemFetch('users||1')).toEqual({
637
+ data: { id: 1, name: 'Updated User 1' },
638
+ error: null,
639
+ });
640
+
641
+ serverMock.setFetchError('error');
642
+
643
+ expect(await listQueryStore.awaitItemFetch('users||1')).toEqual({
644
+ data: null,
645
+ error: { message: 'error' },
646
+ });
647
+
648
+ expect(serverMock.fetchsCount).toEqual(2);
649
+ });
650
+
651
+ test.concurrent('test helpers inital snapshot', async () => {
652
+ const loaded = createTestEnv({ initialServerData });
653
+
654
+ loaded.store.scheduleItemFetch('lowPriority', 'users||1');
655
+
656
+ await loaded.serverMock.waitFetchIdle();
657
+
658
+ const withStateSnapshot = createTestEnv({
659
+ initialServerData,
660
+ useLoadedSnapshot: { items: ['users||1'] },
661
+ });
662
+
663
+ expect(withStateSnapshot.store.store.state).toEqual(
664
+ loaded.store.store.state,
665
+ );
666
+ });
667
+
668
+ test.concurrent('test helpers inital snapshot 2', async () => {
669
+ const loaded = createTestEnv({ initialServerData });
670
+
671
+ loaded.store.scheduleItemFetch('lowPriority', 'users||1');
672
+ loaded.store.scheduleListQueryFetch('lowPriority', {
673
+ tableId: 'users',
674
+ });
675
+
676
+ await loaded.serverMock.waitFetchIdle();
677
+
678
+ const withStateSnapshot = createTestEnv({
679
+ initialServerData,
680
+ useLoadedSnapshot: { tables: ['users'], items: ['users||1'] },
681
+ });
682
+
683
+ expect(withStateSnapshot.store.store.state).toEqual(
684
+ loaded.store.store.state,
685
+ );
686
+ });
687
+
688
+ test('refetch item with updated data', async () => {
689
+ const { serverMock, store: listQueryStore } = createTestEnv({
690
+ initialServerData,
691
+ useLoadedSnapshot: { items: ['users||1'] },
692
+ });
693
+
694
+ serverMock.produceData((draft) => {
695
+ draft['users']![0]!.name = 'Updated User 1';
696
+ });
697
+
698
+ listQueryStore.scheduleItemFetch('highPriority', 'users||1');
699
+
700
+ expect(listQueryStore.store.state.itemQueries['users||1'])
701
+ .toMatchSnapshotString(`
702
+ {
703
+ "error": null,
704
+ "refetchOnMount": false,
705
+ "status": "refetching",
706
+ "wasLoaded": true,
707
+ }
708
+ `);
709
+ expect(listQueryStore.getItemState('users||1')).toMatchSnapshotString(`
710
+ {
711
+ "id": 1,
712
+ "name": "User 1",
713
+ }
714
+ `);
715
+
716
+ await serverMock.waitFetchIdle();
717
+
718
+ expect(listQueryStore.store.state.itemQueries['users||1'])
719
+ .toMatchSnapshotString(`
720
+ {
721
+ "error": null,
722
+ "refetchOnMount": false,
723
+ "status": "success",
724
+ "wasLoaded": true,
725
+ }
726
+ `);
727
+
728
+ expect(listQueryStore.store.state.items).toMatchSnapshotString(`
729
+ {
730
+ "users||1": {
731
+ "id": 1,
732
+ "name": "Updated User 1",
733
+ },
734
+ }
735
+ `);
736
+
737
+ expect(serverMock.fetchsCount).toBe(1);
738
+ });
739
+
740
+ test('refetch item with error', async () => {
741
+ const { serverMock, store: listQueryStore } = createTestEnv({
742
+ initialServerData,
743
+ useLoadedSnapshot: { items: ['users||1'] },
744
+ });
745
+
746
+ serverMock.setFetchError('error');
747
+
748
+ listQueryStore.scheduleItemFetch('highPriority', 'users||1');
749
+
750
+ await serverMock.waitFetchIdle();
751
+
752
+ expect(listQueryStore.store.state.itemQueries['users||1'])
753
+ .toMatchSnapshotString(`
754
+ {
755
+ "error": {
756
+ "message": "error",
757
+ },
758
+ "refetchOnMount": false,
759
+ "status": "error",
760
+ "wasLoaded": true,
761
+ }
762
+ `);
763
+
764
+ // refetch with success
765
+ serverMock.setFetchError(null);
766
+
767
+ listQueryStore.scheduleItemFetch('highPriority', 'users||1');
768
+
769
+ expect(listQueryStore.store.state.itemQueries['users||1'])
770
+ .toMatchSnapshotString(`
771
+ {
772
+ "error": null,
773
+ "refetchOnMount": false,
774
+ "status": "refetching",
775
+ "wasLoaded": true,
776
+ }
777
+ `);
778
+
779
+ await serverMock.waitFetchIdle();
780
+
781
+ expect(listQueryStore.store.state.itemQueries['users||1'])
782
+ .toMatchSnapshotString(`
783
+ {
784
+ "error": null,
785
+ "refetchOnMount": false,
786
+ "status": "success",
787
+ "wasLoaded": true,
788
+ }
789
+ `);
790
+ expect(listQueryStore.store.state.items).toMatchSnapshotString(`
791
+ {
792
+ "users||1": {
793
+ "id": 1,
794
+ "name": "User 1",
795
+ },
796
+ }
797
+ `);
798
+ });
799
+
800
+ test('load item with error', async () => {
801
+ const { serverMock, store: listQueryStore } = createTestEnv({
802
+ initialServerData,
803
+ });
804
+
805
+ serverMock.setFetchError('error');
806
+
807
+ listQueryStore.scheduleItemFetch('highPriority', 'users||1');
808
+
809
+ await serverMock.waitFetchIdle();
810
+
811
+ expect(listQueryStore.store.state.itemQueries['users||1'])
812
+ .toMatchSnapshotString(`
813
+ {
814
+ "error": {
815
+ "message": "error",
816
+ },
817
+ "refetchOnMount": false,
818
+ "status": "error",
819
+ "wasLoaded": false,
820
+ }
821
+ `);
822
+ expect(listQueryStore.store.state.items).toMatchSnapshotString(`{}`);
823
+
824
+ // refetch with success
825
+ serverMock.setFetchError(null);
826
+
827
+ listQueryStore.scheduleItemFetch('highPriority', 'users||1');
828
+
829
+ expect(listQueryStore.store.state.itemQueries['users||1'])
830
+ .toMatchSnapshotString(`
831
+ {
832
+ "error": null,
833
+ "refetchOnMount": false,
834
+ "status": "loading",
835
+ "wasLoaded": false,
836
+ }
837
+ `);
838
+
839
+ await serverMock.waitFetchIdle();
840
+
841
+ expect(listQueryStore.store.state.itemQueries['users||1'])
842
+ .toMatchSnapshotString(`
843
+ {
844
+ "error": null,
845
+ "refetchOnMount": false,
846
+ "status": "success",
847
+ "wasLoaded": true,
848
+ }
849
+ `);
850
+ expect(listQueryStore.store.state.items).toMatchSnapshotString(`
851
+ {
852
+ "users||1": {
853
+ "id": 1,
854
+ "name": "User 1",
855
+ },
856
+ }
857
+ `);
858
+ });
859
+
860
+ test.concurrent(
861
+ 'multiple item fetchs with different ids do not cancel each other, but cancel the ones with same id',
862
+ async () => {
863
+ const { serverMock, store: listQueryStore } = createTestEnv({
864
+ initialServerData,
865
+ });
866
+
867
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||1');
868
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||2');
869
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||3');
870
+
871
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||1');
872
+ listQueryStore.scheduleItemFetch('highPriority', 'users||2');
873
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||3');
874
+
875
+ await sleep(10);
876
+
877
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||1');
878
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||2');
879
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||3');
880
+
881
+ await serverMock.waitFetchIdle(40);
882
+
883
+ expect(serverMock.fetchsCount).toBe(3);
884
+
885
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||1');
886
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||2');
887
+ listQueryStore.scheduleItemFetch('lowPriority', 'users||3');
888
+
889
+ listQueryStore.scheduleItemFetch('highPriority', 'users||1');
890
+ listQueryStore.scheduleItemFetch('highPriority', 'users||2');
891
+ listQueryStore.scheduleItemFetch('highPriority', 'users||3');
892
+
893
+ await serverMock.waitFetchIdle(40);
894
+
895
+ expect(serverMock.fetchsCount).toBe(6);
896
+ },
897
+ );
898
+
899
+ test('load a item that was previously loaded by a query', async () => {
900
+ const { serverMock, store: listQueryStore } = createTestEnv({
901
+ initialServerData,
902
+ useLoadedSnapshot: { tables: ['users'] },
903
+ });
904
+
905
+ listQueryStore.scheduleItemFetch('highPriority', 'users||1');
906
+
907
+ expect(listQueryStore.store.state.itemQueries['users||1'])
908
+ .toMatchSnapshotString(`
909
+ {
910
+ "error": null,
911
+ "refetchOnMount": false,
912
+ "status": "refetching",
913
+ "wasLoaded": true,
914
+ }
915
+ `);
916
+
917
+ await serverMock.waitFetchIdle();
918
+
919
+ expect(listQueryStore.store.state.itemQueries['users||1'])
920
+ .toMatchSnapshotString(`
921
+ {
922
+ "error": null,
923
+ "refetchOnMount": false,
924
+ "status": "success",
925
+ "wasLoaded": true,
926
+ }
927
+ `);
928
+
929
+ expect(listQueryStore.store.state.items['users||1']).toMatchSnapshotString(`
930
+ {
931
+ "id": 1,
932
+ "name": "User 1",
933
+ }
934
+ `);
935
+ });
936
+
937
+ test.concurrent(
938
+ 'load item should share the lowPriority throttle context of the queries',
939
+ async () => {
940
+ const { store: listQueryStore } = createTestEnv({
941
+ initialServerData,
942
+ });
943
+
944
+ listQueryStore.scheduleListQueryFetch('highPriority', {
945
+ tableId: 'users',
946
+ });
947
+
948
+ await sleep(50);
949
+
950
+ const result = listQueryStore.scheduleItemFetch(
951
+ 'lowPriority',
952
+ 'users||1',
953
+ );
954
+
955
+ expect(result).toBe('skipped');
956
+ },
957
+ );
958
+ });
959
+
960
+ describe('update state functions', () => {
961
+ test('update state of one item', () => {
962
+ const { store } = createTestEnv({
963
+ initialServerData,
964
+ useLoadedSnapshot: { tables: ['users'] },
965
+ });
966
+
967
+ expect(store.getItemState('users||1')).toMatchInlineSnapshot(`
968
+ {
969
+ "id": 1,
970
+ "name": "User 1",
971
+ }
972
+ `);
973
+
974
+ store.updateItemState('users||1', (data) => {
975
+ data.name = 'User 1 updated';
976
+ });
977
+
978
+ expect(store.getItemState('users||1')).toMatchInlineSnapshot(`
979
+ {
980
+ "id": 1,
981
+ "name": "User 1 updated",
982
+ }
983
+ `);
984
+ });
985
+
986
+ test('update multiple itens state', () => {
987
+ const { store } = createTestEnv({
988
+ initialServerData,
989
+ useLoadedSnapshot: { tables: ['users'] },
990
+ });
991
+
992
+ store.updateItemState(['users||1', 'users||2'], () => {
993
+ return {
994
+ name: 'new name',
995
+ id: 1,
996
+ };
997
+ });
998
+
999
+ expect(
1000
+ simplifyArraySnapshot(
1001
+ store.getItemState(['users||1', 'users||2', 'users||3']),
1002
+ ),
1003
+ ).toMatchInlineSnapshot(`
1004
+ "
1005
+ id: users||1, data: {name: new name, id: 1}
1006
+ id: users||2, data: {name: new name, id: 1}
1007
+ id: users||3, data: {id: 3, name: User 3}
1008
+ "
1009
+ `);
1010
+ });
1011
+
1012
+ test('update multiple itens state with filter fn', () => {
1013
+ const { store } = createTestEnv({
1014
+ initialServerData,
1015
+ useLoadedSnapshot: { tables: ['users'] },
1016
+ });
1017
+
1018
+ store.updateItemState(
1019
+ (data) => data.id > 2,
1020
+ (data) => {
1021
+ data.name = 'modified';
1022
+ },
1023
+ );
1024
+
1025
+ expect(simplifyArraySnapshot(store.getItemState(() => true)))
1026
+ .toMatchInlineSnapshot(`
1027
+ "
1028
+ id: users||1, data: {id: 1, name: User 1}
1029
+ id: users||2, data: {id: 2, name: User 2}
1030
+ id: users||3, data: {id: 3, name: modified}
1031
+ id: users||4, data: {id: 4, name: modified}
1032
+ id: users||5, data: {id: 5, name: modified}
1033
+ "
1034
+ `);
1035
+ });
1036
+
1037
+ test('create if not exist', () => {
1038
+ const { store } = createTestEnv({
1039
+ initialServerData,
1040
+ useLoadedSnapshot: { tables: ['users'] },
1041
+ });
1042
+
1043
+ let storeUpdates = 0;
1044
+ store.store.subscribe(() => {
1045
+ storeUpdates++;
1046
+ });
1047
+
1048
+ store.updateItemState(
1049
+ '20',
1050
+ (data) => {
1051
+ data.name = 'item 20';
1052
+ },
1053
+ () => {
1054
+ store.addItemToState('users||20', {
1055
+ name: 'item 20',
1056
+ id: 20,
1057
+ });
1058
+ },
1059
+ );
1060
+
1061
+ expect(storeUpdates).toEqual(1);
1062
+
1063
+ expect(simplifyArraySnapshot(store.getItemState(() => true)))
1064
+ .toMatchInlineSnapshot(`
1065
+ "
1066
+ id: users||1, data: {id: 1, name: User 1}
1067
+ id: users||2, data: {id: 2, name: User 2}
1068
+ id: users||3, data: {id: 3, name: User 3}
1069
+ id: users||4, data: {id: 4, name: User 4}
1070
+ id: users||5, data: {id: 5, name: User 5}
1071
+ id: users||20, data: {name: item 20, id: 20}
1072
+ "
1073
+ `);
1074
+ });
1075
+
1076
+ test('addItemToState', () => {
1077
+ const { store } = createTestEnv({
1078
+ initialServerData,
1079
+ useLoadedSnapshot: { tables: ['users'] },
1080
+ });
1081
+
1082
+ expect(store.getItemState('users||20')).toBeUndefined();
1083
+
1084
+ store.addItemToState('users||20', {
1085
+ name: 'item users||20',
1086
+ id: 20,
1087
+ });
1088
+
1089
+ expect(store.getItemState('users||20')).toMatchInlineSnapshot(`
1090
+ {
1091
+ "id": 20,
1092
+ "name": "item users||20",
1093
+ }
1094
+ `);
1095
+ expect(store.store.state.itemQueries['users||20']).toMatchInlineSnapshot(`
1096
+ {
1097
+ "error": null,
1098
+ "refetchOnMount": false,
1099
+ "status": "success",
1100
+ "wasLoaded": true,
1101
+ }
1102
+ `);
1103
+ });
1104
+
1105
+ test('delete item state', () => {
1106
+ const { store } = createTestEnv({
1107
+ initialServerData,
1108
+ useLoadedSnapshot: { tables: ['users'] },
1109
+ });
1110
+
1111
+ expect(store.getItemState('users||1')).toBeDefined();
1112
+
1113
+ store.deleteItemState('users||1');
1114
+
1115
+ expect(store.getItemState('users||1')).toBeNull();
1116
+
1117
+ expect(store.scheduleItemFetch('highPriority', 'users||1')).toBe('started');
1118
+
1119
+ const defaulItemQueryProps = {
1120
+ error: null,
1121
+ refetchOnMount: false,
1122
+ status: 'success',
1123
+ wasLoaded: true,
1124
+ };
1125
+ expect(store.store.state).toEqual({
1126
+ itemQueries: {
1127
+ 'users||1': {
1128
+ error: null,
1129
+ refetchOnMount: false,
1130
+ status: 'loading',
1131
+ wasLoaded: false,
1132
+ },
1133
+ 'users||2': defaulItemQueryProps,
1134
+ 'users||3': defaulItemQueryProps,
1135
+ 'users||4': defaulItemQueryProps,
1136
+ 'users||5': defaulItemQueryProps,
1137
+ },
1138
+ items: {
1139
+ 'users||1': null,
1140
+ 'users||2': { id: 2, name: 'User 2' },
1141
+ 'users||3': { id: 3, name: 'User 3' },
1142
+ 'users||4': { id: 4, name: 'User 4' },
1143
+ 'users||5': { id: 5, name: 'User 5' },
1144
+ },
1145
+ queries: {
1146
+ '[{"tableId":"users"}]': {
1147
+ error: null,
1148
+ hasMore: false,
1149
+ items: ['users||2', 'users||3', 'users||4', 'users||5'],
1150
+ payload: { tableId: 'users' },
1151
+ refetchOnMount: false,
1152
+ status: 'success',
1153
+ wasLoaded: true,
1154
+ },
1155
+ },
1156
+ });
1157
+ });
1158
+ });