-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathonyxTest.js
1047 lines (946 loc) · 38.5 KB
/
onyxTest.js
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 _ from 'underscore';
import Onyx from '../../lib';
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
const ONYX_KEYS = {
TEST_KEY: 'test',
OTHER_TEST: 'otherTest',
COLLECTION: {
TEST_KEY: 'test_',
TEST_CONNECT_COLLECTION: 'testConnectCollection_',
TEST_POLICY: 'testPolicy_',
TEST_UPDATE: 'testUpdate_',
},
};
Onyx.init({
keys: ONYX_KEYS,
registerStorageEventListener: () => {},
initialKeyStates: {
[ONYX_KEYS.OTHER_TEST]: 42,
},
});
describe('Onyx', () => {
let connectionID;
/** @type OnyxCache */
let cache;
beforeEach(() => {
cache = require('../../lib/OnyxCache').default;
});
afterEach(() => {
Onyx.disconnect(connectionID);
return Onyx.clear();
});
it('should remove key value from OnyxCache/Storage when set is called with null value', () =>
Onyx.set(ONYX_KEYS.OTHER_TEST, 42)
.then(() => Onyx.getAllKeys())
.then((keys) => {
expect(keys.includes(ONYX_KEYS.OTHER_TEST)).toBe(true);
return Onyx.set(ONYX_KEYS.OTHER_TEST, null);
})
.then(() => {
// Checks if cache value is removed.
expect(cache.getAllKeys().length).toBe(0);
// When cache keys length is 0, we fetch the keys from storage.
return Onyx.getAllKeys();
})
.then((keys) => {
expect(keys.includes(ONYX_KEYS.OTHER_TEST)).toBe(false);
}));
it('should set a simple key', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
// Set a simple key
return Onyx.set(ONYX_KEYS.TEST_KEY, 'test').then(() => {
expect(testKeyValue).toBe('test');
});
});
it('should merge an object with another object', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, {test1: 'test1'})
.then(() => {
expect(testKeyValue).toEqual({test1: 'test1'});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {test2: 'test2'});
})
.then(() => {
expect(testKeyValue).toEqual({test1: 'test1', test2: 'test2'});
});
});
it('should notify subscribers when data has been cleared', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
let otherTestValue;
const otherTestConnectionID = Onyx.connect({
key: ONYX_KEYS.OTHER_TEST,
callback: (value) => {
otherTestValue = value;
},
});
return waitForPromisesToResolve()
.then(() => Onyx.set(ONYX_KEYS.TEST_KEY, 'test'))
.then(() => {
expect(testKeyValue).toBe('test');
return Onyx.clear().then(waitForPromisesToResolve);
})
.then(() => {
// Test key should be cleared
expect(testKeyValue).toBeNull();
// Other test key should be returned to its default state
expect(otherTestValue).toBe(42);
return Onyx.disconnect(otherTestConnectionID);
});
});
it('should not notify subscribers after they have disconnected', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, 'test')
.then(() => {
expect(testKeyValue).toBe('test');
Onyx.disconnect(connectionID);
return Onyx.set(ONYX_KEYS.TEST_KEY, 'test updated');
})
.then(() => {
// Test value has not changed
expect(testKeyValue).toBe('test');
});
});
it('should merge arrays by replacing previous value with new value', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, ['test1'])
.then(() => {
expect(testKeyValue).toStrictEqual(['test1']);
return Onyx.merge(ONYX_KEYS.TEST_KEY, ['test2', 'test3', 'test4']);
})
.then(waitForPromisesToResolve)
.then(() => {
expect(testKeyValue).toStrictEqual(['test2', 'test3', 'test4']);
});
});
it('should merge 2 objects when it has no initial stored value for test key', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
Onyx.merge(ONYX_KEYS.TEST_KEY, {test1: 'test1'});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {test2: 'test2'}).then(() => {
expect(testKeyValue).toStrictEqual({test1: 'test1', test2: 'test2'});
});
});
it('should merge 2 arrays when it has no initial stored value for test key', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
Onyx.merge(ONYX_KEYS.TEST_KEY, ['test1']);
return Onyx.merge(ONYX_KEYS.TEST_KEY, ['test2']).then(() => {
expect(testKeyValue).toEqual(['test2']);
});
});
it('should ignore top-level undefined values', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, {test1: 'test1'})
.then(() => {
expect(testKeyValue).toEqual({test1: 'test1'});
return Onyx.merge(ONYX_KEYS.TEST_KEY, undefined);
})
.then(() => {
expect(testKeyValue).toEqual({test1: 'test1'});
});
});
it('should remove keys that are set to null when merging', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, {
test1: {
test2: 'test2',
test3: {
test4: 'test4',
},
},
})
.then(() => {
expect(testKeyValue).toEqual({
test1: {
test2: 'test2',
test3: {
test4: 'test4',
},
},
});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {
test1: {
test3: {
test4: null,
},
},
});
})
.then(() => {
expect(testKeyValue).toEqual({
test1: {
test2: 'test2',
test3: {},
},
});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {
test1: {
test3: null,
},
});
})
.then(() => {
expect(testKeyValue).toEqual({test1: {test2: 'test2'}});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {test1: null});
})
.then(() => {
expect(testKeyValue).toEqual({});
});
});
it('should ignore `undefined` values when merging', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, {
test1: {
test2: 'test2',
test3: 'test3',
},
})
.then(() => {
expect(testKeyValue).toEqual({
test1: {
test2: 'test2',
test3: 'test3',
},
});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {
test1: {
test2: undefined,
},
});
})
.then(() => {
expect(testKeyValue).toEqual({test1: {test2: 'test2', test3: 'test3'}});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {test1: undefined});
})
.then(() => {
expect(testKeyValue).toEqual({test1: {test2: 'test2', test3: 'test3'}});
});
});
it('should overwrite an array key nested inside an object', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
Onyx.merge(ONYX_KEYS.TEST_KEY, {something: [1, 2, 3]});
return Onyx.merge(ONYX_KEYS.TEST_KEY, {something: [4]}).then(() => {
expect(testKeyValue).toEqual({something: [4]});
});
});
it('should overwrite an array key nested inside an object when using merge on a collection', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
Onyx.merge(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {something: [1, 2, 3]}});
return Onyx.merge(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {something: [4]}}).then(() => {
expect(testKeyValue).toEqual({test_1: {something: [4]}});
});
});
it('should properly set and merge when using mergeCollection', () => {
const valuesReceived = {};
const mockCallback = jest.fn((data) => (valuesReceived[data.ID] = data.value));
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_KEY,
initWithStoredValues: false,
callback: mockCallback,
});
// The first time we call mergeCollection we'll be doing a multiSet internally
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: {
ID: 123,
value: 'one',
},
test_2: {
ID: 234,
value: 'two',
},
test_3: {
ID: 345,
value: 'three',
},
})
.then(() =>
// 2 key values to update and 2 new keys to add.
// MergeCollection will perform a mix of multiSet and multiMerge
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: {
ID: 123,
value: 'five',
},
test_2: {
ID: 234,
value: 'four',
},
test_4: {
ID: 456,
value: 'two',
},
test_5: {
ID: 567,
value: 'one',
},
}),
)
.then(() => {
// 3 items on the first mergeCollection + 4 items the next mergeCollection
expect(mockCallback).toHaveBeenCalledTimes(7);
expect(mockCallback).toHaveBeenNthCalledWith(1, {ID: 123, value: 'one'}, 'test_1');
expect(mockCallback).toHaveBeenNthCalledWith(2, {ID: 234, value: 'two'}, 'test_2');
expect(mockCallback).toHaveBeenNthCalledWith(3, {ID: 345, value: 'three'}, 'test_3');
expect(mockCallback).toHaveBeenNthCalledWith(4, {ID: 123, value: 'five'}, 'test_1');
expect(mockCallback).toHaveBeenNthCalledWith(5, {ID: 234, value: 'four'}, 'test_2');
expect(mockCallback).toHaveBeenNthCalledWith(6, {ID: 456, value: 'two'}, 'test_4');
expect(mockCallback).toHaveBeenNthCalledWith(7, {ID: 567, value: 'one'}, 'test_5');
expect(valuesReceived[123]).toEqual('five');
expect(valuesReceived[234]).toEqual('four');
expect(valuesReceived[345]).toEqual('three');
expect(valuesReceived[456]).toEqual('two');
expect(valuesReceived[567]).toEqual('one');
});
});
it('should skip the update when a key not belonging to collection key is present in mergeCollection', () => {
const valuesReceived = {};
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_KEY,
initWithStoredValues: false,
callback: (data, key) => (valuesReceived[key] = data),
});
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {test_1: {ID: 123}, notMyTest: {beep: 'boop'}}).then(() => {
expect(valuesReceived).toEqual({});
});
});
it('should return full object to callback when calling mergeCollection()', () => {
const valuesReceived = {};
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_KEY,
initWithStoredValues: false,
callback: (data, key) => (valuesReceived[key] = data),
});
return Onyx.multiSet({
test_1: {
existingData: 'test',
},
test_2: {
existingData: 'test',
},
})
.then(() =>
Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_KEY, {
test_1: {
ID: 123,
value: 'one',
},
test_2: {
ID: 234,
value: 'two',
},
}),
)
.then(() => {
expect(valuesReceived).toEqual({
test_1: {
ID: 123,
value: 'one',
existingData: 'test',
},
test_2: {
ID: 234,
value: 'two',
existingData: 'test',
},
});
});
});
it('should use update data object to set/merge keys', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
let otherTestKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.OTHER_TEST,
initWithStoredValues: false,
callback: (value) => {
otherTestKeyValue = value;
},
});
return waitForPromisesToResolve()
.then(() => {
// Given the initial Onyx state: {test: true, otherTest: {test1: 'test1'}}
Onyx.set(ONYX_KEYS.TEST_KEY, true);
Onyx.set(ONYX_KEYS.OTHER_TEST, {test1: 'test1'});
return waitForPromisesToResolve();
})
.then(() => {
expect(testKeyValue).toBe(true);
expect(otherTestKeyValue).toEqual({test1: 'test1'});
// When we pass a data object to Onyx.update
return Onyx.update([
{
onyxMethod: 'set',
key: ONYX_KEYS.TEST_KEY,
value: 'one',
},
{
onyxMethod: 'merge',
key: ONYX_KEYS.OTHER_TEST,
value: {test2: 'test2'},
},
]);
})
.then(() => {
// Then the final Onyx state should be {test: 'one', otherTest: {test1: 'test1', test2: 'test2'}}
expect(testKeyValue).toBe('one');
expect(otherTestKeyValue).toEqual({test1: 'test1', test2: 'test2'});
});
});
it('should use update data object to merge a collection of keys', () => {
const valuesReceived = {};
const mockCallback = jest.fn((data) => (valuesReceived[data.ID] = data.value));
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_KEY,
initWithStoredValues: false,
callback: mockCallback,
});
return waitForPromisesToResolve()
.then(() => {
// Given the initial Onyx state: {test_1: {existingData: 'test',}, test_2: {existingData: 'test',}}
Onyx.multiSet({
test_1: {
existingData: 'test',
},
test_2: {
existingData: 'test',
},
});
return waitForPromisesToResolve();
})
.then(() => {
expect(mockCallback).toHaveBeenNthCalledWith(1, {existingData: 'test'}, 'test_1');
expect(mockCallback).toHaveBeenNthCalledWith(2, {existingData: 'test'}, 'test_2');
// When we pass a mergeCollection data object to Onyx.update
return Onyx.update([
{
onyxMethod: 'mergecollection',
key: ONYX_KEYS.COLLECTION.TEST_KEY,
value: {
test_1: {
ID: 123,
value: 'one',
},
test_2: {
ID: 234,
value: 'two',
},
test_3: {
ID: 345,
value: 'three',
},
},
},
]);
})
.then(() => {
/* Then the final Onyx state should be:
{
test_1: {
existingData: 'test'
ID: 123,
value: 'one',
},
test_2: {
existingData: 'test'
ID: 234,
value: 'two',
},
test_3: {
ID: 345,
value: 'three',
},
}
*/
expect(mockCallback).toHaveBeenNthCalledWith(3, {ID: 123, value: 'one', existingData: 'test'}, 'test_1');
expect(mockCallback).toHaveBeenNthCalledWith(4, {ID: 234, value: 'two', existingData: 'test'}, 'test_2');
expect(mockCallback).toHaveBeenNthCalledWith(5, {ID: 345, value: 'three'}, 'test_3');
});
});
it('should throw an error when the data object is incorrect in Onyx.update', () => {
// Given the invalid data object with onyxMethod='multiSet'
const data = [
{onyxMethod: 'set', key: ONYX_KEYS.TEST_KEY, value: 'four'},
{onyxMethod: 'murge', key: ONYX_KEYS.OTHER_TEST, value: {test2: 'test2'}},
];
try {
// When we pass it to Onyx.update
Onyx.update(data);
} catch (error) {
// Then we should expect the error message below
expect(error.message).toEqual('Invalid onyxMethod murge in Onyx update.');
}
try {
// Given the invalid data object with key=true
data[1] = {onyxMethod: 'merge', key: true, value: {test2: 'test2'}};
// When we pass it to Onyx.update
Onyx.update(data);
} catch (error) {
// Then we should expect the error message below
expect(error.message).toEqual('Invalid boolean key provided in Onyx update. Onyx key must be of type string.');
}
});
it('should properly set all keys provided in a multiSet called via update', () => {
const valuesReceived = {};
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_KEY,
initWithStoredValues: false,
callback: (data, key) => (valuesReceived[key] = data),
});
return Onyx.multiSet({
test_1: {
existingData: 'test',
},
test_2: {
existingData: 'test',
},
})
.then(() =>
Onyx.update([
{
onyxMethod: 'multiset',
value: {
test_1: {
ID: 123,
value: 'one',
},
test_2: {
ID: 234,
value: 'two',
},
},
},
]),
)
.then(() => {
expect(valuesReceived).toEqual({
test_1: {
ID: 123,
value: 'one',
},
test_2: {
ID: 234,
value: 'two',
},
});
});
});
it('should reject an improperly formatted multiset operation called via update', () => {
try {
Onyx.update([
{
onyxMethod: 'multiset',
value: [
{
ID: 123,
value: 'one',
},
{
ID: 234,
value: 'two',
},
],
},
]);
} catch (error) {
expect(error.message).toEqual('Invalid value provided in Onyx multiSet. Onyx multiSet value must be of type object.');
}
});
it('should return all collection keys as a single object when waitForCollectionCallback = true', () => {
const mockCallback = jest.fn();
// Given some initial collection data
const initialCollectionData = {
testConnectCollection_1: {
ID: 123,
value: 'one',
},
testConnectCollection_2: {
ID: 234,
value: 'two',
},
testConnectCollection_3: {
ID: 345,
value: 'three',
},
};
return Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION, initialCollectionData)
.then(() => {
// When we connect to that collection with waitForCollectionCallback = true
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_CONNECT_COLLECTION,
waitForCollectionCallback: true,
callback: mockCallback,
});
return waitForPromisesToResolve();
})
.then(() => {
// Then we expect the callback to be called only once and the initial stored value to be initialCollectionData
expect(mockCallback).toHaveBeenCalledTimes(1);
expect(mockCallback).toHaveBeenCalledWith(initialCollectionData, undefined);
});
});
it('should return all collection keys as a single object when updating a collection key with waitForCollectionCallback = true', () => {
const mockCallback = jest.fn();
const collectionUpdate = {
testPolicy_1: {ID: 234, value: 'one'},
testPolicy_2: {ID: 123, value: 'two'},
};
// Given an Onyx.connect call with waitForCollectionCallback=true
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_POLICY,
waitForCollectionCallback: true,
callback: mockCallback,
});
return (
waitForPromisesToResolve()
// When mergeCollection is called with an updated collection
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate))
.then(() => {
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
expect(mockCallback).toHaveBeenCalledTimes(2);
// AND the value for the first call should be null since the collection was not initialized at that point
expect(mockCallback).toHaveBeenNthCalledWith(1, null, undefined);
// AND the value for the second call should be collectionUpdate since the collection was updated
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate);
})
);
});
it('should send a value to Onyx.connect() when subscribing to a specific collection member key and keysChanged() is called', () => {
const mockCallback = jest.fn();
const collectionUpdate = {
testPolicy_1: {ID: 234, value: 'one'},
testPolicy_2: {ID: 123, value: 'two'},
};
// Given an Onyx.connect call with waitForCollectionCallback=false
connectionID = Onyx.connect({
key: `${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`,
callback: mockCallback,
});
return (
waitForPromisesToResolve()
// When mergeCollection is called with an updated collection
.then(() => Onyx.mergeCollection(ONYX_KEYS.COLLECTION.TEST_POLICY, collectionUpdate))
.then(() => {
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
expect(mockCallback).toHaveBeenCalledTimes(2);
// AND the value for the first call should be null since the collection was not initialized at that point
expect(mockCallback).toHaveBeenNthCalledWith(1, null, undefined);
// AND the value for the second call should be collectionUpdate since the collection was updated
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate.testPolicy_1, 'testPolicy_1');
})
);
});
it('should return all collection keys as a single object for subscriber using waitForCollectionCallback when a single collection member key is updated', () => {
const mockCallback = jest.fn();
const collectionUpdate = {
testPolicy_1: {ID: 234, value: 'one'},
};
// Given an Onyx.connect call with waitForCollectionCallback=true
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_POLICY,
waitForCollectionCallback: true,
callback: mockCallback,
});
return (
waitForPromisesToResolve()
// When mergeCollection is called with an updated collection
.then(() => Onyx.merge(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, collectionUpdate.testPolicy_1))
.then(() => {
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
expect(mockCallback).toHaveBeenCalledTimes(2);
// AND the value for the second call should be collectionUpdate
expect(mockCallback).toHaveBeenLastCalledWith(collectionUpdate);
})
);
});
it('should return a promise when set() called with the same value and there is no change', () => {
const promiseOne = Onyx.set('test', 'pizza');
expect(promiseOne).toBeInstanceOf(Promise);
return promiseOne.then(() => {
const promiseTwo = Onyx.set('test', 'pizza');
expect(promiseTwo).toBeInstanceOf(Promise);
});
});
it('should not update a subscriber if the value in the cache has not changed at all', () => {
const mockCallback = jest.fn();
const collectionUpdate = {
testPolicy_1: {ID: 234, value: 'one'},
};
// Given an Onyx.connect call with waitForCollectionCallback=true
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_POLICY,
waitForCollectionCallback: true,
callback: mockCallback,
});
return (
waitForPromisesToResolve()
// When merge is called with an updated collection
.then(() => Onyx.merge(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, collectionUpdate.testPolicy_1))
.then(() => {
// Then we expect the callback to have called twice, once for the initial connect call + once for the collection update
expect(mockCallback).toHaveBeenCalledTimes(2);
// And the value for the second call should be collectionUpdate
expect(mockCallback).toHaveBeenNthCalledWith(2, collectionUpdate);
})
// When merge is called again with the same collection not modified
.then(() => Onyx.merge(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, collectionUpdate.testPolicy_1))
.then(() => {
// Then we should not expect another invocation of the callback
expect(mockCallback).toHaveBeenCalledTimes(2);
})
// When merge is called again with an object of equivalent value but not the same reference
.then(() => Onyx.merge(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, _.clone(collectionUpdate.testPolicy_1)))
.then(() => {
// Then we should not expect another invocation of the callback
expect(mockCallback).toHaveBeenCalledTimes(2);
})
);
});
it('should update subscriber if the value in the cache has not changed at all but initWithStoredValues === false', () => {
const mockCallback = jest.fn();
const collectionUpdate = {
testPolicy_1: {ID: 234, value: 'one'},
};
// Given an Onyx.connect call with waitForCollectionCallback=true
connectionID = Onyx.connect({
key: ONYX_KEYS.COLLECTION.TEST_POLICY,
waitForCollectionCallback: true,
callback: mockCallback,
initWithStoredValues: false,
});
return (
waitForPromisesToResolve()
// When merge is called with an updated collection
.then(() => Onyx.merge(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, collectionUpdate.testPolicy_1))
.then(() => {
// Then we expect the callback to have called once. 0 times the initial connect call + 1 time for the merge()
expect(mockCallback).toHaveBeenCalledTimes(1);
// And the value for the second call should be collectionUpdate
expect(mockCallback).toHaveBeenNthCalledWith(1, collectionUpdate);
})
// When merge is called again with the same collection not modified
.then(() => Onyx.merge(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, collectionUpdate.testPolicy_1))
.then(() => {
// Then we should expect another invocation of the callback because initWithStoredValues = false
expect(mockCallback).toHaveBeenCalledTimes(2);
})
// When merge is called again with an object of equivalent value but not the same reference
.then(() => Onyx.merge(`${ONYX_KEYS.COLLECTION.TEST_POLICY}${1}`, _.clone(collectionUpdate.testPolicy_1)))
.then(() => {
// Then we should expect another invocation of the callback because initWithStoredValues = false
expect(mockCallback).toHaveBeenCalledTimes(3);
})
);
});
it('should return a promise that completes when all update() operations are done', () => {
const connectionIDs = [];
const testCallback = jest.fn();
const otherTestCallback = jest.fn();
const collectionCallback = jest.fn();
const itemKey = `${ONYX_KEYS.COLLECTION.TEST_UPDATE}1`;
connectionIDs.push(Onyx.connect({key: ONYX_KEYS.TEST_KEY, callback: testCallback}));
connectionIDs.push(Onyx.connect({key: ONYX_KEYS.OTHER_TEST, callback: otherTestCallback}));
connectionIDs.push(Onyx.connect({key: ONYX_KEYS.COLLECTION.TEST_UPDATE, callback: collectionCallback, waitForCollectionCallback: true}));
return Onyx.update([
{onyxMethod: Onyx.METHOD.SET, key: ONYX_KEYS.TEST_KEY, value: 'taco'},
{onyxMethod: Onyx.METHOD.MERGE, key: ONYX_KEYS.OTHER_TEST, value: 'pizza'},
{onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: ONYX_KEYS.COLLECTION.TEST_UPDATE, value: {[itemKey]: {a: 'a'}}},
]).then(() => {
expect(collectionCallback).toHaveBeenNthCalledWith(1, {[itemKey]: {a: 'a'}});
expect(testCallback).toHaveBeenNthCalledWith(1, 'taco', ONYX_KEYS.TEST_KEY);
expect(otherTestCallback).toHaveBeenNthCalledWith(1, 'pizza', ONYX_KEYS.OTHER_TEST);
Onyx.disconnect(connectionIDs);
});
});
it('should merge an object with a batch of objects and undefined', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, {test1: 'test1'})
.then(() => {
expect(testKeyValue).toEqual({test1: 'test1'});
Onyx.merge(ONYX_KEYS.TEST_KEY, {test2: 'test2'});
Onyx.merge(ONYX_KEYS.TEST_KEY, {test3: 'test3'});
Onyx.merge(ONYX_KEYS.TEST_KEY, undefined);
Onyx.merge(ONYX_KEYS.TEST_KEY, {test4: 'test4'});
Onyx.merge(ONYX_KEYS.TEST_KEY, undefined);
return waitForPromisesToResolve();
})
.then(() => {
expect(testKeyValue).toEqual({
test1: 'test1',
test2: 'test2',
test3: 'test3',
test4: 'test4',
});
});
});
it('should merge an object with null and overwrite the value', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, {test1: 'test1'})
.then(() => {
expect(testKeyValue).toEqual({test1: 'test1'});
Onyx.merge(ONYX_KEYS.TEST_KEY, null);
Onyx.merge(ONYX_KEYS.TEST_KEY, {test2: 'test2'});
Onyx.merge(ONYX_KEYS.TEST_KEY, {test3: 'test3'});
return waitForPromisesToResolve();
})
.then(() => {
expect(testKeyValue).toEqual({
test2: 'test2',
test3: 'test3',
});
});
});
it('should merge a key with null and allow subsequent updates', () => {
let testKeyValue;
connectionID = Onyx.connect({
key: ONYX_KEYS.TEST_KEY,
initWithStoredValues: false,
callback: (value) => {
testKeyValue = value;
},
});
return Onyx.set(ONYX_KEYS.TEST_KEY, 1)