// Mock the heap-react-native-autocapture module first const mockRegister = jest.fn(); const mockNormalizeOptions = jest.fn(); jest.mock('@contentsquare/react-native-autocapture', () => ({ register: mockRegister, normalizeOptions: mockNormalizeOptions, })); jest.mock('@heap/heap-javascript-bridge-support', () => ({ LogChannel: {}, LogLevel: {}, })); // Mock the heap module with all required properties const mockHeap = { logToConsole: jest.fn(), // Add other methods that might be used by the heap module start: jest.fn(), track: jest.fn(), identify: jest.fn(), }; const mockHeapLogger = { log: jest.fn(), error: jest.fn(), warn: jest.fn(), info: jest.fn(), }; jest.mock('../../heap', () => ({ Heap: mockHeap, HeapLogger: mockHeapLogger, })); // Mock the core module to control getProductAnalyticsOptions const mockGetProductAnalyticsOptions = jest.fn(); jest.mock('../../core/csqCore', () => ({ getProductAnalyticsOptions: mockGetProductAnalyticsOptions, })); describe('csqAutocapture', () => { let registerAutocapture: (isDefaultOrOptions?: boolean | { isDefault?: boolean; preserveTestIdProp?: boolean }) => void; let logToConsole: jest.Mock; beforeAll(() => { const module = require('../../autocapture/csqAutocapture'); registerAutocapture = module.registerAutocapture; logToConsole = module.logToConsole; }); beforeEach(() => { jest.clearAllMocks(); mockNormalizeOptions.mockReturnValue({}); mockRegister.mockImplementation(() => {}); mockGetProductAnalyticsOptions.mockReturnValue(undefined); }); afterEach(() => { jest.restoreAllMocks(); mockRegister.mockImplementation(() => {}); mockNormalizeOptions.mockImplementation((input) => input || {}); }); describe('registerAutocapture', () => { it('should call register with correct parameters when called with boolean true', () => { const isDefaultOrOptions = true; const normalizedOptions = { isDefault: true }; mockNormalizeOptions.mockReturnValue(normalizedOptions); const mockDate = new Date('2023-10-01T10:00:00.000Z'); jest.spyOn(global, 'Date').mockImplementation(() => mockDate); registerAutocapture(isDefaultOrOptions); expect(mockNormalizeOptions).toHaveBeenCalledWith(isDefaultOrOptions); expect(mockRegister).toHaveBeenCalledWith( mockHeap, mockHeapLogger, normalizedOptions, mockDate, undefined ); }); it('should call register with correct parameters when called with boolean false', () => { const isDefaultOrOptions = false; const normalizedOptions = { isDefault: false }; mockNormalizeOptions.mockReturnValue(normalizedOptions); const mockDate = new Date('2023-10-01T10:00:00.000Z'); jest.spyOn(global, 'Date').mockImplementation(() => mockDate); registerAutocapture(isDefaultOrOptions); expect(mockNormalizeOptions).toHaveBeenCalledWith(isDefaultOrOptions); expect(mockRegister).toHaveBeenCalledWith( mockHeap, mockHeapLogger, normalizedOptions, mockDate, undefined ); }); it('should call register with correct parameters when called with options object', () => { const isDefaultOrOptions = { isDefault: true, preserveTestIdProp: false, }; const normalizedOptions = { isDefault: true, preserveTestIdProp: false, }; mockNormalizeOptions.mockReturnValue(normalizedOptions); const mockDate = new Date('2023-10-01T10:00:00.000Z'); jest.spyOn(global, 'Date').mockImplementation(() => mockDate); registerAutocapture(isDefaultOrOptions); expect(mockNormalizeOptions).toHaveBeenCalledWith(isDefaultOrOptions); expect(mockRegister).toHaveBeenCalledWith( mockHeap, mockHeapLogger, normalizedOptions, mockDate, undefined ); }); it('should call register with correct parameters when called with undefined', () => { const normalizedOptions = {}; mockNormalizeOptions.mockReturnValue(normalizedOptions); const mockDate = new Date('2023-10-01T10:00:00.000Z'); jest.spyOn(global, 'Date').mockImplementation(() => mockDate); registerAutocapture(undefined); expect(mockNormalizeOptions).toHaveBeenCalledWith(undefined); expect(mockRegister).toHaveBeenCalledWith( mockHeap, mockHeapLogger, normalizedOptions, mockDate, undefined ); }); it('should call register with correct parameters when called with no arguments', () => { const normalizedOptions = {}; mockNormalizeOptions.mockReturnValue(normalizedOptions); const mockDate = new Date('2023-10-01T10:00:00.000Z'); jest.spyOn(global, 'Date').mockImplementation(() => mockDate); registerAutocapture(); expect(mockNormalizeOptions).toHaveBeenCalledWith(undefined); expect(mockRegister).toHaveBeenCalledWith( mockHeap, mockHeapLogger, normalizedOptions, mockDate, undefined ); }); it('should call register with StartRecordingOptions when ProductAnalyticsOptions is provided', () => { const productAnalyticsOptions = { baseUrl: 'https://example.com', uploadInterval: 5000, clearEventPropertiesOnNewUser: true, resumePreviousSession: false, captureAdvertiserId: true, captureVendorId: false, disablePageviewAutocapture: false, disableInteractionAutocapture: true, disableInteractionTextCapture: false, disableInteractionAccessibilityLabelCapture: true, enableRNAutocapture: true, }; const expectedStartRecordingOptions = { baseUrl: 'https://example.com', uploadInterval: 5000, clearEventPropertiesOnNewUser: true, resumePreviousSession: false, captureAdvertiserId: true, captureVendorId: false, disablePageviewAutocapture: false, disableInteractionAutocapture: true, disableInteractionTextCapture: false, disableInteractionAccessibilityLabelCapture: true, }; mockGetProductAnalyticsOptions.mockReturnValue(productAnalyticsOptions); const isDefaultOrOptions = { isDefault: true }; const normalizedOptions = { isDefault: true }; mockNormalizeOptions.mockReturnValue(normalizedOptions); const mockDate = new Date('2023-10-01T10:00:00.000Z'); jest.spyOn(global, 'Date').mockImplementation(() => mockDate); registerAutocapture(isDefaultOrOptions); expect(mockGetProductAnalyticsOptions).toHaveBeenCalled(); expect(mockNormalizeOptions).toHaveBeenCalledWith(isDefaultOrOptions); expect(mockRegister).toHaveBeenCalledWith( mockHeap, mockHeapLogger, normalizedOptions, mockDate, expectedStartRecordingOptions ); }); it('should skip registration and log warning when enableRNAutocapture is false', () => { const productAnalyticsOptions = { baseUrl: 'https://example.com', enableRNAutocapture: false, }; mockGetProductAnalyticsOptions.mockReturnValue(productAnalyticsOptions); const isDefaultOrOptions = { isDefault: true }; registerAutocapture(isDefaultOrOptions); expect(mockGetProductAnalyticsOptions).toHaveBeenCalled(); expect(mockHeapLogger.warn).toHaveBeenCalledWith( '[Autocapture] React Native autocapture registration skipped: enableRNAutocapture is set to false in configureProductAnalytics options' ); expect(mockNormalizeOptions).not.toHaveBeenCalled(); expect(mockRegister).not.toHaveBeenCalled(); }); }); describe('logToConsole', () => { it('should call Heap.logToConsole when invoked', () => { logToConsole(); expect(mockHeap.logToConsole).toHaveBeenCalled(); }); }); });