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

@@ -1,7 +1,7 @@
beforeAll(() => { beforeAll(() => {
jest.spyOn(global, 'setTimeout'); jest.spyOn(global, 'setTimeout');
document.body.innerHTML = ` document.body.innerHTML = `
<dialog class="usa-modal" id="sessionTimer" aria-labelledby="sessionTimerHeading" aria-describedby="timeLeft"> <dialog class="usa-modal" id="sessionTimer" aria-labelledby="sessionTimerHeading" aria-describedby="timeLeft">
<div class="usa-modal__content"> <div class="usa-modal__content">
<div class="usa-modal__main"> <div class="usa-modal__main">
@@ -32,100 +32,114 @@ 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();
}); });
afterAll(() => { 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();
}); });
afterEach(() => { afterEach(() => {
jest.useFakeTimers(); jest.useFakeTimers();
}); });
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();
});
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(() => { const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession;
jest.useFakeTimers();
});
test('signoutUser method logs the user out', () => { extendSessionMethod();
const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser;
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', () => { window.GOVUK.Modules.TimeoutPopup.showTimer();
const expireUserSessionMethod = window.GOVUK.Modules.TimeoutPopup.expireUserSession;
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', () => { window.GOVUK.Modules.TimeoutPopup.closeTimer();
const windowReload = jest.spyOn(window.location, 'reload');
const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession;
extendSessionMethod(); expect(closeTimerMock).toHaveBeenCalled();
});
expect(windowReload).toHaveBeenCalled(); test('checkTimer is called', () => {
}); const checkTimerMock = jest.spyOn(
window.GOVUK.Modules.TimeoutPopup,
test('showTimer method shows the session timer modal', () => { 'checkTimer'
const sessionTimer = document.getElementById("sessionTimer"); );
sessionTimer.showModal = jest.fn(); window.GOVUK.Modules.TimeoutPopup.checkTimer();
expect(checkTimerMock).toHaveBeenCalled();
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();
});
}); });