mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-14 02:48:45 -04:00
Update tests to use most recent jest and supporting libraries
This commit is contained in:
@@ -99,9 +99,6 @@ describe('Authenticate with security key', () => {
|
||||
})
|
||||
|
||||
test('authenticates and passes a redirect url through to the authenticate admin endpoint', (done) => {
|
||||
// https://github.com/facebook/jest/issues/890#issuecomment-415202799
|
||||
window.history.pushState({}, 'Test Title', '/?next=%2Ffoo%3Fbar%3Dbaz')
|
||||
|
||||
jest.spyOn(window, 'fetch')
|
||||
.mockImplementationOnce((_url) => {
|
||||
// initial fetch of options from the server
|
||||
@@ -131,11 +128,11 @@ describe('Authenticate with security key', () => {
|
||||
.mockImplementationOnce((url, options = {}) => {
|
||||
// subsequent POST of credential data to server
|
||||
expect(url.toString()).toEqual(
|
||||
'https://www.notifications.service.gov.uk/webauthn/authenticate?next=%2Ffoo%3Fbar%3Dbaz'
|
||||
'https://www.notifications.service.gov.uk/webauthn/authenticate'
|
||||
)
|
||||
return Promise.resolve({
|
||||
ok: true, arrayBuffer: () => Promise.resolve(window.CBOR.encode({ redirect_url: '/foo' }))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
jest.spyOn(window.location, 'assign').mockImplementation((href) => {
|
||||
|
||||
@@ -28,7 +28,7 @@ describe("Cookie message", () => {
|
||||
document.getElementsByTagName('head')[0].appendChild(cookieMessageStyles);
|
||||
|
||||
// protect against any previous tests setting a cookies-policy cookie
|
||||
helpers.deleteCookie('cookies-policy');
|
||||
helpers.deleteCookie('cookies_policy');
|
||||
|
||||
});
|
||||
|
||||
@@ -92,7 +92,7 @@ describe("Cookie message", () => {
|
||||
|
||||
});
|
||||
|
||||
/*
|
||||
/*
|
||||
Note: If no JS, the cookie banner is hidden.
|
||||
|
||||
This works through CSS, based on the presence of the `js-enabled` class on the <body> so is not tested here.
|
||||
@@ -198,7 +198,7 @@ describe("Cookie message", () => {
|
||||
|
||||
expect(banner.is('hidden')).toBe(true);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
test("The consent cookie should be set, with analytics set to 'true'", () => {
|
||||
|
||||
@@ -242,7 +242,7 @@ describe("Cookie message", () => {
|
||||
|
||||
expect(banner.is('hidden')).toBe(true);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
test("The consent cookie should be set, with analytics set to 'false'", () => {
|
||||
|
||||
|
||||
@@ -13,12 +13,11 @@ describe('Enhanced textbox', () => {
|
||||
let input;
|
||||
let textarea;
|
||||
let backgroundEl;
|
||||
const stylesheet = document.createElement('style');
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
// set some default styling
|
||||
const stylesheet = document.createElement('style');
|
||||
|
||||
stylesheet.innerHTML = ".textbox-highlight-textbox { padding: 2px; width: 576px; border-width: 1px; }";
|
||||
stylesheet.innerHTML += "textarea.textbox-highlight-textbox { height: 224px; }";
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
module.exports = {
|
||||
setupFiles: ['./support/setup.js'],
|
||||
testURL: 'https://www.notifications.service.gov.uk',
|
||||
testEnvironment: 'jsdom',
|
||||
testEnvironmentOptions: {
|
||||
url: 'https://www.notifications.service.gov.uk',
|
||||
},
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ describe('Prevent duplicate form submissions', () => {
|
||||
|
||||
let form;
|
||||
let button;
|
||||
let formSubmitSpy;
|
||||
let formEventSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -26,7 +26,7 @@ describe('Prevent duplicate form submissions', () => {
|
||||
button = document.querySelector('button');
|
||||
|
||||
// requires a helper due to JSDOM not implementing the submit method
|
||||
formSubmitSpy = helpers.spyOnFormSubmit(jest, form);
|
||||
formEventSpy = helpers.spyOnFormSubmitEventPrevention(jest, form);
|
||||
|
||||
require('../../app/assets/javascripts/preventDuplicateFormSubmissions.js');
|
||||
|
||||
@@ -40,7 +40,7 @@ describe('Prevent duplicate form submissions', () => {
|
||||
// the module cache needs resetting each time for the script to execute
|
||||
jest.resetModules();
|
||||
|
||||
formSubmitSpy.mockClear();
|
||||
formEventSpy.mockClear();
|
||||
|
||||
});
|
||||
|
||||
@@ -49,7 +49,7 @@ describe('Prevent duplicate form submissions', () => {
|
||||
helpers.triggerEvent(button, 'click');
|
||||
helpers.triggerEvent(button, 'click');
|
||||
|
||||
expect(formSubmitSpy.mock.calls.length).toEqual(1);
|
||||
expect(formEventSpy.mock.calls.length).toEqual(1);
|
||||
|
||||
});
|
||||
|
||||
@@ -61,7 +61,7 @@ describe('Prevent duplicate form submissions', () => {
|
||||
|
||||
helpers.triggerEvent(button, 'click');
|
||||
|
||||
expect(formSubmitSpy.mock.calls.length).toEqual(2);
|
||||
expect(formEventSpy.mock.calls.length).toEqual(0);
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -23,5 +23,5 @@ exports.templatesAndFoldersCheckboxes = html.templatesAndFoldersCheckboxes;
|
||||
exports.element = elements.element;
|
||||
exports.WindowMock = rendering.WindowMock;
|
||||
exports.ScreenMock = rendering.ScreenMock;
|
||||
exports.spyOnFormSubmit = forms.spyOnFormSubmit;
|
||||
exports.spyOnFormSubmitEventPrevention = forms.spyOnFormSubmitEventPrevention;
|
||||
exports.getFormDataFromPairs = utilities.getFormDataFromPairs;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
// helper for spying on the submit method on a form element
|
||||
// JSDOM's implementation of submit just wraps a 'not implemented' error so we need to mock that to track calls to it
|
||||
// helper for spying on the submit method on a form element, via the event's `preventDefault` method
|
||||
// JSDOM's implementation of requestSubmit triggers the submit event but then throws an error unless the submit handler returns false
|
||||
//
|
||||
// * Remove when JSDOM implements submit on its form elements *
|
||||
//
|
||||
@@ -11,7 +11,9 @@
|
||||
//
|
||||
// form elements link to their implementation instance via a symbol property
|
||||
// this spies on the submit method of the implementation instance for a form element and mocks it to prevent 'not implemented' errors
|
||||
function spyOnFormSubmit (jest, form) {
|
||||
// it returns a spy on the event preventDefault function because submit is called every time, no matter if they submission will happen or not
|
||||
|
||||
function spyOnFormSubmitEventPrevention (jest, form) {
|
||||
|
||||
const formImplementationSymbols = Object.getOwnPropertySymbols(form).filter(
|
||||
symbol => form[symbol].constructor.name === 'HTMLFormElementImpl'
|
||||
@@ -23,11 +25,15 @@ function spyOnFormSubmit (jest, form) {
|
||||
|
||||
const HTMLFormElementImpl = form[formImplementationSymbols[0]];
|
||||
|
||||
const submitSpy = jest.spyOn(HTMLFormElementImpl, 'submit')
|
||||
const event = new Event("submit", {bubbles: true, cancelable: true})
|
||||
const preventDefaultSpy = jest.spyOn(event, 'preventDefault')
|
||||
|
||||
submitSpy.mockImplementation();
|
||||
return submitSpy;
|
||||
const submitSpy = jest.spyOn(HTMLFormElementImpl, 'requestSubmit')
|
||||
submitSpy.mockImplementation(() => {
|
||||
form.dispatchEvent(event)
|
||||
});
|
||||
|
||||
return preventDefaultSpy;
|
||||
};
|
||||
|
||||
exports.spyOnFormSubmit = spyOnFormSubmit;
|
||||
exports.spyOnFormSubmitEventPrevention = spyOnFormSubmitEventPrevention;
|
||||
|
||||
@@ -1,53 +1,15 @@
|
||||
// Polyfills for any parts of the DOM API available in browsers but not JSDOM
|
||||
|
||||
// From: https://gist.github.com/eligrey/1276030
|
||||
HTMLElement.prototype.insertAdjacentHTML = function(position, html) {
|
||||
"use strict";
|
||||
|
||||
var
|
||||
ref = this
|
||||
, container = ref.ownerDocument.createElementNS("http://www.w3.org/1999/xhtml", "_")
|
||||
, ref_parent = ref.parentNode
|
||||
, node, first_child, next_sibling
|
||||
;
|
||||
|
||||
container.innerHTML = html;
|
||||
|
||||
switch (position.toLowerCase()) {
|
||||
case "beforebegin":
|
||||
while ((node = container.firstChild)) {
|
||||
ref_parent.insertBefore(node, ref);
|
||||
}
|
||||
break;
|
||||
case "afterbegin":
|
||||
first_child = ref.firstChild;
|
||||
while ((node = container.lastChild)) {
|
||||
first_child = ref.insertBefore(node, first_child);
|
||||
}
|
||||
break;
|
||||
case "beforeend":
|
||||
while ((node = container.firstChild)) {
|
||||
ref.appendChild(node);
|
||||
}
|
||||
break;
|
||||
case "afterend":
|
||||
next_sibling = ref.nextSibling;
|
||||
while ((node = container.lastChild)) {
|
||||
next_sibling = ref_parent.insertBefore(node, next_sibling);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// from: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentText#Polyfill
|
||||
if (!Element.prototype.insertAdjacentText) {
|
||||
Element.prototype.insertAdjacentText = function(type, txt){
|
||||
this.insertAdjacentHTML(
|
||||
type,
|
||||
(txt+'') // convert to string
|
||||
.replace(/&/g, '&') // embed ampersand symbols
|
||||
.replace(/</g, '<') // embed less-than symbols
|
||||
)
|
||||
}
|
||||
let _location = {
|
||||
reload: jest.fn(),
|
||||
hostname: "www.notifications.service.gov.uk",
|
||||
assign: jest.fn(),
|
||||
href: "https://www.notifications.service.gov.uk",
|
||||
}
|
||||
|
||||
Object.defineProperty(window, 'location', {
|
||||
get: () => _location,
|
||||
set: (value) => {
|
||||
_location = value
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const each = require('jest-each').default;
|
||||
const jestDateMock = require('jest-date-mock');
|
||||
|
||||
const helpers = require('./support/helpers.js');
|
||||
|
||||
@@ -22,7 +21,7 @@ beforeAll(() => {
|
||||
jqueryAJAXReturnObj = {
|
||||
done: callback => {
|
||||
// The server takes 1 second to respond
|
||||
jestDateMock.advanceBy(1000);
|
||||
jest.advanceTimersByTime(1000);
|
||||
callback(responseObj);
|
||||
return jqueryAJAXReturnObj;
|
||||
},
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const each = require('jest-each').default;
|
||||
const jestDateMock = require('jest-date-mock');
|
||||
|
||||
const helpers = require('./support/helpers.js');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user