From a57811a8a86b4de6a70a4b1ff78dd330cf2ac23d Mon Sep 17 00:00:00 2001 From: alexjanousekGSA Date: Mon, 16 Jun 2025 19:33:32 -0400 Subject: [PATCH] Updated tests to mock new immutable window location object --- tests/javascripts/support/polyfills.js | 17 --- tests/javascripts/timeoutPopup.test.js | 158 ++++++++++++++----------- 2 files changed, 86 insertions(+), 89 deletions(-) delete mode 100644 tests/javascripts/support/polyfills.js diff --git a/tests/javascripts/support/polyfills.js b/tests/javascripts/support/polyfills.js deleted file mode 100644 index 1549cbf8f..000000000 --- a/tests/javascripts/support/polyfills.js +++ /dev/null @@ -1,17 +0,0 @@ -// Polyfills for any parts of the DOM API available in browsers but not JSDOM - -let _location = { - reload: jest.fn(), - hostname: "beta.notify.gov", - assign: jest.fn(), - href: "https://beta.notify.gov", -} - -// JSDOM provides a read-only window.location, which does not allow for -// mocking or setting. -Object.defineProperty(window, 'location', { - get: () => _location, - set: (value) => { - _location = value - }, -}) diff --git a/tests/javascripts/timeoutPopup.test.js b/tests/javascripts/timeoutPopup.test.js index 1909362f2..075fc72a3 100644 --- a/tests/javascripts/timeoutPopup.test.js +++ b/tests/javascripts/timeoutPopup.test.js @@ -1,7 +1,7 @@ beforeAll(() => { - jest.spyOn(global, 'setTimeout'); + jest.spyOn(global, 'setTimeout'); - document.body.innerHTML = ` + document.body.innerHTML = `
@@ -32,100 +32,114 @@ beforeAll(() => {
- ` + `; - const sessionTimerModule = require('../../app/assets/javascripts/timeoutPopup.js'); - window.GOVUK.modules.start(); + const sessionTimerModule = require('../../app/assets/javascripts/timeoutPopup.js'); + window.GOVUK.modules.start(); }); afterAll(() => { - document.body.innerHTML = ''; + document.body.innerHTML = ''; }); - describe('When the session timer module is loaded', () => { - beforeEach(() => { - jest.useFakeTimers(); - }); + beforeEach(() => { + jest.useFakeTimers(); + }); - afterEach(() => { - jest.useFakeTimers(); - }); + afterEach(() => { + jest.useFakeTimers(); + }); - test('everything initializes properly', () => { - const sessionTimer = document.getElementById("sessionTimer"); - sessionTimer.showModal = jest.fn(); - sessionTimer.close = jest.fn(); - - jest.runAllTimers(); - }); + 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(); + 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, }); - afterEach(() => { - jest.useFakeTimers(); - }); + const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession; - test('signoutUser method logs the user out', () => { - const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser; + extendSessionMethod(); - expect(window.location.href).toEqual(expect.not.stringContaining('/sign-out')); + expect(mockReload).toHaveBeenCalled(); + }); - signoutUserMethod(); + test('showTimer method shows the session timer modal', () => { + const sessionTimer = document.getElementById('sessionTimer'); + sessionTimer.showModal = jest.fn(); - expect(window.location.href).toEqual(expect.stringContaining('/sign-out')); - }); + const showTimerMock = jest.spyOn(sessionTimer, 'showModal'); - test('expireUserSession method logs the user out with next query parameter', () => { - const expireUserSessionMethod = window.GOVUK.Modules.TimeoutPopup.expireUserSession; + window.GOVUK.Modules.TimeoutPopup.showTimer(); - expect(window.location.href).toEqual(expect.not.stringContaining('/sign-out?next=')); + expect(showTimerMock).toHaveBeenCalled(); + }); - expireUserSessionMethod(); + test('closeTimer method closes the session timer modal', () => { + const sessionTimer = document.getElementById('sessionTimer'); + sessionTimer.close = jest.fn(); - expect(window.location.href).toEqual(expect.stringContaining('/sign-out?next=')); - }); + const closeTimerMock = jest.spyOn(sessionTimer, 'close'); - test('extendSession method reloads the page', () => { - const windowReload = jest.spyOn(window.location, 'reload'); - const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession; + window.GOVUK.Modules.TimeoutPopup.closeTimer(); - extendSessionMethod(); + expect(closeTimerMock).toHaveBeenCalled(); + }); - expect(windowReload).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(); - }); + test('checkTimer is called', () => { + const checkTimerMock = jest.spyOn( + window.GOVUK.Modules.TimeoutPopup, + 'checkTimer' + ); + window.GOVUK.Modules.TimeoutPopup.checkTimer(); + expect(checkTimerMock).toHaveBeenCalled(); + }); });