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