Improving code coverage

This commit is contained in:
alexjanousekGSA
2025-06-16 20:30:44 -04:00
parent f12a977608
commit 4abf4068a9
+15 -37
View File
@@ -70,61 +70,39 @@ describe('The session timer ', () => {
}); });
test('signoutUser method logs the user out', () => { test('signoutUser method logs the user out', () => {
// Replace the real function with a fake one we can track const restore = mockWindowLocation();
const originalSignoutUser = window.GOVUK.Modules.TimeoutPopup.signoutUser;
const mockSignoutUser = jest.fn(() => {
// Do what the real function would do
window.location.href = '/sign-out';
});
window.GOVUK.Modules.TimeoutPopup.signoutUser = mockSignoutUser;
// Test the actual function, not a mock
const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser; const signoutUserMethod = window.GOVUK.Modules.TimeoutPopup.signoutUser;
signoutUserMethod();
expect(mockSignoutUser).toHaveBeenCalled(); // This will try to set location.href but our mock will catch it
expect(() => signoutUserMethod()).not.toThrow();
// Put the real function back restore();
window.GOVUK.Modules.TimeoutPopup.signoutUser = originalSignoutUser;
}); });
test('expireUserSession method logs the user out with next query parameter', () => { test('expireUserSession method logs the user out with next query parameter', () => {
// Replace this function too so we can check it gets called const restore = mockWindowLocation();
const originalExpireUserSession = window.GOVUK.Modules.TimeoutPopup.expireUserSession;
const mockExpireUserSession = jest.fn(() => {
// Build the sign out URL just like the real function
const signOutLink = '/sign-out?next=' + window.location.pathname;
window.location.href = signOutLink;
});
window.GOVUK.Modules.TimeoutPopup.expireUserSession = mockExpireUserSession;
// Test the actual function, not a mock
const expireUserSessionMethod = window.GOVUK.Modules.TimeoutPopup.expireUserSession; const expireUserSessionMethod = window.GOVUK.Modules.TimeoutPopup.expireUserSession;
expireUserSessionMethod();
expect(mockExpireUserSession).toHaveBeenCalled(); // This will try to set location.href but our mock will catch it
expect(() => expireUserSessionMethod()).not.toThrow();
// Put the original function back restore();
window.GOVUK.Modules.TimeoutPopup.expireUserSession = originalExpireUserSession;
}); });
test('extendSession method reloads the page', () => { test('extendSession method reloads the page', () => {
// Same deal with the extend session function const restore = mockWindowLocation();
const originalExtendSession = window.GOVUK.Modules.TimeoutPopup.extendSession;
const mockExtendSession = jest.fn(() => {
// Just reload the page like the real function
window.location.reload();
});
window.GOVUK.Modules.TimeoutPopup.extendSession = mockExtendSession;
// Test the actual function, not a mock
const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession; const extendSessionMethod = window.GOVUK.Modules.TimeoutPopup.extendSession;
extendSessionMethod();
expect(mockExtendSession).toHaveBeenCalled(); // This will try to call location.reload but our mock will catch it
expect(() => extendSessionMethod()).not.toThrow();
// Clean up after ourselves restore();
window.GOVUK.Modules.TimeoutPopup.extendSession = originalExtendSession;
}); });
test('showTimer method shows the session timer modal', () => { test('showTimer method shows the session timer modal', () => {