forked from Expensify/react-native-onyx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathonyxUtilsTest.ts
More file actions
1366 lines (1119 loc) · 59.5 KB
/
Copy pathonyxUtilsTest.ts
File metadata and controls
1366 lines (1119 loc) · 59.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {act} from '@testing-library/react-native';
import Onyx from '../../lib';
import OnyxUtils from '../../lib/OnyxUtils';
import type {GenericDeepRecord} from '../types';
import utils from '../../lib/utils';
import type {Collection, OnyxCollection} from '../../lib/types';
import type GenericCollection from '../utils/GenericCollection';
import OnyxCache from '../../lib/OnyxCache';
import * as Logger from '../../lib/Logger';
import StorageMock from '../../lib/storage';
import createDeferredTask from '../../lib/createDeferredTask';
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
const testObject: GenericDeepRecord = {
a: 'a',
b: {
c: 'c',
d: {
e: 'e',
f: 'f',
},
g: 'g',
},
};
const testMergeChanges: GenericDeepRecord[] = [
{
b: {
d: {
h: 'h',
},
},
},
{
b: {
// Removing "d" object.
d: null,
h: 'h',
},
},
{
b: {
// Adding back "d" property with a object.
// The "ONYX_INTERNALS__REPLACE_OBJECT_MARK" marker property should be added here when batching merge changes.
d: {
i: 'i',
},
},
},
{
b: {
// Removing "d" object again.
d: null,
// Removing "g" object.
g: null,
},
},
{
b: {
// Adding back "d" property with a object.
// The "ONYX_INTERNALS__REPLACE_OBJECT_MARK" marker property should be added here when batching merge changes.
d: {
i: 'i',
j: 'j',
},
// Adding back "g" property with a object.
// The "ONYX_INTERNALS__REPLACE_OBJECT_MARK" marker property should be added here when batching merge changes.
g: {
k: 'k',
},
},
},
];
const ONYXKEYS = {
TEST_KEY: 'test',
TEST_KEY_2: 'test2',
COLLECTION: {
TEST_KEY: 'test_',
TEST_LEVEL_KEY: 'test_level_',
TEST_LEVEL_LAST_KEY: 'test_level_last_',
ROUTES: 'routes_',
RAM_ONLY_COLLECTION: 'ramOnlyCollection_',
},
RAM_ONLY_KEY: 'ramOnlyKey',
};
describe('OnyxUtils', () => {
beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
ramOnlyKeys: [ONYXKEYS.RAM_ONLY_KEY, ONYXKEYS.COLLECTION.RAM_ONLY_COLLECTION],
}),
);
beforeEach(() => Onyx.clear());
afterEach(() => jest.clearAllMocks());
describe('partialSetCollection', () => {
beforeEach(() => {
Onyx.clear();
});
afterEach(() => {
Onyx.clear();
});
it('should replace all existing collection members with new values and keep old ones intact', async () => {
let result: OnyxCollection<unknown>;
const routeA = `${ONYXKEYS.COLLECTION.ROUTES}A`;
const routeB = `${ONYXKEYS.COLLECTION.ROUTES}B`;
const routeB1 = `${ONYXKEYS.COLLECTION.ROUTES}B1`;
const routeC = `${ONYXKEYS.COLLECTION.ROUTES}C`;
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.ROUTES,
callback: (value) => (result = value),
waitForCollectionCallback: true,
});
// Set initial collection state
await Onyx.setCollection(ONYXKEYS.COLLECTION.ROUTES, {
[routeA]: {name: 'Route A'},
[routeB1]: {name: 'Route B1'},
[routeC]: {name: 'Route C'},
} as GenericCollection);
// Replace with new collection data
await OnyxUtils.partialSetCollection({
collectionKey: ONYXKEYS.COLLECTION.ROUTES,
collection: {
[routeA]: {name: 'New Route A'},
[routeB]: {name: 'New Route B'},
[routeC]: {name: 'New Route C'},
} as GenericCollection,
});
expect(result).toEqual({
[routeA]: {name: 'New Route A'},
[routeB]: {name: 'New Route B'},
[routeB1]: {name: 'Route B1'},
[routeC]: {name: 'New Route C'},
});
await Onyx.disconnect(connection);
});
it('should not replace anything in the collection with empty values', async () => {
let result: OnyxCollection<unknown>;
const routeA = `${ONYXKEYS.COLLECTION.ROUTES}A`;
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.ROUTES,
callback: (value) => (result = value),
waitForCollectionCallback: true,
});
await Onyx.mergeCollection(ONYXKEYS.COLLECTION.ROUTES, {
[routeA]: {name: 'Route A'},
} as GenericCollection);
await OnyxUtils.partialSetCollection({collectionKey: ONYXKEYS.COLLECTION.ROUTES, collection: {} as GenericCollection});
expect(result).toEqual({
[routeA]: {name: 'Route A'},
});
await Onyx.disconnect(connection);
});
it('should reject collection items with invalid keys', async () => {
let result: OnyxCollection<unknown>;
const routeA = `${ONYXKEYS.COLLECTION.ROUTES}A`;
const invalidRoute = 'invalid_route';
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.ROUTES,
callback: (value) => (result = value),
waitForCollectionCallback: true,
});
await Onyx.mergeCollection(ONYXKEYS.COLLECTION.ROUTES, {
[routeA]: {name: 'Route A'},
} as GenericCollection);
await OnyxUtils.partialSetCollection({
collectionKey: ONYXKEYS.COLLECTION.ROUTES,
collection: {
[invalidRoute]: {name: 'Invalid Route'},
} as GenericCollection,
});
expect(result).toEqual({
[routeA]: {name: 'Route A'},
});
await Onyx.disconnect(connection);
});
});
describe('multiSetWithRetry', () => {
it('should fire collection-level callback only once per collection even with multiple members', async () => {
const collectionCallback = jest.fn();
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TEST_KEY,
callback: collectionCallback,
waitForCollectionCallback: true,
});
await waitForPromisesToResolve();
collectionCallback.mockClear();
// multiSet with 3 members of the same collection
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]: {id: 1},
[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]: {id: 2},
[`${ONYXKEYS.COLLECTION.TEST_KEY}3`]: {id: 3},
});
// Should be called only ONCE with the batched collection (not 3 times)
expect(collectionCallback).toHaveBeenCalledTimes(1);
const [collection] = collectionCallback.mock.calls[0];
expect(collection[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]).toEqual({id: 1});
expect(collection[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]).toEqual({id: 2});
expect(collection[`${ONYXKEYS.COLLECTION.TEST_KEY}3`]).toEqual({id: 3});
Onyx.disconnect(connection);
});
it('should fire individual member-key subscribers once per key', async () => {
const spy1 = jest.fn();
const spy2 = jest.fn();
const spy3 = jest.fn();
const conn1 = Onyx.connect({key: `${ONYXKEYS.COLLECTION.TEST_KEY}1`, callback: spy1});
const conn2 = Onyx.connect({key: `${ONYXKEYS.COLLECTION.TEST_KEY}2`, callback: spy2});
const conn3 = Onyx.connect({key: `${ONYXKEYS.COLLECTION.TEST_KEY}3`, callback: spy3});
await waitForPromisesToResolve();
spy1.mockClear();
spy2.mockClear();
spy3.mockClear();
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]: {id: 1},
[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]: {id: 2},
[`${ONYXKEYS.COLLECTION.TEST_KEY}3`]: {id: 3},
});
expect(spy1).toHaveBeenCalledTimes(1);
expect(spy1).toHaveBeenCalledWith({id: 1}, `${ONYXKEYS.COLLECTION.TEST_KEY}1`);
expect(spy2).toHaveBeenCalledTimes(1);
expect(spy2).toHaveBeenCalledWith({id: 2}, `${ONYXKEYS.COLLECTION.TEST_KEY}2`);
expect(spy3).toHaveBeenCalledTimes(1);
expect(spy3).toHaveBeenCalledWith({id: 3}, `${ONYXKEYS.COLLECTION.TEST_KEY}3`);
Onyx.disconnect(conn1);
Onyx.disconnect(conn2);
Onyx.disconnect(conn3);
});
it('should notify non-collection keys individually alongside batched collection updates', async () => {
const collectionCallback = jest.fn();
const singleKeyCallback = jest.fn();
const connCollection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TEST_KEY,
callback: collectionCallback,
waitForCollectionCallback: true,
});
const connSingle = Onyx.connect({
key: ONYXKEYS.TEST_KEY,
callback: singleKeyCallback,
});
await waitForPromisesToResolve();
collectionCallback.mockClear();
singleKeyCallback.mockClear();
// Mix of collection members and a non-collection key
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]: {id: 1},
[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]: {id: 2},
[ONYXKEYS.TEST_KEY]: 'standalone',
});
// Collection callback fires once (batched)
expect(collectionCallback).toHaveBeenCalledTimes(1);
// Non-collection key callback fires once
expect(singleKeyCallback).toHaveBeenCalledTimes(1);
expect(singleKeyCallback).toHaveBeenCalledWith('standalone', ONYXKEYS.TEST_KEY);
Onyx.disconnect(connCollection);
Onyx.disconnect(connSingle);
});
it('should batch notifications per-collection when members span multiple collections', async () => {
const testCallback = jest.fn();
const routesCallback = jest.fn();
const connTest = Onyx.connect({
key: ONYXKEYS.COLLECTION.TEST_KEY,
callback: testCallback,
waitForCollectionCallback: true,
});
const connRoutes = Onyx.connect({
key: ONYXKEYS.COLLECTION.ROUTES,
callback: routesCallback,
waitForCollectionCallback: true,
});
await waitForPromisesToResolve();
testCallback.mockClear();
routesCallback.mockClear();
// multiSet with members of two different collections
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]: {id: 1},
[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]: {id: 2},
[`${ONYXKEYS.COLLECTION.ROUTES}A`]: {name: 'A'},
[`${ONYXKEYS.COLLECTION.ROUTES}B`]: {name: 'B'},
});
// Each collection callback fires once
expect(testCallback).toHaveBeenCalledTimes(1);
expect(routesCallback).toHaveBeenCalledTimes(1);
Onyx.disconnect(connTest);
Onyx.disconnect(connRoutes);
});
it('should pass previous values to keysChanged so unchanged members skip notification', async () => {
// Set initial data
const initial1 = {id: 1, name: 'A'};
const initial2 = {id: 2, name: 'B'};
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]: initial1,
[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]: initial2,
});
const spy1 = jest.fn();
const spy2 = jest.fn();
const conn1 = Onyx.connect({key: `${ONYXKEYS.COLLECTION.TEST_KEY}1`, callback: spy1});
const conn2 = Onyx.connect({key: `${ONYXKEYS.COLLECTION.TEST_KEY}2`, callback: spy2});
await waitForPromisesToResolve();
spy1.mockClear();
spy2.mockClear();
// multiSet: change key 1, keep key 2 with same content (but new reference)
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]: {id: 1, name: 'A-updated'},
[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]: initial2,
});
// Key 1 subscriber fires (value changed)
expect(spy1).toHaveBeenCalledTimes(1);
expect(spy1).toHaveBeenCalledWith({id: 1, name: 'A-updated'}, `${ONYXKEYS.COLLECTION.TEST_KEY}1`);
// Key 2 keeps the same reference (passed as-is in multiSet) — subscriber should not fire
// because keysChanged sees the same reference as previousCollection[key]
expect(spy2).not.toHaveBeenCalled();
Onyx.disconnect(conn1);
Onyx.disconnect(conn2);
});
it('should stop firing callbacks for a collection subscriber that disconnects itself mid-batch', async () => {
// A collection subscriber (waitForCollectionCallback=false) disconnects itself when
// it receives the first member. Subsequent changed members in the same batch must NOT
// trigger further callbacks for this subscriber.
const callback = jest.fn();
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TEST_KEY,
callback,
waitForCollectionCallback: false,
});
await waitForPromisesToResolve();
callback.mockReset();
callback.mockImplementation(() => {
Onyx.disconnect(connection);
});
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.TEST_KEY}1`]: {id: 1},
[`${ONYXKEYS.COLLECTION.TEST_KEY}2`]: {id: 2},
[`${ONYXKEYS.COLLECTION.TEST_KEY}3`]: {id: 3},
});
// Despite 3 changed members, callback should fire at most once before disconnect stops it
expect(callback).toHaveBeenCalledTimes(1);
});
it('should keep cache and subscriber state consistent when a non-collection callback writes to another payload key', async () => {
// A subscriber for keyA synchronously calls Onyx.set() on keyB during its callback.
// After multiSet completes, the cache must reflect the multiSet's value for keyB
// (multiSet wins), and the keyB subscriber's last seen value must equal the cache.
await Onyx.multiSet({[ONYXKEYS.TEST_KEY]: 'initialA', [ONYXKEYS.TEST_KEY_2]: 'initialB'});
const callbackA = jest.fn((value: unknown) => {
if (value !== 'newA') {
return;
}
// While processing the new value of keyA, write to keyB.
// keyB is later in the same multiSet payload — multiSet should win.
Onyx.set(ONYXKEYS.TEST_KEY_2, 'callbackB');
});
const callbackB = jest.fn();
const connA = Onyx.connect({
key: ONYXKEYS.TEST_KEY,
callback: callbackA,
});
const connB = Onyx.connect({
key: ONYXKEYS.TEST_KEY_2,
callback: callbackB,
});
await waitForPromisesToResolve();
callbackA.mockClear();
callbackB.mockClear();
await Onyx.multiSet({
[ONYXKEYS.TEST_KEY]: 'newA',
[ONYXKEYS.TEST_KEY_2]: 'multiSetB',
});
// Cache reflects multiSet's payload value for keyB (the multiSet's later cache.set wins)
expect(OnyxCache.get(ONYXKEYS.TEST_KEY_2)).toBe('multiSetB');
expect(callbackB.mock.calls.length).toBe(2);
expect(callbackB.mock.calls.at(0)?.[0]).toBe('callbackB');
// keyB subscriber's last received value matches the cache (no stale callback)
expect(callbackB.mock.calls.at(1)?.[0]).toBe('multiSetB');
Onyx.disconnect(connA);
Onyx.disconnect(connB);
});
});
describe('keysChanged', () => {
beforeEach(() => {
Onyx.clear();
});
afterEach(() => {
Onyx.clear();
});
it('should call callback when data actually changes for collection member key subscribers', async () => {
const callbackSpy = jest.fn();
const entryKey = `${ONYXKEYS.COLLECTION.TEST_KEY}123`;
const connection = Onyx.connect({
key: entryKey,
callback: callbackSpy,
});
const entryData = {value: 'updated_data'};
// Create partial collection data that includes our member key
const collection = {
[entryKey]: entryData,
} as Collection<string, {value: string}>;
// Clear the callback spy to focus on the keysChanged behavior
callbackSpy.mockClear();
await Onyx.setCollection(ONYXKEYS.COLLECTION.TEST_KEY, collection);
// Verify the subscriber callback was called
expect(callbackSpy).toHaveBeenCalledTimes(1);
expect(callbackSpy).toHaveBeenCalledWith(entryData, entryKey);
await Onyx.disconnect(connection);
});
it('should set lastConnectionCallbackData for collection member key subscribers', async () => {
const entryKey = `${ONYXKEYS.COLLECTION.TEST_KEY}456`;
const initialEntryData = {value: 'initial_data'};
const updatedEntryData = {value: 'updated_data'};
const newEntryData = {value: 'new_data'};
const callbackSpy = jest.fn();
const connection = await Onyx.connect({
key: entryKey,
callback: callbackSpy,
});
// Create partial collection data that includes our member key
const initialCollection = {
[entryKey]: initialEntryData,
} as Collection<string, {value: string}>;
// Clear the callback spy to focus on the keysChanged behavior
callbackSpy.mockClear();
OnyxUtils.keysChanged(
ONYXKEYS.COLLECTION.TEST_KEY,
{[entryKey]: updatedEntryData}, // new collection
initialCollection, // previous collection
);
// Should be called again because data changed
expect(callbackSpy).toHaveBeenCalledTimes(1);
expect(callbackSpy).toHaveBeenCalledWith(undefined, entryKey);
// Clear the callback spy to focus on the keyChanged behavior
callbackSpy.mockClear();
OnyxUtils.keyChanged(
entryKey,
newEntryData, // Second update with different data
() => true, // notify connect subscribers
);
// Should be called again because data changed
expect(callbackSpy).toHaveBeenCalledTimes(1);
expect(callbackSpy).toHaveBeenCalledWith(newEntryData, entryKey);
await Onyx.disconnect(connection);
});
it('should notify collection-level subscribers with waitForCollectionCallback', async () => {
const entryKey = `${ONYXKEYS.COLLECTION.TEST_KEY}789`;
const entryData = {value: 'data'};
const collectionCallback = jest.fn();
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TEST_KEY,
callback: collectionCallback,
waitForCollectionCallback: true,
});
await Onyx.set(entryKey, entryData);
collectionCallback.mockClear();
// Trigger keysChanged directly with a partial collection
OnyxUtils.keysChanged(ONYXKEYS.COLLECTION.TEST_KEY, {[entryKey]: entryData}, {});
expect(collectionCallback).toHaveBeenCalledTimes(1);
// Collection subscriber receives the full cached collection, subscriber.key, and partial
const [receivedCollection, receivedKey, receivedPartial] = collectionCallback.mock.calls[0];
expect(receivedKey).toBe(ONYXKEYS.COLLECTION.TEST_KEY);
expect(receivedCollection[entryKey]).toEqual(entryData);
expect(receivedPartial).toEqual({[entryKey]: entryData});
Onyx.disconnect(connection);
});
it('should skip notification when member value has same reference in previous and current collection', async () => {
const entryKey = `${ONYXKEYS.COLLECTION.TEST_KEY}same`;
const sameValue = {value: 'unchanged'};
await Onyx.set(entryKey, sameValue);
const callbackSpy = jest.fn();
const connection = Onyx.connect({
key: entryKey,
callback: callbackSpy,
});
await waitForPromisesToResolve();
callbackSpy.mockClear();
// Simulate keysChanged where the previous and current value are the SAME reference
// (which happens with frozen snapshots when nothing changed). === should skip notification.
OnyxUtils.keysChanged(ONYXKEYS.COLLECTION.TEST_KEY, {[entryKey]: sameValue}, {[entryKey]: sameValue});
expect(callbackSpy).not.toHaveBeenCalled();
Onyx.disconnect(connection);
});
it('should notify member subscribers only for changed keys in a batched update', async () => {
const keyA = `${ONYXKEYS.COLLECTION.TEST_KEY}A`;
const keyB = `${ONYXKEYS.COLLECTION.TEST_KEY}B`;
const keyC = `${ONYXKEYS.COLLECTION.TEST_KEY}C`;
const dataA = {value: 'A'};
const dataB = {value: 'B'};
const dataC = {value: 'C'};
await Onyx.multiSet({[keyA]: dataA, [keyB]: dataB, [keyC]: dataC});
const spyA = jest.fn();
const spyB = jest.fn();
const spyC = jest.fn();
const connA = Onyx.connect({key: keyA, callback: spyA});
const connB = Onyx.connect({key: keyB, callback: spyB});
const connC = Onyx.connect({key: keyC, callback: spyC});
await waitForPromisesToResolve();
spyA.mockClear();
spyB.mockClear();
spyC.mockClear();
// Update cache so keysChanged reads the new values via getCachedCollection
const newA = {value: 'A-updated'};
const newC = {value: 'C-updated'};
OnyxCache.set(keyA, newA);
OnyxCache.set(keyC, newC);
// keyB stays the same reference
OnyxUtils.keysChanged(ONYXKEYS.COLLECTION.TEST_KEY, {[keyA]: newA, [keyB]: dataB, [keyC]: newC}, {[keyA]: dataA, [keyB]: dataB, [keyC]: dataC});
expect(spyA).toHaveBeenCalledTimes(1);
expect(spyB).not.toHaveBeenCalled();
expect(spyC).toHaveBeenCalledTimes(1);
Onyx.disconnect(connA);
Onyx.disconnect(connB);
Onyx.disconnect(connC);
});
it('should catch errors thrown by subscriber callbacks and continue notifying others', async () => {
const entryKey = `${ONYXKEYS.COLLECTION.TEST_KEY}errorTest`;
const entryData = {value: 'data'};
await Onyx.set(entryKey, entryData);
const failingCallback = jest.fn();
const workingCallback = jest.fn();
const connFailing = Onyx.connect({key: entryKey, callback: failingCallback, reuseConnection: false});
const connWorking = Onyx.connect({key: entryKey, callback: workingCallback, reuseConnection: false});
await waitForPromisesToResolve();
failingCallback.mockReset();
failingCallback.mockImplementation(() => {
throw new Error('subscriber failure');
});
workingCallback.mockClear();
// Spy on Logger to verify the error is logged
const logSpy = jest.spyOn(Logger, 'logAlert').mockImplementation(() => undefined);
const newData = {value: 'new'};
// Update the cache so keysChanged sees the new value as different from previous
OnyxCache.set(entryKey, newData);
OnyxUtils.keysChanged(ONYXKEYS.COLLECTION.TEST_KEY, {[entryKey]: newData}, {[entryKey]: entryData});
// Both callbacks should have been attempted; error should be logged
expect(failingCallback).toHaveBeenCalled();
expect(workingCallback).toHaveBeenCalled();
expect(logSpy).toHaveBeenCalled();
logSpy.mockRestore();
Onyx.disconnect(connFailing);
Onyx.disconnect(connWorking);
});
});
describe('mergeChanges', () => {
it("should return the last change if it's an array", () => {
const {result} = OnyxUtils.mergeChanges([...testMergeChanges, [0, 1, 2]], testObject);
expect(result).toEqual([0, 1, 2]);
});
it("should return the last change if the changes aren't objects", () => {
const {result} = OnyxUtils.mergeChanges(['a', 0, 'b', 1], testObject);
expect(result).toEqual(1);
});
it('should merge data correctly when applying batched changes', () => {
const batchedChanges: GenericDeepRecord = {
b: {
d: {
i: 'i',
j: 'j',
[utils.ONYX_INTERNALS__REPLACE_OBJECT_MARK]: true,
},
h: 'h',
g: {
[utils.ONYX_INTERNALS__REPLACE_OBJECT_MARK]: true,
k: 'k',
},
},
};
const {result} = OnyxUtils.mergeChanges([batchedChanges], testObject);
expect(result).toEqual({
a: 'a',
b: {
c: 'c',
d: {
i: 'i',
j: 'j',
},
h: 'h',
g: {
k: 'k',
},
},
});
});
});
describe('mergeAndMarkChanges', () => {
it('should apply the replacement markers if we have properties with objects being removed and added back during the changes', () => {
const {result, replaceNullPatches} = OnyxUtils.mergeAndMarkChanges(testMergeChanges);
expect(result).toEqual({
b: {
d: {
i: 'i',
j: 'j',
[utils.ONYX_INTERNALS__REPLACE_OBJECT_MARK]: true,
},
h: 'h',
g: {
[utils.ONYX_INTERNALS__REPLACE_OBJECT_MARK]: true,
k: 'k',
},
},
});
expect(replaceNullPatches).toEqual([
[['b', 'd'], {i: 'i'}],
[['b', 'd'], {i: 'i', j: 'j'}],
[['b', 'g'], {k: 'k'}],
]);
});
});
describe('retryOperation', () => {
const retryOperationSpy = jest.spyOn(OnyxUtils, 'retryOperation');
const genericError = new Error('Generic storage error');
const invalidDataError = new Error("Failed to execute 'put' on 'IDBObjectStore': invalid data");
const diskFullError = new Error('database or disk is full');
const nonRetriableIdbError = Object.assign(new Error('Internal error opening backing store for indexedDB.open.'), {name: 'UnknownError'});
it('should retry only one time if the operation is firstly failed and then passed', async () => {
StorageMock.setItem = jest.fn(StorageMock.setItem).mockRejectedValueOnce(genericError).mockImplementation(StorageMock.setItem);
await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});
// Should be called once, since Storage.setItem if failed only once
expect(retryOperationSpy).toHaveBeenCalledTimes(1);
});
it('should stop retrying after MAX_STORAGE_OPERATION_RETRY_ATTEMPTS retries for failing operation', async () => {
StorageMock.setItem = jest.fn().mockRejectedValue(genericError);
await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});
// Should be called 6 times: initial attempt + 5 retries (MAX_STORAGE_OPERATION_RETRY_ATTEMPTS)
expect(retryOperationSpy).toHaveBeenCalledTimes(6);
});
it("should throw error for if operation failed with \"Failed to execute 'put' on 'IDBObjectStore': invalid data\" error", async () => {
StorageMock.setItem = jest.fn().mockRejectedValueOnce(invalidDataError);
await expect(Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'})).rejects.toThrow(invalidDataError);
});
it('should not retry in case of storage capacity error and no keys to evict', async () => {
StorageMock.setItem = jest.fn().mockRejectedValue(diskFullError);
await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});
// Should only be called once since there are no evictable keys
expect(retryOperationSpy).toHaveBeenCalledTimes(1);
});
it('should not retry for non-retriable IndexedDB backing-store errors', async () => {
StorageMock.setItem = jest.fn().mockRejectedValue(nonRetriableIdbError);
await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});
// Called once (initial attempt only) -- no recursion, unlike the 6 calls for generic errors
expect(retryOperationSpy).toHaveBeenCalledTimes(1);
});
it('should log a single skip alert for non-retriable errors', async () => {
const logAlertSpy = jest.spyOn(Logger, 'logAlert');
StorageMock.setItem = jest.fn().mockRejectedValue(nonRetriableIdbError);
await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});
expect(logAlertSpy).toHaveBeenCalledWith(`Storage operation skipped retry for non-retriable error. Error: ${nonRetriableIdbError}. onyxMethod: setWithRetry.`);
// Not paired with the "5 retries exhausted" alert
expect(logAlertSpy).toHaveBeenCalledTimes(1);
});
it('should include the error in logAlert for IDBObjectStore invalid data errors', async () => {
const logAlertSpy = jest.spyOn(Logger, 'logAlert');
StorageMock.setItem = jest.fn().mockRejectedValueOnce(invalidDataError);
await expect(Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'})).rejects.toThrow(invalidDataError);
expect(logAlertSpy).toHaveBeenCalledWith(`Attempted to set invalid data set in Onyx. Please ensure all data is serializable. Error: ${invalidDataError}`);
});
it('should include the error in logs when out of storage with no evictable keys', async () => {
const logAlertSpy = jest.spyOn(Logger, 'logAlert');
const logInfoSpy = jest.spyOn(Logger, 'logInfo');
StorageMock.setItem = jest.fn().mockRejectedValue(diskFullError);
await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});
expect(logAlertSpy).toHaveBeenCalledWith(`Out of storage. But found no acceptable keys to remove. Error: ${diskFullError}`);
expect(logInfoSpy).toHaveBeenCalledWith(`Storage Quota Check -- bytesUsed: 0 bytesRemaining: Infinity. Original error: ${diskFullError}`);
});
it('should include the error in logAlert when out of storage and getDatabaseSize fails', async () => {
const dbSizeError = new Error('Failed to estimate storage');
const logAlertSpy = jest.spyOn(Logger, 'logAlert');
StorageMock.setItem = jest.fn().mockRejectedValue(diskFullError);
StorageMock.getDatabaseSize = jest.fn().mockRejectedValue(dbSizeError);
await Onyx.set(ONYXKEYS.TEST_KEY, {test: 'data'});
expect(logAlertSpy).toHaveBeenCalledWith(`Unable to get database size. getDatabaseSize error: ${dbSizeError}. Original error: ${diskFullError}`);
});
it('should not re-add an evicted key to recentlyAccessedKeys after removal', async () => {
// Re-init with evictable keys so getKeyForEviction() has something to return
Object.assign(OnyxUtils.getDeferredInitTask(), createDeferredTask());
Onyx.init({
keys: ONYXKEYS,
evictableKeys: [ONYXKEYS.COLLECTION.TEST_KEY],
});
await waitForPromisesToResolve();
const evictableKey = `${ONYXKEYS.COLLECTION.TEST_KEY}1`;
await Onyx.set(evictableKey, {id: 1});
expect(OnyxCache.getKeyForEviction()).toBe(evictableKey);
await OnyxUtils.remove(evictableKey);
expect(OnyxCache.getKeyForEviction()).toBeUndefined();
});
});
describe('mergeCollection cache-first ordering', () => {
// Save originals so we can restore them after each test. The tests below replace
// StorageMock.multiMerge / StorageMock.multiSet with rejecting mocks; without
// restoring, the mock leaks into later describe blocks (e.g. eviction tests) whose
// setup relies on these storage methods working normally.
const originalMultiMerge = StorageMock.multiMerge;
const originalMultiSet = StorageMock.multiSet;
afterEach(() => {
StorageMock.multiMerge = originalMultiMerge;
StorageMock.multiSet = originalMultiSet;
});
it('updates cache and notifies subscribers even when Storage.multiMerge rejects', async () => {
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
const existingMemberKey = `${collectionKey}1`;
const newMemberKey = `${collectionKey}2`;
// Seed an existing member so the merge path exercises multiMerge (existing) + multiSet (new)
await Onyx.set(existingMemberKey, {value: 'initial'});
const collectionCallback = jest.fn();
Onyx.connect({
key: collectionKey,
waitForCollectionCallback: true,
callback: collectionCallback,
});
await waitForPromisesToResolve();
collectionCallback.mockClear();
// Force Storage.multiMerge to reject with a non-retriable IDB error so the failure
// path is taken without burning the full retry budget and without rejecting the
// outer Onyx.mergeCollection promise.
const nonRetriableIdbError = Object.assign(new Error('Internal error opening backing store for indexedDB.open.'), {name: 'UnknownError'});
StorageMock.multiMerge = jest.fn().mockRejectedValue(nonRetriableIdbError);
await Onyx.mergeCollection(collectionKey, {
[existingMemberKey]: {value: 'merged'},
[newMemberKey]: {value: 'new'},
} as GenericCollection);
// Cache must reflect the merge regardless of the multiMerge rejection. This is the
// cache-first / storage-second invariant that mergeCollectionWithPatches must honor.
const cachedCollection = OnyxCache.getCollectionData(collectionKey);
expect(cachedCollection?.[existingMemberKey]).toEqual({value: 'merged'});
expect(cachedCollection?.[newMemberKey]).toEqual({value: 'new'});
// Subscribers must have been notified with the merged values.
expect(collectionCallback).toHaveBeenCalled();
const lastBroadcast = collectionCallback.mock.calls.at(-1)?.[0] as Record<string, unknown> | undefined;
expect(lastBroadcast?.[existingMemberKey]).toEqual({value: 'merged'});
expect(lastBroadcast?.[newMemberKey]).toEqual({value: 'new'});
});
it('updates cache and notifies subscribers even when Storage.multiSet rejects', async () => {
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
const newMemberKey1 = `${collectionKey}1`;
const newMemberKey2 = `${collectionKey}2`;
// No keys are seeded, so every merged key is a "new" key. This forces the merge path
// to use Storage.multiSet (existing keys would go through Storage.multiMerge).
const collectionCallback = jest.fn();
Onyx.connect({
key: collectionKey,
waitForCollectionCallback: true,
callback: collectionCallback,
});
await waitForPromisesToResolve();
collectionCallback.mockClear();
// Force Storage.multiSet to reject with a non-retriable IDB error so the failure
// path is taken without burning the full retry budget and without rejecting the
// outer Onyx.mergeCollection promise.
const nonRetriableIdbError = Object.assign(new Error('Internal error opening backing store for indexedDB.open.'), {name: 'UnknownError'});
StorageMock.multiSet = jest.fn().mockRejectedValue(nonRetriableIdbError);
await Onyx.mergeCollection(collectionKey, {
[newMemberKey1]: {value: 'first'},
[newMemberKey2]: {value: 'second'},
} as GenericCollection);
// Cache must reflect the merge regardless of the multiSet rejection. This is the
// cache-first / storage-second invariant that mergeCollectionWithPatches must honor.
const cachedCollection = OnyxCache.getCollectionData(collectionKey);
expect(cachedCollection?.[newMemberKey1]).toEqual({value: 'first'});
expect(cachedCollection?.[newMemberKey2]).toEqual({value: 'second'});
// Subscribers must have been notified with the merged values.
expect(collectionCallback).toHaveBeenCalled();
const lastBroadcast = collectionCallback.mock.calls.at(-1)?.[0] as Record<string, unknown> | undefined;
expect(lastBroadcast?.[newMemberKey1]).toEqual({value: 'first'});
expect(lastBroadcast?.[newMemberKey2]).toEqual({value: 'second'});
});
});
describe('mergeCollection pre-warm', () => {
// The retryOperation describe block above mutates StorageMock.setItem (and never restores it),
// so by the time we run, setItem may be a rejecting mock from a prior test. Capture pristine
// references at file-load time and restore them in beforeEach so our seeding via Onyx.set
// actually reaches the in-memory storage provider.
const pristineSetItem = StorageMock.setItem;
const pristineMultiSet = StorageMock.multiSet;
const pristineMultiGet = StorageMock.multiGet;
const pristineGetItem = StorageMock.getItem;
const pristineMultiMerge = StorageMock.multiMerge;
beforeEach(() => {
StorageMock.setItem = pristineSetItem;
StorageMock.multiSet = pristineMultiSet;
StorageMock.multiGet = pristineMultiGet;
StorageMock.getItem = pristineGetItem;
StorageMock.multiMerge = pristineMultiMerge;
});
// Make a key "cold" — value evicted from cache but the key is still tracked as persisted.
// OnyxCache.drop also removes the key from `storageKeys`, which would cause getAllKeys() to
// miss it (unless the entire storageKeys set is empty, in which case the function falls back
// to Storage.getAllKeys). To reliably hit the cold-but-persisted state regardless of how many
// other keys remain in cache, we re-register the key as known after dropping its value.
const evictFromCache = (...keys: string[]) => {
for (const key of keys) {
OnyxCache.drop(key);
OnyxCache.addKey(key);
}
};
it('fast path: skips storage reads entirely when every existing key is warm in cache', async () => {
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
const existingKey1 = `${collectionKey}1`;
const existingKey2 = `${collectionKey}2`;
// Seed both members so they are both warm in cache and present in storage.
await Onyx.set(existingKey1, {value: 'initial-1'});
await Onyx.set(existingKey2, {value: 'initial-2'});
const multiGetSpy = jest.spyOn(StorageMock, 'multiGet');
const getItemSpy = jest.spyOn(StorageMock, 'getItem');
await Onyx.mergeCollection(collectionKey, {
[existingKey1]: {value: 'merged-1'},
[existingKey2]: {value: 'merged-2'},
} as GenericCollection);
// With every existingKey warm, the diff swaps Promise.all(get) for Promise.resolve(),
// so no storage reads should happen during the pre-warm.
expect(multiGetSpy).not.toHaveBeenCalled();
expect(getItemSpy).not.toHaveBeenCalled();
// Cache still reflects the merge.
const cached = OnyxCache.getCollectionData(collectionKey);
expect(cached?.[existingKey1]).toEqual({value: 'merged-1'});
expect(cached?.[existingKey2]).toEqual({value: 'merged-2'});
});
it('slow path: batches cold existing keys into a single Storage.multiGet, with no individual getItem calls', async () => {
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
const coldKey1 = `${collectionKey}1`;
const coldKey2 = `${collectionKey}2`;
const warmKey = `${collectionKey}3`;
// Seed all three in storage, then evict two from cache so they are cold-but-persisted.
await Onyx.set(coldKey1, {value: 'persisted-1'});
await Onyx.set(coldKey2, {value: 'persisted-2'});
await Onyx.set(warmKey, {value: 'persisted-3'});
evictFromCache(coldKey1, coldKey2);
// Reset spies AFTER seeding so we only count calls made during mergeCollection itself.
const multiGetSpy = jest.spyOn(StorageMock, 'multiGet').mockClear();
const getItemSpy = jest.spyOn(StorageMock, 'getItem').mockClear();
await Onyx.mergeCollection(collectionKey, {
[coldKey1]: {value: 'merged-1'},
[coldKey2]: {value: 'merged-2'},