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,857 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
+ import {
3
+ act,
4
+ cleanup,
5
+ fireEvent,
6
+ render,
7
+ renderHook,
8
+ } from '@testing-library/react';
9
+ import { useEffect, useState } from 'react';
10
+ import { afterEach, describe, expect, test } from 'vitest';
11
+ import { TSDFDocumentStore, TSDFUseDocumentReturn } from '../src/documentStore';
12
+ import { ServerMock } from './mocks/fetchMock';
13
+ import { pick } from './utils/objectUtils';
14
+ import { sleep } from './utils/sleep';
15
+ import {
16
+ createDefaultDocumentStore,
17
+ createRenderStore,
18
+ createValueStore,
19
+ DefaultDocStoreData as DefaultDocumentStoreData,
20
+ } from './utils/storeUtils';
21
+
22
+ const createTestEnv = createDefaultDocumentStore;
23
+
24
+ async function actionWithOptimisticUpdate(
25
+ serverMock: ServerMock<DefaultDocumentStoreData>,
26
+ documentStore: TSDFDocumentStore<DefaultDocumentStoreData, any>,
27
+ newText: string,
28
+ error?: string,
29
+ ) {
30
+ const endMutation = documentStore.startMutation();
31
+
32
+ documentStore.updateState((draftData) => {
33
+ draftData.hello = newText;
34
+ });
35
+
36
+ try {
37
+ const result = await serverMock.emulateMutation(
38
+ { hello: newText },
39
+ { emulateError: error },
40
+ );
41
+
42
+ endMutation();
43
+
44
+ return result;
45
+ } catch (e) {
46
+ endMutation();
47
+
48
+ documentStore.invalidateData();
49
+ return false;
50
+ }
51
+ }
52
+
53
+ async function actionWithOptimisticUpdateAndRevalidation(
54
+ serverMock: ServerMock<DefaultDocumentStoreData>,
55
+ documentStore: TSDFDocumentStore<DefaultDocumentStoreData, any>,
56
+ newText: string,
57
+ ) {
58
+ const endMutation = documentStore.startMutation();
59
+
60
+ documentStore.updateState((draftData) => {
61
+ draftData.hello = newText;
62
+ });
63
+
64
+ try {
65
+ const result = await serverMock.emulateMutation({ hello: newText });
66
+
67
+ endMutation();
68
+
69
+ return result;
70
+ } catch (e) {
71
+ endMutation();
72
+
73
+ return false;
74
+ } finally {
75
+ documentStore.invalidateData();
76
+ }
77
+ }
78
+
79
+ async function actionWithoutOptimisticUpdate(
80
+ serverMock: ServerMock<DefaultDocumentStoreData>,
81
+ documentStore: TSDFDocumentStore<DefaultDocumentStoreData, any>,
82
+ newText: string,
83
+ ) {
84
+ const endMutation = documentStore.startMutation();
85
+
86
+ try {
87
+ const result = await serverMock.emulateMutation({ hello: newText });
88
+
89
+ documentStore.updateState((draftData) => {
90
+ draftData.hello = result.hello;
91
+ });
92
+
93
+ endMutation();
94
+
95
+ return result;
96
+ } catch (e) {
97
+ endMutation();
98
+
99
+ documentStore.invalidateData();
100
+ return false;
101
+ }
102
+ }
103
+
104
+ async function actionWithRevalidationAndWithoutOptimisticUpdate(
105
+ serverMock: ServerMock<DefaultDocumentStoreData>,
106
+ documentStore: TSDFDocumentStore<DefaultDocumentStoreData, any>,
107
+ newText: string,
108
+ ) {
109
+ const endMutation = documentStore.startMutation();
110
+
111
+ try {
112
+ const result = await serverMock.emulateMutation({ hello: newText });
113
+
114
+ endMutation();
115
+
116
+ return result;
117
+ } catch (e) {
118
+ endMutation();
119
+
120
+ return false;
121
+ } finally {
122
+ documentStore.invalidateData();
123
+ }
124
+ }
125
+
126
+ const Component = ({
127
+ store,
128
+ testIdPrefix = '',
129
+ onRender,
130
+ returnRefetchingStatus,
131
+ enableRefetchOnMount,
132
+ }: {
133
+ store: TSDFDocumentStore<any, any>;
134
+ testIdPrefix?: string;
135
+ returnRefetchingStatus?: boolean;
136
+ enableRefetchOnMount?: boolean;
137
+ onRender?: (renderResult: Readonly<TSDFUseDocumentReturn<any, any>>) => void;
138
+ }) => {
139
+ const selectionResult = store.useDocument({
140
+ returnRefetchingStatus,
141
+ disableRefetchOnMount: !enableRefetchOnMount,
142
+ });
143
+
144
+ onRender?.(selectionResult);
145
+
146
+ return (
147
+ <div>
148
+ <div data-testid={`${testIdPrefix}status`}>{selectionResult.status}</div>
149
+ <div data-testid={`${testIdPrefix}isLoading`}>
150
+ {selectionResult.isLoading ? 'true' : 'false'}
151
+ </div>
152
+ <div data-testid={`${testIdPrefix}data`}>
153
+ {JSON.stringify(selectionResult.data)}
154
+ </div>
155
+ <div data-testid={`${testIdPrefix}error`}>
156
+ {selectionResult.error?.message}
157
+ </div>
158
+ </div>
159
+ );
160
+ };
161
+
162
+ afterEach(() => {
163
+ cleanup();
164
+ });
165
+
166
+ test('load data', async () => {
167
+ const { serverMock, store: documentStore } = createDefaultDocumentStore();
168
+
169
+ const { getByTestId, unmount } = render(<Component store={documentStore} />);
170
+
171
+ expect(getByTestId('status').textContent).toBe('loading');
172
+ expect(getByTestId('isLoading').textContent).toBe('true');
173
+ expect(getByTestId('data').textContent).toBe('null');
174
+
175
+ await sleep(serverMock.fetchDuration + 5);
176
+
177
+ expect(getByTestId('status').textContent).toBe('success');
178
+ expect(getByTestId('isLoading').textContent).toBe('false');
179
+ expect(getByTestId('data').textContent).toBe('{"hello":"world"}');
180
+
181
+ unmount();
182
+ });
183
+
184
+ test('invalidate data', async () => {
185
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
186
+ useLoadedSnapshot: true,
187
+ });
188
+
189
+ const { getByTestId } = render(<Component store={documentStore} />);
190
+
191
+ expect(getByTestId('data').textContent).toBe('{"hello":"world"}');
192
+
193
+ serverMock.mutateData({ hello: 'was invalidated' });
194
+ documentStore.invalidateData();
195
+
196
+ await sleep(serverMock.fetchDuration + 5);
197
+
198
+ expect(getByTestId('data').textContent).toBe('{"hello":"was invalidated"}');
199
+ });
200
+
201
+ test('revalidation with multiple components do not trigger multiple fetchs', async () => {
202
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
203
+ useLoadedSnapshot: true,
204
+ });
205
+
206
+ const { getByTestId } = render(
207
+ <>
208
+ <Component store={documentStore} />
209
+ <Component store={documentStore} />
210
+ <Component store={documentStore} />
211
+ <Component store={documentStore} />
212
+ <Component store={documentStore} />
213
+ <Component store={documentStore} />
214
+ <Component store={documentStore} />
215
+ <Component store={documentStore} />
216
+ <Component store={documentStore} />
217
+ <Component store={documentStore} />
218
+ <Component store={documentStore} />
219
+ <Component store={documentStore} />
220
+ <Component store={documentStore} />
221
+ <Component store={documentStore} />
222
+ <Component store={documentStore} />
223
+ <Component store={documentStore} />
224
+ <Component store={documentStore} />
225
+ <Component store={documentStore} />
226
+ <Component store={documentStore} />
227
+ <Component store={documentStore} />
228
+ <Component store={documentStore} />
229
+ <Component store={documentStore} />
230
+ <Component store={documentStore} />
231
+ <Component store={documentStore} />
232
+ <Component store={documentStore} />
233
+ <Component store={documentStore} />
234
+ <Component store={documentStore} />
235
+ <Component store={documentStore} testIdPrefix="check-" />
236
+ </>,
237
+ );
238
+
239
+ act(() => {
240
+ serverMock.mutateData({ hello: 'was invalidated' });
241
+ documentStore.invalidateData();
242
+ });
243
+
244
+ await sleep(300);
245
+
246
+ expect(getByTestId('check-data').textContent).toBe(
247
+ '{"hello":"was invalidated"}',
248
+ );
249
+
250
+ expect(serverMock.fetchsCount).toBe(1);
251
+ });
252
+
253
+ test('data selector', () => {
254
+ const { store: documentStore } = createDefaultDocumentStore({
255
+ useLoadedSnapshot: true,
256
+ });
257
+
258
+ const Comp = () => {
259
+ const data = documentStore.useDocument({
260
+ selector: (data) => data?.hello,
261
+ });
262
+
263
+ return <div data-testid="selector-data">{data.data}</div>;
264
+ };
265
+
266
+ const { getByTestId } = render(<Comp />);
267
+
268
+ expect(getByTestId('selector-data').textContent).toBe('world');
269
+ });
270
+
271
+ describe('disable', () => {
272
+ test('disable initial fetch', async () => {
273
+ const { serverMock, store: documentStore } = createDefaultDocumentStore();
274
+
275
+ const Comp = () => {
276
+ const data = documentStore.useDocument({
277
+ disabled: true,
278
+ });
279
+
280
+ return (
281
+ <div>
282
+ <div data-testid="data">{data.data?.hello}</div>
283
+ <div data-testid="status">{data.status}</div>
284
+ </div>
285
+ );
286
+ };
287
+
288
+ const { getByTestId } = render(<Comp />);
289
+
290
+ await sleep(200);
291
+
292
+ expect(getByTestId('data').textContent).toBe('');
293
+ expect(getByTestId('status').textContent).toBe('idle');
294
+ expect(serverMock.fetchsCount).toBe(0);
295
+ });
296
+
297
+ test('disable then enable fetch', async () => {
298
+ const { serverMock, store: documentStore } = createDefaultDocumentStore();
299
+
300
+ const renders: any[] = [];
301
+
302
+ const Comp = () => {
303
+ const [disabled, setDisabled] = useState(true);
304
+
305
+ const data = documentStore.useDocument({
306
+ disabled,
307
+ });
308
+
309
+ renders.push({ data: data.data, status: data.status, disabled });
310
+
311
+ return (
312
+ <div>
313
+ <div data-testid="data">{data.data?.hello}</div>
314
+ <div data-testid="status">{data.status}</div>
315
+ <button
316
+ data-testid="toggle-disabled"
317
+ onClick={() => setDisabled(!disabled)}
318
+ >
319
+ btn
320
+ </button>
321
+ </div>
322
+ );
323
+ };
324
+
325
+ const { getByTestId } = render(<Comp />);
326
+
327
+ await sleep(40);
328
+
329
+ expect(serverMock.fetchsCount).toBe(0);
330
+ expect(renders).toMatchInlineSnapshot(`
331
+ [
332
+ {
333
+ "data": null,
334
+ "disabled": true,
335
+ "status": "idle",
336
+ },
337
+ ]
338
+ `);
339
+
340
+ act(() => {
341
+ fireEvent.click(getByTestId('toggle-disabled'));
342
+ });
343
+
344
+ await sleep(100);
345
+
346
+ expect(serverMock.fetchsCount).toBe(1);
347
+ expect(renders).toMatchInlineSnapshot(`
348
+ [
349
+ {
350
+ "data": null,
351
+ "disabled": true,
352
+ "status": "idle",
353
+ },
354
+ {
355
+ "data": null,
356
+ "disabled": false,
357
+ "status": "loading",
358
+ },
359
+ {
360
+ "data": {
361
+ "hello": "world",
362
+ },
363
+ "disabled": false,
364
+ "status": "success",
365
+ },
366
+ ]
367
+ `);
368
+ });
369
+ });
370
+
371
+ test('disableRefetchOnMount', async () => {
372
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
373
+ useLoadedSnapshot: false,
374
+ });
375
+
376
+ const renders: any[] = [];
377
+ const comp2Renders: any[] = [];
378
+
379
+ const Comp2 = () => {
380
+ const data = documentStore.useDocument({
381
+ disableRefetchOnMount: true,
382
+ });
383
+
384
+ comp2Renders.push({ data: data.data, status: data.status });
385
+
386
+ return <div />;
387
+ };
388
+
389
+ const Comp = () => {
390
+ const [mountComp2, setMountComp2] = useState(false);
391
+
392
+ const data = documentStore.useDocument({
393
+ disableRefetchOnMount: true,
394
+ });
395
+
396
+ renders.push({ data: data.data, status: data.status });
397
+
398
+ useEffect(() => {
399
+ if (data.status === 'success') {
400
+ setMountComp2(true);
401
+ }
402
+ }, [data.status]);
403
+
404
+ return <>{mountComp2 && <Comp2 />}</>;
405
+ };
406
+
407
+ render(<Comp />);
408
+
409
+ await sleep(200);
410
+
411
+ // loads the initial data
412
+ expect(renders).toMatchInlineSnapshot(`
413
+ [
414
+ {
415
+ "data": null,
416
+ "status": "loading",
417
+ },
418
+ {
419
+ "data": {
420
+ "hello": "world",
421
+ },
422
+ "status": "success",
423
+ },
424
+ {
425
+ "data": {
426
+ "hello": "world",
427
+ },
428
+ "status": "success",
429
+ },
430
+ ]
431
+ `);
432
+
433
+ expect(serverMock.fetchsCount).toBe(1);
434
+
435
+ await sleep(200);
436
+
437
+ expect(comp2Renders).toMatchInlineSnapshot(`
438
+ [
439
+ {
440
+ "data": {
441
+ "hello": "world",
442
+ },
443
+ "status": "success",
444
+ },
445
+ ]
446
+ `);
447
+
448
+ // does not refetch on mount
449
+ expect(serverMock.fetchsCount).toBe(1);
450
+ });
451
+
452
+ test('do not return refetchin status by default', async () => {
453
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
454
+ useLoadedSnapshot: true,
455
+ });
456
+
457
+ const renders: any[] = [];
458
+
459
+ render(
460
+ <Component
461
+ store={documentStore}
462
+ onRender={({ data, status }) => {
463
+ renders.push([data.hello, status]);
464
+ }}
465
+ />,
466
+ );
467
+
468
+ act(() => {
469
+ serverMock.mutateData({ hello: 'was invalidated' });
470
+ documentStore.invalidateData();
471
+ });
472
+
473
+ await sleep(200);
474
+
475
+ expect(renders).toMatchInlineSnapshot(`
476
+ [
477
+ [
478
+ "world",
479
+ "success",
480
+ ],
481
+ [
482
+ "was invalidated",
483
+ "success",
484
+ ],
485
+ ]
486
+ `);
487
+ });
488
+
489
+ describe('action types', () => {
490
+ test('action with optmistic update', async () => {
491
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
492
+ useLoadedSnapshot: true,
493
+ });
494
+
495
+ const renders: any[] = [];
496
+
497
+ render(
498
+ <Component
499
+ store={documentStore}
500
+ onRender={({ data, status }) => {
501
+ renders.push([data.hello, status]);
502
+ }}
503
+ />,
504
+ );
505
+
506
+ act(() => {
507
+ actionWithOptimisticUpdate(serverMock, documentStore, 'was updated');
508
+ });
509
+
510
+ await sleep(150);
511
+
512
+ expect(renders).toMatchInlineSnapshot(`
513
+ [
514
+ [
515
+ "world",
516
+ "success",
517
+ ],
518
+ [
519
+ "was updated",
520
+ "success",
521
+ ],
522
+ ]
523
+ `);
524
+ });
525
+
526
+ test('action with optmistic update and revalidation', async () => {
527
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
528
+ useLoadedSnapshot: true,
529
+ });
530
+
531
+ const renders: any[] = [];
532
+
533
+ render(
534
+ <Component
535
+ returnRefetchingStatus
536
+ store={documentStore}
537
+ onRender={({ data, status }) => {
538
+ renders.push([data.hello, status]);
539
+ }}
540
+ />,
541
+ );
542
+
543
+ act(() => {
544
+ actionWithOptimisticUpdateAndRevalidation(
545
+ serverMock,
546
+ documentStore,
547
+ 'was updated',
548
+ );
549
+ });
550
+
551
+ await sleep(150);
552
+
553
+ expect(renders).toMatchInlineSnapshot(`
554
+ [
555
+ [
556
+ "world",
557
+ "success",
558
+ ],
559
+ [
560
+ "was updated",
561
+ "success",
562
+ ],
563
+ [
564
+ "was updated",
565
+ "refetching",
566
+ ],
567
+ [
568
+ "was updated",
569
+ "success",
570
+ ],
571
+ ]
572
+ `);
573
+ });
574
+
575
+ test('action without optimistic update', async () => {
576
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
577
+ useLoadedSnapshot: true,
578
+ });
579
+
580
+ const renders: any[] = [];
581
+
582
+ render(
583
+ <Component
584
+ store={documentStore}
585
+ onRender={({ data, status }) => {
586
+ renders.push([data.hello, status]);
587
+ }}
588
+ />,
589
+ );
590
+
591
+ act(() => {
592
+ actionWithoutOptimisticUpdate(serverMock, documentStore, 'was updated');
593
+ });
594
+
595
+ await sleep(150);
596
+
597
+ expect(renders).toMatchInlineSnapshot(`
598
+ [
599
+ [
600
+ "world",
601
+ "success",
602
+ ],
603
+ [
604
+ "was updated",
605
+ "success",
606
+ ],
607
+ ]
608
+ `);
609
+ });
610
+
611
+ test('action with revalition and without optimistic update', async () => {
612
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
613
+ useLoadedSnapshot: true,
614
+ });
615
+
616
+ const renders: any[] = [];
617
+
618
+ render(
619
+ <Component
620
+ store={documentStore}
621
+ returnRefetchingStatus
622
+ onRender={({ data, status }) => {
623
+ renders.push([data.hello, status]);
624
+ }}
625
+ />,
626
+ );
627
+
628
+ act(() => {
629
+ actionWithRevalidationAndWithoutOptimisticUpdate(
630
+ serverMock,
631
+ documentStore,
632
+ 'was updated',
633
+ );
634
+ });
635
+
636
+ await sleep(150);
637
+
638
+ expect(renders).toMatchInlineSnapshot(`
639
+ [
640
+ [
641
+ "world",
642
+ "success",
643
+ ],
644
+ [
645
+ "world",
646
+ "refetching",
647
+ ],
648
+ [
649
+ "was updated",
650
+ "success",
651
+ ],
652
+ ]
653
+ `);
654
+ });
655
+ });
656
+
657
+ test('rollback on error', async () => {
658
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
659
+ useLoadedSnapshot: true,
660
+ });
661
+
662
+ const renders: any[] = [];
663
+
664
+ render(
665
+ <Component
666
+ store={documentStore}
667
+ returnRefetchingStatus
668
+ onRender={({ data, status, error }) => {
669
+ renders.push([data.hello, status, error]);
670
+ }}
671
+ />,
672
+ );
673
+
674
+ act(() => {
675
+ actionWithOptimisticUpdate(
676
+ serverMock,
677
+ documentStore,
678
+ 'was updated',
679
+ 'error',
680
+ );
681
+ });
682
+
683
+ await sleep(200);
684
+
685
+ expect(renders).toMatchInlineSnapshot(`
686
+ [
687
+ [
688
+ "world",
689
+ "success",
690
+ null,
691
+ ],
692
+ [
693
+ "was updated",
694
+ "success",
695
+ null,
696
+ ],
697
+ [
698
+ "was updated",
699
+ "refetching",
700
+ null,
701
+ ],
702
+ [
703
+ "world",
704
+ "success",
705
+ null,
706
+ ],
707
+ ]
708
+ `);
709
+ });
710
+
711
+ describe('isolated tests', () => {
712
+ test('use ensureIsLoaded prop', async () => {
713
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
714
+ useLoadedSnapshot: false,
715
+ });
716
+
717
+ const renders = createRenderStore();
718
+
719
+ renderHook(() => {
720
+ const selectionResult = documentStore.useDocument({
721
+ ensureIsLoaded: true,
722
+ });
723
+
724
+ renders.add(
725
+ pick(selectionResult, ['status', 'isLoading', 'data'], {
726
+ isLoading: 'L',
727
+ }),
728
+ );
729
+ });
730
+
731
+ await serverMock.waitFetchIdle();
732
+
733
+ expect(renders.snapshot).toMatchInlineSnapshot(`
734
+ "
735
+ status: loading -- L: true -- data: null
736
+ status: loading -- L: true -- data: {hello:world}
737
+ status: success -- L: false -- data: {hello:world}
738
+ "
739
+ `);
740
+
741
+ renders.reset();
742
+
743
+ expect(documentStore.scheduleFetch('highPriority')).toBe('started');
744
+
745
+ await serverMock.waitFetchIdle();
746
+
747
+ // ignore refetching status
748
+ expect(renders.snapshot).toMatchInlineSnapshot(`
749
+ "
750
+ status: success -- L: false -- data: {hello:world}
751
+ "
752
+ `);
753
+ });
754
+
755
+ test('use ensureIsLoaded prop with disabled', async () => {
756
+ const { serverMock, store: documentStore } = createDefaultDocumentStore({
757
+ useLoadedSnapshot: false,
758
+ });
759
+
760
+ const renders = createRenderStore();
761
+
762
+ const disabled = createValueStore<boolean>(true);
763
+
764
+ renderHook(() => {
765
+ const res = documentStore.useDocument({
766
+ ensureIsLoaded: true,
767
+ disabled: disabled.useValue(),
768
+ });
769
+
770
+ renders.add(
771
+ pick(res, ['status', 'isLoading', 'data'], { isLoading: 'L' }),
772
+ );
773
+ });
774
+
775
+ expect(renders.snapshot).toMatchInlineSnapshot(`
776
+ "
777
+ status: idle -- L: false -- data: null
778
+ "
779
+ `);
780
+
781
+ // enable loading
782
+ disabled.set(false);
783
+
784
+ await serverMock.waitFetchIdle();
785
+
786
+ expect(renders.snapshot).toMatchInlineSnapshot(`
787
+ "
788
+ status: idle -- L: false -- data: null
789
+ ---
790
+ status: loading -- L: true -- data: null
791
+ status: loading -- L: true -- data: {hello:world}
792
+ status: success -- L: false -- data: {hello:world}
793
+ "
794
+ `);
795
+ });
796
+ });
797
+
798
+ test.concurrent('RTU update works', async () => {
799
+ const env = createTestEnv({
800
+ initialServerData: 'lucas',
801
+ useLoadedSnapshot: true,
802
+ emulateRTU: true,
803
+ dynamicRTUThrottleMs() {
804
+ return 300;
805
+ },
806
+ });
807
+
808
+ const renders = createRenderStore();
809
+
810
+ env.serverMock.produceData((draft) => {
811
+ draft.hello = 'RTU Update';
812
+ });
813
+
814
+ await sleep(100);
815
+
816
+ expect(env.store.store.state).toMatchSnapshotString(`
817
+ {
818
+ "data": {
819
+ "hello": "world",
820
+ },
821
+ "error": null,
822
+ "refetchOnMount": "realtimeUpdate",
823
+ "status": "success",
824
+ }
825
+ `);
826
+
827
+ renderHook(() => {
828
+ const { data, status } = env.store.useDocument({
829
+ returnRefetchingStatus: true,
830
+ disableRefetchOnMount: true,
831
+ });
832
+
833
+ renders.add({ status, data });
834
+ });
835
+
836
+ await env.serverMock.waitFetchIdle(0, 1500);
837
+
838
+ env.serverMock.produceData((draft) => {
839
+ draft.hello = 'Throttle update';
840
+ });
841
+
842
+ await env.serverMock.waitFetchIdle(0, 1500);
843
+
844
+ expect(renders.getSnapshot({ arrays: 'all' })).toMatchSnapshotString(`
845
+ "
846
+ status: success -- data: {hello:world}
847
+ status: refetching -- data: {hello:world}
848
+ status: success -- data: {hello:RTU Update}
849
+ status: refetching -- data: {hello:RTU Update}
850
+ status: success -- data: {hello:Throttle update}
851
+ "
852
+ `);
853
+
854
+ expect(
855
+ env.serverMock.fetchs[1]!.time.start - env.serverMock.fetchs[0]!.time.end,
856
+ ).toBeGreaterThanOrEqual(300);
857
+ });