import { DATADOG_RUM_FOR_DEV } from '../constants'; import { clearOverride, enableOverride, isForceEnabled } from '../override'; const TTL_MS = 12 * 60 * 60 * 1000; const NOW = 1_700_000_000_000; describe('[datadog-rum] runtime-override/override', () => { const record = (exp: number) => localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify({ exp })); beforeEach(() => { jest.restoreAllMocks(); localStorage.clear(); jest.spyOn(Date, 'now').mockReturnValue(NOW); }); describe('isForceEnabled', () => { const subject = () => isForceEnabled(); const itReturns = (value: boolean) => test(`is ${value}`, () => { expect(subject()).toBe(value); }); describe('without an override', () => itReturns(false)); describe('with a non-expired override', () => { beforeEach(() => record(NOW + 1000)); itReturns(true); }); describe('with an expired override', () => { beforeEach(() => record(NOW - 1)); itReturns(false); }); describe('with a malformed override', () => { beforeEach(() => localStorage.setItem(DATADOG_RUM_FOR_DEV, 'not json')); itReturns(false); }); describe('with a record missing an expiry', () => { beforeEach(() => localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify({}))); itReturns(false); }); describe('when storage access throws', () => { beforeEach(() => jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { throw new Error('blocked'); }) ); itReturns(false); }); }); describe('enableOverride', () => { const subject = () => enableOverride(); test('persists a 12h override window', () => { subject(); expect(localStorage.getItem(DATADOG_RUM_FOR_DEV)).toBe( JSON.stringify({ exp: NOW + TTL_MS }) ); }); describe('when the storage write fails', () => { beforeEach(() => jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { throw new Error('blocked'); }) ); test('does not throw', () => { expect(subject).not.toThrow(); }); }); }); describe('clearOverride', () => { const subject = () => clearOverride(); beforeEach(() => record(NOW + 1000)); test('removes the override', () => { subject(); expect(localStorage.getItem(DATADOG_RUM_FOR_DEV)).toBeNull(); }); describe('when the storage removal fails', () => { beforeEach(() => jest.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => { throw new Error('blocked'); }) ); test('does not throw', () => { expect(subject).not.toThrow(); }); }); }); });