beforeAll(() => { jest.spyOn(global, 'setTimeout'); document.body.innerHTML = `

Your session will end soon. Please choose to extend your session or sign out. Your session will expire in 5 minutes or less.

You have been inactive for too long. Your session will expire in .

`; const sessionTimerModule = require('../../app/assets/javascripts/timeoutPopup.js'); window.GOVUK.modules.start(); }); afterAll(() => { document.body.innerHTML = ''; }); describe('When the session timer module is loaded', () => { beforeEach(() => { jest.useFakeTimers(); }); afterEach(() => { jest.useFakeTimers(); }); test('everything initializes properly', () => { const sessionTimer = document.getElementById('sessionTimer'); sessionTimer.showModal = jest.fn(); sessionTimer.close = jest.fn(); jest.runAllTimers(); }); }); describe('The session timer ', () => { beforeEach(() => { jest.useFakeTimers(); }); afterEach(() => { jest.useFakeTimers(); }); test('signoutUser method logs the user out', () => { const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser; expect(window.location.href).toEqual( expect.not.stringContaining('/sign-out') ); signoutUserMethod(); expect(window.location.href).toEqual(expect.stringContaining('/sign-out')); }); test('expireUserSession method logs the user out with next query parameter', () => { const expireUserSessionMethod = window.GOVUK.Modules.TimeoutPopup.expireUserSession; expect(window.location.href).toEqual( expect.not.stringContaining('/sign-out?next=') ); expireUserSessionMethod(); expect(window.location.href).toEqual( expect.stringContaining('/sign-out?next=') ); }); test('extendSession method reloads the page', () => { // Jest 30 made location.reload read-only, so we can't spy on it the old way. // Instead, we have to replace it with our own mock function. const mockReload = jest.fn(); Object.defineProperty(window.location, 'reload', { value: mockReload, configurable: true, }); const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession; extendSessionMethod(); expect(mockReload).toHaveBeenCalled(); }); test('showTimer method shows the session timer modal', () => { const sessionTimer = document.getElementById('sessionTimer'); sessionTimer.showModal = jest.fn(); const showTimerMock = jest.spyOn(sessionTimer, 'showModal'); window.GOVUK.Modules.TimeoutPopup.showTimer(); expect(showTimerMock).toHaveBeenCalled(); }); test('closeTimer method closes the session timer modal', () => { const sessionTimer = document.getElementById('sessionTimer'); sessionTimer.close = jest.fn(); const closeTimerMock = jest.spyOn(sessionTimer, 'close'); window.GOVUK.Modules.TimeoutPopup.closeTimer(); expect(closeTimerMock).toHaveBeenCalled(); }); test('checkTimer is called', () => { const checkTimerMock = jest.spyOn( window.GOVUK.Modules.TimeoutPopup, 'checkTimer' ); window.GOVUK.Modules.TimeoutPopup.checkTimer(); expect(checkTimerMock).toHaveBeenCalled(); }); });