|
| 1 | +import { CustomEventCollector } from '../../events/customEventCollector'; |
| 2 | +import { EventType } from '../../events/eventType'; |
| 3 | +import { validateAttributes } from '../../utils/attributeValueValidator'; |
| 4 | + |
| 5 | +jest.mock('../../native/measureBridge', () => ({ |
| 6 | + trackEvent: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +const mockTrackEvent = require('../../native/measureBridge') |
| 10 | + .trackEvent as jest.Mock; |
| 11 | + |
| 12 | +describe('CustomEventCollector', () => { |
| 13 | + let logger: { log: jest.Mock }; |
| 14 | + let timeProvider: { now: jest.Mock }; |
| 15 | + let configProvider: { |
| 16 | + maxEventNameLength: number; |
| 17 | + customEventNameRegex: string; |
| 18 | + }; |
| 19 | + let collector: CustomEventCollector; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + logger = { log: jest.fn() }; |
| 24 | + timeProvider = { now: jest.fn(() => 123456789) }; |
| 25 | + configProvider = { |
| 26 | + maxEventNameLength: 10, |
| 27 | + customEventNameRegex: '^[a-zA-Z0-9_]+$', |
| 28 | + }; |
| 29 | + collector = new CustomEventCollector({ |
| 30 | + logger: logger as any, |
| 31 | + timeProvider: timeProvider as any, |
| 32 | + configProvider: configProvider as any, |
| 33 | + }); |
| 34 | + collector.register(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('does nothing when disabled', async () => { |
| 38 | + collector.unregister(); |
| 39 | + await collector.trackCustomEvent('test'); |
| 40 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 41 | + expect(logger.log).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('logs error if name is empty', async () => { |
| 45 | + await collector.trackCustomEvent(''); |
| 46 | + expect(logger.log).toHaveBeenCalledWith( |
| 47 | + 'error', |
| 48 | + 'Invalid event: name is empty' |
| 49 | + ); |
| 50 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('logs error if name is too long', async () => { |
| 54 | + await collector.trackCustomEvent('toolongnamehere'); |
| 55 | + expect(logger.log).toHaveBeenCalledWith( |
| 56 | + 'error', |
| 57 | + 'Invalid event(toolongnamehere): exceeds maximum length of 10 characters' |
| 58 | + ); |
| 59 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('logs error if name fails regex', async () => { |
| 63 | + await collector.trackCustomEvent('bad-name!'); |
| 64 | + expect(logger.log).toHaveBeenCalledWith( |
| 65 | + 'error', |
| 66 | + 'Invalid event(bad-name!) format' |
| 67 | + ); |
| 68 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | +it('does not track event when attributes are invalid', async () => { |
| 72 | + const attrs = { good: 'ok', bad: { nested: true } } as any; |
| 73 | + |
| 74 | + // Run the call — should detect invalid attributes and not track |
| 75 | + await collector.trackCustomEvent('event1', attrs, 111); |
| 76 | + |
| 77 | + // Expect that native trackEvent was never called |
| 78 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 79 | + |
| 80 | + // Expect that an error was logged instead |
| 81 | + expect(logger.log).toHaveBeenCalledWith( |
| 82 | + 'error', |
| 83 | + 'Invalid attributes provided for event(event1). Dropping the event.' |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | + it('logs error if nativeTrackEvent throws', async () => { |
| 88 | + mockTrackEvent.mockRejectedValueOnce(new Error('boom')); |
| 89 | + await collector.trackCustomEvent('event2'); |
| 90 | + expect(logger.log).toHaveBeenCalledWith( |
| 91 | + 'error', |
| 92 | + expect.stringContaining('Failed to track custom event event2') |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments