mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-09 19:04:33 -04:00
More tests fixed
This commit is contained in:
66
tests/javascripts/support/polyfills.js
Normal file
66
tests/javascripts/support/polyfills.js
Normal file
@@ -0,0 +1,66 @@
|
||||
// Fixes for browser features that JSDOM doesn't support or handles differently
|
||||
//
|
||||
// Jest 30 was a bit of a game changer for how window.location works in tests.
|
||||
// The old tricks we used to use for mocking location don't work anymore, but
|
||||
// honestly the new approach is cleaner once you get used to it.
|
||||
|
||||
// Stop JSDOM from complaining about navigation attempts
|
||||
const originalConsoleError = console.error;
|
||||
console.error = function(message, ...args) {
|
||||
if (typeof message === 'object' && message.message && message.message.includes('Not implemented: navigation')) {
|
||||
return; // Just ignore these, they're not helpful in tests
|
||||
}
|
||||
if (typeof message === 'string' && message.includes('Not implemented: navigation')) {
|
||||
return; // Just ignore these, they're not helpful in tests
|
||||
}
|
||||
originalConsoleError.apply(console, [message, ...args]);
|
||||
};
|
||||
|
||||
// A helper for tests that need to fake window.location behavior
|
||||
global.mockWindowLocation = function(mockValues = {}) {
|
||||
const originalLocation = window.location;
|
||||
|
||||
// Jest 30 won't let us mess with href directly, so we work around it
|
||||
let hrefAssignments = [];
|
||||
let currentHref = mockValues.href || 'https://beta.notify.gov/';
|
||||
|
||||
// Build a fake location object that behaves like the real thing
|
||||
const mockLocation = {
|
||||
href: currentHref,
|
||||
pathname: mockValues.pathname || '/',
|
||||
search: '',
|
||||
hash: '',
|
||||
host: 'beta.notify.gov',
|
||||
hostname: 'beta.notify.gov',
|
||||
protocol: 'https:',
|
||||
port: '',
|
||||
origin: 'https://beta.notify.gov',
|
||||
assign: mockValues.assign || jest.fn(),
|
||||
reload: mockValues.reload || jest.fn(),
|
||||
replace: mockValues.replace || jest.fn(),
|
||||
toString: () => currentHref,
|
||||
...mockValues
|
||||
};
|
||||
|
||||
// Make href track changes when code tries to navigate
|
||||
Object.defineProperty(mockLocation, 'href', {
|
||||
get() { return currentHref; },
|
||||
set(value) {
|
||||
currentHref = value;
|
||||
hrefAssignments.push(value);
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true
|
||||
});
|
||||
|
||||
// Swap out the real location for our fake one
|
||||
delete window.location;
|
||||
window.location = mockLocation;
|
||||
|
||||
// Return a function to put everything back when the test is done
|
||||
return () => {
|
||||
delete window.location;
|
||||
window.location = originalLocation;
|
||||
return { hrefAssignments, currentHref };
|
||||
};
|
||||
};
|
||||
@@ -1,8 +1,11 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Set up jQuery
|
||||
// Fill in the gaps where JSDOM doesn't quite match real browsers
|
||||
require('./polyfills.js');
|
||||
|
||||
// Make jQuery available everywhere
|
||||
global.$ = global.jQuery = require('jquery');
|
||||
|
||||
// Load module code
|
||||
// Bring in the GOV.UK modules system
|
||||
require('govuk_frontend_toolkit/javascripts/govuk/modules.js');
|
||||
|
||||
Reference in New Issue
Block a user