|
1 | 1 | import {act} from '@testing-library/react-native'; |
2 | 2 | import Onyx from '../../lib'; |
3 | 3 | import OnyxUtils from '../../lib/OnyxUtils'; |
| 4 | +import connectionManager from '../../lib/OnyxConnectionManager'; |
4 | 5 | import type {GenericDeepRecord} from '../types'; |
5 | 6 | import utils from '../../lib/utils'; |
6 | 7 | import type {Collection, OnyxCollection} from '../../lib/types'; |
@@ -518,6 +519,57 @@ describe('OnyxUtils', () => { |
518 | 519 | }); |
519 | 520 | }); |
520 | 521 |
|
| 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 | + |
521 | 573 | describe('afterInit', () => { |
522 | 574 | beforeEach(() => { |
523 | 575 | // Resets the deferred init task before each test. |
|
0 commit comments