import { clearBeforeEachErrorRecoveryCount, getBeforeEachErrorRecoveryCount, hasBeforeEachErrorRecoveryBeenAttempted, incrementBeforeEachErrorRecoveryCount, resetBeforeEachErrorRecoveryStateForTests, } from "./before-each-error-recovery"; import { sessionStorageKeyConstants } from "@/constants/session-storage"; describe("router/methods/before-each-error-recovery", () => { const recoveryKey = sessionStorageKeyConstants.BEFORE_EACH_ERROR_RECOVERY_COUNT; let originalSessionStorage; beforeEach(() => { resetBeforeEachErrorRecoveryStateForTests(); originalSessionStorage = window.sessionStorage; }); afterEach(() => { Object.defineProperty(window, "sessionStorage", { configurable: true, value: originalSessionStorage, }); resetBeforeEachErrorRecoveryStateForTests(); }); it("reads and writes recovery count via sessionStorage when available", () => { incrementBeforeEachErrorRecoveryCount(); expect(window.sessionStorage.getItem(recoveryKey)).toBe("1"); expect(getBeforeEachErrorRecoveryCount()).toBe(1); expect(hasBeforeEachErrorRecoveryBeenAttempted()).toBe(true); }); it("clears sessionStorage and in-memory count", () => { incrementBeforeEachErrorRecoveryCount(); clearBeforeEachErrorRecoveryCount(); expect(window.sessionStorage.getItem(recoveryKey)).toBeNull(); expect(getBeforeEachErrorRecoveryCount()).toBe(0); expect(hasBeforeEachErrorRecoveryBeenAttempted()).toBe(false); }); it("uses in-memory count when sessionStorage getItem throws", () => { Object.defineProperty(window, "sessionStorage", { configurable: true, value: { getItem: () => { throw new Error("sessionStorage blocked"); }, setItem: jest.fn(), removeItem: jest.fn(), }, }); incrementBeforeEachErrorRecoveryCount(); incrementBeforeEachErrorRecoveryCount(); expect(hasBeforeEachErrorRecoveryBeenAttempted()).toBe(true); expect(getBeforeEachErrorRecoveryCount()).toBe(2); }); it("uses in-memory count when sessionStorage setItem throws", () => { Object.defineProperty(window, "sessionStorage", { configurable: true, value: { getItem: () => null, setItem: () => { throw new Error("sessionStorage blocked"); }, removeItem: jest.fn(), }, }); incrementBeforeEachErrorRecoveryCount(); expect(hasBeforeEachErrorRecoveryBeenAttempted()).toBe(true); expect(getBeforeEachErrorRecoveryCount()).toBe(1); }); });