Skip to content

Commit cbe637a

Browse files
committed
add tests
1 parent 9ad129b commit cbe637a

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

tests/unit/onyxUtilsTest.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {act} from '@testing-library/react-native';
22
import Onyx from '../../lib';
33
import OnyxUtils from '../../lib/OnyxUtils';
4+
import connectionManager from '../../lib/OnyxConnectionManager';
45
import type {GenericDeepRecord} from '../types';
56
import utils from '../../lib/utils';
67
import type {Collection, OnyxCollection} from '../../lib/types';
@@ -518,6 +519,57 @@ describe('OnyxUtils', () => {
518519
});
519520
});
520521

522+
describe('unsubscribeFromKey', () => {
523+
// Disable the dot-notation rule, since connectionsMap is a private var, but we need to access it
524+
// eslint-disable-next-line dot-notation
525+
const connectionsMap = connectionManager['connectionsMap'] as Map<string, {subscriptionID: number}>;
526+
527+
it('should clean up the correct subscription ID from lastConnectionCallbackData on disconnect', async () => {
528+
const deleteSpy = jest.spyOn(Map.prototype, 'delete');
529+
530+
const connectionA = Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
531+
Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
532+
await act(async () => waitForPromisesToResolve());
533+
534+
const subscriptionIdA = connectionsMap.get(connectionA.id)?.subscriptionID;
535+
536+
await Onyx.set(ONYXKEYS.TEST_KEY, 'value1');
537+
await act(async () => waitForPromisesToResolve());
538+
539+
deleteSpy.mockClear();
540+
Onyx.disconnect(connectionA);
541+
542+
const numericDeleteArgs = deleteSpy.mock.calls.map((call) => call[0]).filter((arg): arg is number => typeof arg === 'number');
543+
expect(numericDeleteArgs).toContain(subscriptionIdA);
544+
545+
deleteSpy.mockRestore();
546+
});
547+
548+
it('should remove the subscription ID from onyxKeyToSubscriptionIDs on disconnect', async () => {
549+
const setSpy = jest.spyOn(Map.prototype, 'set');
550+
551+
const connectionA = Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
552+
const connectionB = Onyx.connect({key: ONYXKEYS.TEST_KEY, callback: jest.fn(), reuseConnection: false});
553+
await act(async () => waitForPromisesToResolve());
554+
555+
const subscriptionIdA = connectionsMap.get(connectionA.id)?.subscriptionID;
556+
const subscriptionIdB = connectionsMap.get(connectionB.id)?.subscriptionID;
557+
558+
setSpy.mockClear();
559+
Onyx.disconnect(connectionA);
560+
561+
const setCallsForKey = setSpy.mock.calls.filter((call) => call[0] === ONYXKEYS.TEST_KEY);
562+
expect(setCallsForKey.length).toBeGreaterThan(0);
563+
564+
const updatedIDs = setCallsForKey[setCallsForKey.length - 1][1] as number[];
565+
expect(updatedIDs).not.toContain(subscriptionIdA);
566+
expect(updatedIDs).toContain(subscriptionIdB);
567+
568+
setSpy.mockRestore();
569+
Onyx.disconnect(connectionB);
570+
});
571+
});
572+
521573
describe('afterInit', () => {
522574
beforeEach(() => {
523575
// Resets the deferred init task before each test.

0 commit comments

Comments
 (0)