Updated tests to mock new immutable window location object

This commit is contained in:
alexjanousekGSA
2025-06-16 19:33:32 -04:00
parent b4b8435fb7
commit a57811a8a8
2 changed files with 86 additions and 89 deletions

View File

@@ -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
},
})

View File

@@ -32,7 +32,7 @@ beforeAll(() => {
</div> </div>
</div> </div>
</dialog> </dialog>
` `;
const sessionTimerModule = require('../../app/assets/javascripts/timeoutPopup.js'); const sessionTimerModule = require('../../app/assets/javascripts/timeoutPopup.js');
window.GOVUK.modules.start(); window.GOVUK.modules.start();
@@ -42,7 +42,6 @@ afterAll(() => {
document.body.innerHTML = ''; document.body.innerHTML = '';
}); });
describe('When the session timer module is loaded', () => { describe('When the session timer module is loaded', () => {
beforeEach(() => { beforeEach(() => {
jest.useFakeTimers(); jest.useFakeTimers();
@@ -53,16 +52,14 @@ describe('When the session timer module is loaded', () => {
}); });
test('everything initializes properly', () => { test('everything initializes properly', () => {
const sessionTimer = document.getElementById("sessionTimer"); const sessionTimer = document.getElementById('sessionTimer');
sessionTimer.showModal = jest.fn(); sessionTimer.showModal = jest.fn();
sessionTimer.close = jest.fn(); sessionTimer.close = jest.fn();
jest.runAllTimers(); jest.runAllTimers();
}); });
}); });
describe('The session timer ', () => { describe('The session timer ', () => {
beforeEach(() => { beforeEach(() => {
jest.useFakeTimers(); jest.useFakeTimers();
@@ -75,7 +72,9 @@ describe('The session timer ', () => {
test('signoutUser method logs the user out', () => { test('signoutUser method logs the user out', () => {
const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser; const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser;
expect(window.location.href).toEqual(expect.not.stringContaining('/sign-out')); expect(window.location.href).toEqual(
expect.not.stringContaining('/sign-out')
);
signoutUserMethod(); signoutUserMethod();
@@ -83,26 +82,38 @@ describe('The session timer ', () => {
}); });
test('expireUserSession method logs the user out with next query parameter', () => { test('expireUserSession method logs the user out with next query parameter', () => {
const expireUserSessionMethod = window.GOVUK.Modules.TimeoutPopup.expireUserSession; const expireUserSessionMethod =
window.GOVUK.Modules.TimeoutPopup.expireUserSession;
expect(window.location.href).toEqual(expect.not.stringContaining('/sign-out?next=')); expect(window.location.href).toEqual(
expect.not.stringContaining('/sign-out?next=')
);
expireUserSessionMethod(); expireUserSessionMethod();
expect(window.location.href).toEqual(expect.stringContaining('/sign-out?next=')); expect(window.location.href).toEqual(
expect.stringContaining('/sign-out?next=')
);
}); });
test('extendSession method reloads the page', () => { test('extendSession method reloads the page', () => {
const windowReload = jest.spyOn(window.location, 'reload'); // 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; const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession;
extendSessionMethod(); extendSessionMethod();
expect(windowReload).toHaveBeenCalled(); expect(mockReload).toHaveBeenCalled();
}); });
test('showTimer method shows the session timer modal', () => { test('showTimer method shows the session timer modal', () => {
const sessionTimer = document.getElementById("sessionTimer"); const sessionTimer = document.getElementById('sessionTimer');
sessionTimer.showModal = jest.fn(); sessionTimer.showModal = jest.fn();
const showTimerMock = jest.spyOn(sessionTimer, 'showModal'); const showTimerMock = jest.spyOn(sessionTimer, 'showModal');
@@ -113,7 +124,7 @@ describe('The session timer ', () => {
}); });
test('closeTimer method closes the session timer modal', () => { test('closeTimer method closes the session timer modal', () => {
const sessionTimer = document.getElementById("sessionTimer"); const sessionTimer = document.getElementById('sessionTimer');
sessionTimer.close = jest.fn(); sessionTimer.close = jest.fn();
const closeTimerMock = jest.spyOn(sessionTimer, 'close'); const closeTimerMock = jest.spyOn(sessionTimer, 'close');
@@ -124,7 +135,10 @@ describe('The session timer ', () => {
}); });
test('checkTimer is called', () => { test('checkTimer is called', () => {
const checkTimerMock = jest.spyOn(window.GOVUK.Modules.TimeoutPopup, "checkTimer"); const checkTimerMock = jest.spyOn(
window.GOVUK.Modules.TimeoutPopup,
'checkTimer'
);
window.GOVUK.Modules.TimeoutPopup.checkTimer(); window.GOVUK.Modules.TimeoutPopup.checkTimer();
expect(checkTimerMock).toHaveBeenCalled(); expect(checkTimerMock).toHaveBeenCalled();
}); });