-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathOnyxConnectionManagerTest.ts
More file actions
582 lines (428 loc) · 27.6 KB
/
OnyxConnectionManagerTest.ts
File metadata and controls
582 lines (428 loc) · 27.6 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
import {act} from '@testing-library/react-native';
import Onyx from '../../lib';
import type {Connection} from '../../lib/OnyxConnectionManager';
import connectionManager from '../../lib/OnyxConnectionManager';
import OnyxCache from '../../lib/OnyxCache';
import StorageMock from '../../lib/storage';
import type GenericCollection from '../utils/GenericCollection';
import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
// We need access to some internal properties of `connectionManager` during the tests but they are private,
// so this workaround allows us to have access to them.
// eslint-disable-next-line dot-notation
const connectionsMap = connectionManager['connectionsMap'];
// eslint-disable-next-line dot-notation
const generateConnectionID = connectionManager['generateConnectionID'];
// eslint-disable-next-line dot-notation
const getSessionID = () => connectionManager['sessionID'];
const ONYXKEYS = {
TEST_KEY: 'test',
TEST_KEY_2: 'test2',
COLLECTION: {
TEST_KEY: 'test_',
TEST_KEY_2: 'test2_',
},
};
Onyx.init({
keys: ONYXKEYS,
});
beforeEach(() => Onyx.clear());
describe('OnyxConnectionManager', () => {
// Always use a "fresh" instance
beforeEach(() => {
connectionManager.disconnectAll();
});
describe('generateConnectionID', () => {
it('should generate a stable connection ID', async () => {
const connectionID = generateConnectionID({key: ONYXKEYS.TEST_KEY});
expect(connectionID).toEqual(`onyxKey=${ONYXKEYS.TEST_KEY},initWithStoredValues=true,waitForCollectionCallback=false,sessionID=${getSessionID()}`);
});
it("should generate a stable connection ID regardless of the order which the option's properties were passed", async () => {
const connectionID = generateConnectionID({key: ONYXKEYS.TEST_KEY, waitForCollectionCallback: true, initWithStoredValues: true});
expect(connectionID).toEqual(`onyxKey=${ONYXKEYS.TEST_KEY},initWithStoredValues=true,waitForCollectionCallback=true,sessionID=${getSessionID()}`);
});
it('should generate unique connection IDs if certain options are passed', async () => {
const connectionID1 = generateConnectionID({key: ONYXKEYS.TEST_KEY, reuseConnection: false});
const connectionID2 = generateConnectionID({key: ONYXKEYS.TEST_KEY, reuseConnection: false});
expect(connectionID1.startsWith(`onyxKey=${ONYXKEYS.TEST_KEY},initWithStoredValues=true,waitForCollectionCallback=false,sessionID=${getSessionID()},uniqueID=`)).toBeTruthy();
expect(connectionID2.startsWith(`onyxKey=${ONYXKEYS.TEST_KEY},initWithStoredValues=true,waitForCollectionCallback=false,sessionID=${getSessionID()},uniqueID=`)).toBeTruthy();
expect(connectionID1).not.toEqual(connectionID2);
const connectionID3 = generateConnectionID({key: ONYXKEYS.TEST_KEY, initWithStoredValues: false});
expect(connectionID3.startsWith(`onyxKey=${ONYXKEYS.TEST_KEY},initWithStoredValues=false,waitForCollectionCallback=false,sessionID=${getSessionID()},uniqueID=`)).toBeTruthy();
});
it('should generate an unique connection ID if the session ID is changed', async () => {
const connectionID1 = generateConnectionID({key: ONYXKEYS.TEST_KEY});
connectionManager.refreshSessionID();
const connectionID2 = generateConnectionID({key: ONYXKEYS.TEST_KEY});
expect(connectionID1).not.toEqual(connectionID2);
});
});
describe('connect / disconnect', () => {
it('should connect to a key and fire the callback with its value', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
const callback1 = jest.fn();
const connection = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback1});
expect(connectionsMap.has(connection.id)).toBeTruthy();
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
connectionManager.disconnect(connection);
expect(connectionsMap.size).toEqual(0);
});
it('should connect two times to the same key and fire both callbacks with its value', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
const callback1 = jest.fn();
const connection1 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback1});
const callback2 = jest.fn();
const connection2 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback2});
expect(connection1.id).toEqual(connection2.id);
expect(connectionsMap.size).toEqual(1);
expect(connectionsMap.has(connection1.id)).toBeTruthy();
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
connectionManager.disconnect(connection1);
connectionManager.disconnect(connection2);
expect(connectionsMap.size).toEqual(0);
});
it('should connect two times to the same key but with different options, and fire the callbacks differently', async () => {
const obj1 = {id: 'entry1_id', name: 'entry1_name'};
const obj2 = {id: 'entry2_id', name: 'entry2_name'};
const collection = {
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]: obj1,
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`]: obj2,
} as GenericCollection;
await StorageMock.multiSet([
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, obj1],
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`, obj2],
]);
const callback1 = jest.fn();
const connection1 = connectionManager.connect({key: ONYXKEYS.COLLECTION.TEST_KEY, callback: callback1});
const callback2 = jest.fn();
const connection2 = connectionManager.connect({key: ONYXKEYS.COLLECTION.TEST_KEY, callback: callback2, waitForCollectionCallback: true});
expect(connection1.id).not.toEqual(connection2.id);
expect(connectionsMap.size).toEqual(2);
expect(connectionsMap.has(connection1.id)).toBeTruthy();
expect(connectionsMap.has(connection2.id)).toBeTruthy();
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(2);
expect(callback1).toHaveBeenNthCalledWith(1, obj1, `${ONYXKEYS.COLLECTION.TEST_KEY}entry1`);
expect(callback1).toHaveBeenNthCalledWith(2, obj2, `${ONYXKEYS.COLLECTION.TEST_KEY}entry2`);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith(collection, ONYXKEYS.COLLECTION.TEST_KEY, undefined);
connectionManager.disconnect(connection1);
connectionManager.disconnect(connection2);
expect(connectionsMap.size).toEqual(0);
});
it('should connect to a key, connect some times more after first connection is made, and fire all subsequent callbacks immediately with its value', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
const callback1 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback1});
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
const callback2 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback2});
const callback3 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback3});
const callback4 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback4});
await act(async () => waitForPromisesToResolve());
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
expect(callback3).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
expect(callback4).toHaveBeenCalledTimes(1);
expect(callback4).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
});
it('should have the connection object already defined when triggering the callback of the second connection to the same key', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
const callback1 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback1});
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
const callback2 = jest.fn();
const connection2 = connectionManager.connect({
key: ONYXKEYS.TEST_KEY,
callback: (...params) => {
callback2(...params);
connectionManager.disconnect(connection2);
},
});
await act(async () => waitForPromisesToResolve());
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
expect(connectionsMap.size).toEqual(1);
});
it('should create a separate connection to the same key when setting reuseConnection to false', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
const callback1 = jest.fn();
const connection1 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback1});
const callback2 = jest.fn();
const connection2 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, reuseConnection: false, callback: callback2});
expect(connection1.id).not.toEqual(connection2.id);
expect(connectionsMap.size).toEqual(2);
expect(connectionsMap.has(connection1.id)).toBeTruthy();
expect(connectionsMap.has(connection2.id)).toBeTruthy();
});
it('should create a separate connection to the same key when setting initWithStoredValues to false', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
const callback1 = jest.fn();
const connection1 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, initWithStoredValues: false, callback: callback1});
await act(async () => waitForPromisesToResolve());
expect(callback1).not.toHaveBeenCalled();
expect(connectionsMap.size).toEqual(1);
expect(connectionsMap.has(connection1.id)).toBeTruthy();
await Onyx.set(ONYXKEYS.TEST_KEY, 'test2');
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith('test2', ONYXKEYS.TEST_KEY);
const callback2 = jest.fn();
const connection2 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, initWithStoredValues: false, callback: callback2});
await act(async () => waitForPromisesToResolve());
expect(callback2).not.toHaveBeenCalled();
expect(connectionsMap.size).toEqual(2);
expect(connectionsMap.has(connection2.id)).toBeTruthy();
await Onyx.set(ONYXKEYS.TEST_KEY, 'test3');
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith('test3', ONYXKEYS.TEST_KEY);
connectionManager.disconnect(connection1);
connectionManager.disconnect(connection2);
expect(connectionsMap.size).toEqual(0);
});
it("should create a separate connection to the same key when it's a collection one and waitForCollectionCallback is undefined/false", async () => {
const collection = {
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]: {id: 'entry1_id', name: 'entry1_name'},
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`]: {id: 'entry2_id', name: 'entry2_name'},
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry3`]: {id: 'entry3_id', name: 'entry3_name'},
};
Onyx.mergeCollection(ONYXKEYS.COLLECTION.TEST_KEY, collection as GenericCollection);
await act(async () => waitForPromisesToResolve());
const callback1 = jest.fn();
const connection1 = connectionManager.connect({key: ONYXKEYS.COLLECTION.TEST_KEY, waitForCollectionCallback: undefined, callback: callback1});
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(3);
expect(callback1).toHaveBeenNthCalledWith(1, collection[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`], `${ONYXKEYS.COLLECTION.TEST_KEY}entry1`);
expect(callback1).toHaveBeenNthCalledWith(2, collection[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`], `${ONYXKEYS.COLLECTION.TEST_KEY}entry2`);
expect(callback1).toHaveBeenNthCalledWith(3, collection[`${ONYXKEYS.COLLECTION.TEST_KEY}entry3`], `${ONYXKEYS.COLLECTION.TEST_KEY}entry3`);
const callback2 = jest.fn();
const connection2 = connectionManager.connect({key: ONYXKEYS.COLLECTION.TEST_KEY, waitForCollectionCallback: false, callback: callback2});
expect(connection1.id).not.toEqual(connection2.id);
expect(connectionsMap.size).toEqual(2);
expect(connectionsMap.has(connection1.id)).toBeTruthy();
expect(connectionsMap.has(connection2.id)).toBeTruthy();
await act(async () => waitForPromisesToResolve());
expect(callback2).toHaveBeenCalledTimes(3);
expect(callback2).toHaveBeenNthCalledWith(1, collection[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`], `${ONYXKEYS.COLLECTION.TEST_KEY}entry1`);
expect(callback2).toHaveBeenNthCalledWith(2, collection[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`], `${ONYXKEYS.COLLECTION.TEST_KEY}entry2`);
expect(callback2).toHaveBeenNthCalledWith(3, collection[`${ONYXKEYS.COLLECTION.TEST_KEY}entry3`], `${ONYXKEYS.COLLECTION.TEST_KEY}entry3`);
});
it('should not throw any errors when passing an undefined connection or trying to access an inexistent one inside disconnect()', () => {
expect(connectionsMap.size).toEqual(0);
expect(() => {
connectionManager.disconnect(undefined as unknown as Connection);
}).not.toThrow();
expect(() => {
connectionManager.disconnect({id: 'connectionID1', callbackID: 'callbackID1'});
}).not.toThrow();
});
it('should create a separate connection for the same key after a Onyx.clear() call', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
const callback1 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback1});
expect(connectionsMap.size).toEqual(1);
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith('test', ONYXKEYS.TEST_KEY);
callback1.mockReset();
await act(async () => Onyx.clear());
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith(undefined, ONYXKEYS.TEST_KEY);
callback1.mockReset();
const callback2 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback2});
const callback3 = jest.fn();
connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback3});
// We expect to have two connections for ONYXKEYS.TEST_KEY, one for the first subscription before Onyx.clear(),
// and the other for the two subscriptions with the same key after Onyx.clear().
expect(connectionsMap.size).toEqual(2);
await act(async () => waitForPromisesToResolve());
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith(undefined, undefined);
expect(callback3).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenCalledWith(undefined, undefined);
callback1.mockReset();
callback2.mockReset();
callback3.mockReset();
Onyx.merge(ONYXKEYS.TEST_KEY, 'test2');
await act(async () => waitForPromisesToResolve());
expect(callback1).toHaveBeenCalledTimes(1);
expect(callback1).toHaveBeenCalledWith('test2', ONYXKEYS.TEST_KEY);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenCalledWith('test2', ONYXKEYS.TEST_KEY);
expect(callback3).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenCalledWith('test2', ONYXKEYS.TEST_KEY);
});
});
describe('unsubscribeFromKey', () => {
it('should clean up the correct subscription ID from lastConnectionCallbackData on disconnect', async () => {
const deleteSpy = jest.spyOn(Map.prototype, 'delete');
const connectionA = Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
await act(async () => waitForPromisesToResolve());
const subscriptionIdA = connectionsMap.get(connectionA.id)?.subscriptionID;
await Onyx.set(ONYXKEYS.TEST_KEY, 'value1');
await act(async () => waitForPromisesToResolve());
deleteSpy.mockClear();
Onyx.disconnect(connectionA);
const numericDeleteArgs = deleteSpy.mock.calls.map((call) => call[0]).filter((arg): arg is number => typeof arg === 'number');
expect(numericDeleteArgs).toContain(subscriptionIdA);
deleteSpy.mockRestore();
});
it('should remove the subscription ID from onyxKeyToSubscriptionIDs on disconnect', async () => {
const setSpy = jest.spyOn(Map.prototype, 'set');
const connectionA = Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
const connectionB = Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
await act(async () => waitForPromisesToResolve());
const subscriptionIdA = connectionsMap.get(connectionA.id)?.subscriptionID;
const subscriptionIdB = connectionsMap.get(connectionB.id)?.subscriptionID;
setSpy.mockClear();
Onyx.disconnect(connectionA);
const setCallsForKey = setSpy.mock.calls.filter((call) => call[0] === ONYXKEYS.TEST_KEY);
expect(setCallsForKey.length).toBeGreaterThan(0);
const updatedIDs = setCallsForKey[setCallsForKey.length - 1][1] as number[];
expect(updatedIDs).not.toContain(subscriptionIdA);
expect(updatedIDs).toContain(subscriptionIdB);
setSpy.mockRestore();
Onyx.disconnect(connectionB);
});
});
describe('disconnectAll', () => {
it('should disconnect all connections', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
await StorageMock.setItem(ONYXKEYS.TEST_KEY_2, 'test2');
const callback1 = jest.fn();
const connection1 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback1});
const callback2 = jest.fn();
const connection2 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: callback2});
const callback3 = jest.fn();
const connection3 = connectionManager.connect({key: ONYXKEYS.TEST_KEY_2, callback: callback3});
expect(connection1.id).toEqual(connection2.id);
expect(connectionsMap.size).toEqual(2);
expect(connectionsMap.has(connection1.id)).toBeTruthy();
expect(connectionsMap.has(connection3.id)).toBeTruthy();
await act(async () => waitForPromisesToResolve());
connectionManager.disconnectAll();
expect(connectionsMap.size).toEqual(0);
});
});
describe('refreshSessionID', () => {
it('should create a separate connection for the same key if the session ID changes', async () => {
await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
await StorageMock.setItem(ONYXKEYS.TEST_KEY_2, 'test2');
const connection1 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn()});
expect(connectionsMap.size).toEqual(1);
connectionManager.refreshSessionID();
const connection2 = connectionManager.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn()});
expect(connectionsMap.size).toEqual(2);
expect(connectionsMap.has(connection1.id)).toBeTruthy();
expect(connectionsMap.has(connection2.id)).toBeTruthy();
});
});
describe('addToEvictionBlockList / removeFromEvictionBlockList', () => {
it('should add and remove connections from the eviction block list correctly', async () => {
const evictionBlocklist = OnyxCache.getEvictionBlocklist();
connectionsMap.set('connectionID1', {subscriptionID: 0, onyxKey: ONYXKEYS.TEST_KEY, callbacks: new Map(), isConnectionMade: true});
connectionsMap.get('connectionID1')?.callbacks.set('callbackID1', () => undefined);
connectionManager.addToEvictionBlockList({id: 'connectionID1', callbackID: 'callbackID1'});
expect(evictionBlocklist[ONYXKEYS.TEST_KEY]).toEqual(['connectionID1_callbackID1']);
connectionsMap.get('connectionID1')?.callbacks.set('callbackID2', () => undefined);
connectionManager.addToEvictionBlockList({id: 'connectionID1', callbackID: 'callbackID2'});
expect(evictionBlocklist[ONYXKEYS.TEST_KEY]).toEqual(['connectionID1_callbackID1', 'connectionID1_callbackID2']);
connectionsMap.set('connectionID2', {subscriptionID: 1, onyxKey: `${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, callbacks: new Map(), isConnectionMade: true});
connectionsMap.get('connectionID2')?.callbacks.set('callbackID3', () => undefined);
connectionManager.addToEvictionBlockList({id: 'connectionID2', callbackID: 'callbackID3'});
expect(evictionBlocklist[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]).toEqual(['connectionID2_callbackID3']);
connectionManager.removeFromEvictionBlockList({id: 'connectionID2', callbackID: 'callbackID3'});
expect(evictionBlocklist[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]).toBeUndefined();
// inexistent callback ID, shouldn't do anything
connectionManager.removeFromEvictionBlockList({id: 'connectionID1', callbackID: 'callbackID1000'});
expect(evictionBlocklist[ONYXKEYS.TEST_KEY]).toEqual(['connectionID1_callbackID1', 'connectionID1_callbackID2']);
connectionManager.removeFromEvictionBlockList({id: 'connectionID1', callbackID: 'callbackID2'});
expect(evictionBlocklist[ONYXKEYS.TEST_KEY]).toEqual(['connectionID1_callbackID1']);
connectionManager.removeFromEvictionBlockList({id: 'connectionID1', callbackID: 'callbackID1'});
expect(evictionBlocklist[ONYXKEYS.TEST_KEY]).toBeUndefined();
// inexistent connection ID, shouldn't do anything
expect(() => connectionManager.removeFromEvictionBlockList({id: 'connectionID0', callbackID: 'callbackID0'})).not.toThrow();
});
it('should not throw any errors when passing an undefined connection or trying to access an inexistent one inside addToEvictionBlockList()', () => {
expect(connectionsMap.size).toEqual(0);
expect(() => {
connectionManager.addToEvictionBlockList(undefined as unknown as Connection);
}).not.toThrow();
expect(() => {
connectionManager.addToEvictionBlockList({id: 'connectionID1', callbackID: 'callbackID1'});
}).not.toThrow();
});
it('should not throw any errors when passing an undefined connection or trying to access an inexistent one inside removeFromEvictionBlockList()', () => {
expect(connectionsMap.size).toEqual(0);
expect(() => {
connectionManager.removeFromEvictionBlockList(undefined as unknown as Connection);
}).not.toThrow();
expect(() => {
connectionManager.removeFromEvictionBlockList({id: 'connectionID1', callbackID: 'callbackID1'});
}).not.toThrow();
});
});
describe('sourceValue parameter', () => {
it('should pass the sourceValue parameter to collection callbacks when waitForCollectionCallback is true', async () => {
const obj1 = {id: 'entry1_id', name: 'entry1_name'};
const obj2 = {id: 'entry2_id', name: 'entry2_name'};
const callback = jest.fn();
const connection = connectionManager.connect({
key: ONYXKEYS.COLLECTION.TEST_KEY,
callback,
waitForCollectionCallback: true,
});
await act(async () => waitForPromisesToResolve());
// Initial callback with undefined values
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(undefined, ONYXKEYS.COLLECTION.TEST_KEY, undefined);
// Reset mock to test the next update
callback.mockReset();
// Update with first object
await Onyx.merge(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, obj1);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith({[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]: obj1}, ONYXKEYS.COLLECTION.TEST_KEY, {[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]: obj1});
// Reset mock to test the next update
callback.mockReset();
// Update with second object
await Onyx.merge(`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`, obj2);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(
{
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`]: obj1,
[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`]: obj2,
},
ONYXKEYS.COLLECTION.TEST_KEY,
{[`${ONYXKEYS.COLLECTION.TEST_KEY}entry2`]: obj2},
);
connectionManager.disconnect(connection);
});
it('should not pass sourceValue to regular callbacks when waitForCollectionCallback is false', async () => {
const obj1 = {id: 'entry1_id', name: 'entry1_name'};
const callback = jest.fn();
const connection = connectionManager.connect({
key: ONYXKEYS.COLLECTION.TEST_KEY,
callback,
waitForCollectionCallback: false,
});
await act(async () => waitForPromisesToResolve());
// Update with object
await Onyx.merge(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, obj1);
expect(callback).toHaveBeenCalledWith(obj1, `${ONYXKEYS.COLLECTION.TEST_KEY}entry1`);
connectionManager.disconnect(connection);
});
});
});