From 9ad129b20f7ecf69a7930c9a1dc7f94476eeb889 Mon Sep 17 00:00:00 2001 From: Jack Senyitko Date: Fri, 20 Mar 2026 12:49:30 -0400 Subject: [PATCH 1/3] unsubscribe from actual key --- lib/OnyxUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OnyxUtils.ts b/lib/OnyxUtils.ts index eca0cc90e..eb4e0ba1a 100644 --- a/lib/OnyxUtils.ts +++ b/lib/OnyxUtils.ts @@ -1242,7 +1242,7 @@ function unsubscribeFromKey(subscriptionID: number): void { return; } - deleteKeyBySubscriptions(lastSubscriptionID); + deleteKeyBySubscriptions(subscriptionID); delete callbackToStateMapping[subscriptionID]; } From cbe637a5e4e5d654438a6032c650c32e5f02655a Mon Sep 17 00:00:00 2001 From: Jack Senyitko Date: Mon, 23 Mar 2026 10:07:51 -0400 Subject: [PATCH 2/3] add tests --- tests/unit/onyxUtilsTest.ts | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/unit/onyxUtilsTest.ts b/tests/unit/onyxUtilsTest.ts index 07ed29beb..245c06f94 100644 --- a/tests/unit/onyxUtilsTest.ts +++ b/tests/unit/onyxUtilsTest.ts @@ -1,6 +1,7 @@ import {act} from '@testing-library/react-native'; import Onyx from '../../lib'; import OnyxUtils from '../../lib/OnyxUtils'; +import connectionManager from '../../lib/OnyxConnectionManager'; import type {GenericDeepRecord} from '../types'; import utils from '../../lib/utils'; import type {Collection, OnyxCollection} from '../../lib/types'; @@ -518,6 +519,57 @@ describe('OnyxUtils', () => { }); }); + describe('unsubscribeFromKey', () => { + // Disable the dot-notation rule, since connectionsMap is a private var, but we need to access it + // eslint-disable-next-line dot-notation + const connectionsMap = connectionManager['connectionsMap'] as Map; + + 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('afterInit', () => { beforeEach(() => { // Resets the deferred init task before each test. From 03f0d6dfbce9f6522cc992bd2bc6832ce4f3a0d6 Mon Sep 17 00:00:00 2001 From: Jack Senyitko Date: Mon, 23 Mar 2026 10:52:50 -0400 Subject: [PATCH 3/3] relocate tests --- tests/unit/OnyxConnectionManagerTest.ts | 47 ++++++++++++++++++++++ tests/unit/onyxUtilsTest.ts | 52 ------------------------- 2 files changed, 47 insertions(+), 52 deletions(-) diff --git a/tests/unit/OnyxConnectionManagerTest.ts b/tests/unit/OnyxConnectionManagerTest.ts index 088c613d3..b570c8107 100644 --- a/tests/unit/OnyxConnectionManagerTest.ts +++ b/tests/unit/OnyxConnectionManagerTest.ts @@ -360,6 +360,53 @@ describe('OnyxConnectionManager', () => { }); }); + 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'); diff --git a/tests/unit/onyxUtilsTest.ts b/tests/unit/onyxUtilsTest.ts index 245c06f94..07ed29beb 100644 --- a/tests/unit/onyxUtilsTest.ts +++ b/tests/unit/onyxUtilsTest.ts @@ -1,7 +1,6 @@ import {act} from '@testing-library/react-native'; import Onyx from '../../lib'; import OnyxUtils from '../../lib/OnyxUtils'; -import connectionManager from '../../lib/OnyxConnectionManager'; import type {GenericDeepRecord} from '../types'; import utils from '../../lib/utils'; import type {Collection, OnyxCollection} from '../../lib/types'; @@ -519,57 +518,6 @@ describe('OnyxUtils', () => { }); }); - describe('unsubscribeFromKey', () => { - // Disable the dot-notation rule, since connectionsMap is a private var, but we need to access it - // eslint-disable-next-line dot-notation - const connectionsMap = connectionManager['connectionsMap'] as Map; - - 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('afterInit', () => { beforeEach(() => { // Resets the deferred init task before each test.