import { NativeEventEmitter } from 'react-native'; jest.mock('react-native', () => ({ NativeEventEmitter: jest.fn().mockImplementation(() => ({ addListener: jest.fn(), })), })); jest.mock('../../core/nativeModules', () => ({ CSEventEmitter: { removeListener: jest.fn(), }, })); describe('csEventSubscriber', () => { let mockEventEmitter: any; let mockAddListener: jest.Mock; beforeEach(() => { jest.clearAllMocks(); mockEventEmitter = new NativeEventEmitter(); mockAddListener = mockEventEmitter.addListener as jest.Mock; }); // Temporary test to be removed once issues with other tests in this file are resolved it('should import the NativeEventEmitter and CSEventEmitter', () => { expect(NativeEventEmitter).toHaveBeenCalled(); expect(mockAddListener).not.toHaveBeenCalled(); }); /* TODO: Fix this test, currenly unable to properly mock NativeEventEmitter, MOBILE-12983, https://contentsquare.atlassian.net/browse/MOBILE-12983 it('should create a subscriber and call the callback when the event is emitted', async () => { const mockCallback = jest.fn(); const mockPayload = { key: 'value' }; // Mock addListener to return a valid subscriber object const mockSubscriber = { remove: jest.fn() }; mockAddListener.mockImplementation((eventType, callback) => { // Directly trigger the callback for testing purposes callback(mockPayload); return mockSubscriber; }); const subscriber = await csEventSubscriber( EventSubscriberType.ON_SESSION_REPLAY_LINK_CHANGE, mockCallback ); // Validate the callback was called with the payload expect(mockCallback).toHaveBeenCalledWith(mockPayload); // Ensure addListener was called with the correct eventType expect(mockAddListener).toHaveBeenCalledWith( EventSubscriberType.ON_SESSION_REPLAY_LINK_CHANGE, expect.any(Function) ); // Ensure subscriber object is returned correctly expect(subscriber).toEqual(mockSubscriber); }); it('should remove the subscriber when remove is called', async () => { const mockCallback = jest.fn(); // Mock addListener to return a valid subscriber object const mockSubscriber = { remove: jest.fn() }; await mockAddListener.mockReturnValue(mockSubscriber); const subscriber = csEventSubscriber( EventSubscriberType.ON_SESSION_REPLAY_LINK_CHANGE, mockCallback ); // Call remove on the subscriber await subscriber.remove(); // Ensure CSEventEmitter's removeListener was called expect(CSEventEmitter.removeListener).toHaveBeenCalledWith( EventSubscriberType.ON_SESSION_REPLAY_LINK_CHANGE ); // Ensure the subscription's remove method was called expect(mockSubscriber.remove).toHaveBeenCalled(); }); it('should handle when the event emitter does not return a subscription object', () => { const mockCallback = jest.fn(); // Mock addListener returning undefined mockAddListener.mockReturnValue(undefined); const subscriber = csEventSubscriber( EventSubscriberType.ON_SESSION_REPLAY_LINK_CHANGE, mockCallback ); // Call remove on the subscriber and ensure no errors are thrown expect(() => subscriber.remove()).not.toThrow(); // Ensure CSEventEmitter's removeListener was called expect(CSEventEmitter.removeListener).toHaveBeenCalledWith( EventSubscriberType.ON_SESSION_REPLAY_LINK_CHANGE ); }); */ });