import type { RumEvent, RumEventDomainContext } from '@datadog/browser-rum'; import { type Mutable, type RumEventType } from '../__mocks__'; import { beforeSend } from '../before-send'; import { enrichEvent } from '../enrich-event'; import { handleResource } from '../handle-resource'; jest.mock('../enrich-event'); jest.mock('../handle-resource'); describe(`[datadog-rum] ${beforeSend.name}`, () => { let event: Mutable; let context: RumEventDomainContext; let customHandler: (() => boolean) | undefined; beforeEach(() => { jest.clearAllMocks(); context = {}; event = { type: 'action' }; customHandler = undefined; }); const subject = () => beforeSend(customHandler)(event as RumEvent, context); function itReturns(value: boolean) { test(`returns ${value}`, () => expect(subject()).toBe(value)); } itReturns(true); test('calls enrichEvent with the event and context', () => { subject(); expect(enrichEvent).toHaveBeenCalledWith(event, context); }); describe('with a "resource" event', () => { beforeEach(() => (event.type = 'resource')); test('calls handleResource with the event and context', () => { subject(); expect(handleResource).toHaveBeenCalledWith(event, context); }); }); describe('with a non-resource event', () => { beforeEach(() => (event.type = 'action')); test('does not call handleResource', () => { subject(); expect(handleResource).not.toHaveBeenCalled(); }); }); describe('with custom handler', () => { let result: boolean; beforeEach(() => { result = true; customHandler = jest.fn(() => result); }); test('calls custom handler', () => { subject(); expect(customHandler).toHaveBeenCalled(); }); describe('when the handler returns false', () => { beforeEach(() => (result = false)); itReturns(false); }); }); });