import { minimalApplication } from '../__mocks__/init-config'; import { bundleState } from '../bundle-state'; import { getOrCreateSharedImpl } from '../get-shared'; import { init } from '../init'; import type { InitRumOptions } from '../types'; jest.mock('../get-shared'); describe(`[datadog-rum] ${init.name}`, () => { const cleanup = jest.fn(); const setServiceContext = jest.fn(); let initializedSdk: boolean; let options: InitRumOptions; beforeEach(() => { jest.clearAllMocks(); bundleState.isSdkInitializer = false; initializedSdk = false; jest.mocked(getOrCreateSharedImpl).mockReturnValue({ init: jest.fn(() => ({ cleanup, initializedSdk })), setServiceContext, }); options = { service: { name: 'desktop' }, application: minimalApplication }; }); const subject = () => init(options); test('forwards to the shared implementation', () => { subject(); expect(getOrCreateSharedImpl().init).toHaveBeenCalledWith(options); }); test('returns the cleanup from the implementation', () => { expect(subject().cleanup).toBe(cleanup); }); test('returns a setServiceContext bound to the service name', () => { const context = { orgId: '42' }; subject().setServiceContext(context); expect(setServiceContext).toHaveBeenCalledWith(options.service.name, context); }); describe('when the implementation initialized the SDK', () => { beforeEach(() => { initializedSdk = true; }); test('marks this bundle as the SDK initializer', () => { subject(); expect(bundleState.isSdkInitializer).toBe(true); }); }); describe('when the implementation did not initialize the SDK', () => { beforeEach(() => { initializedSdk = false; }); test('does not mark this bundle as the SDK initializer', () => { subject(); expect(bundleState.isSdkInitializer).toBe(false); }); }); });