Update tests to use most recent jest and supporting libraries

This commit is contained in:
Ryan Ahearn
2022-10-27 11:12:39 -04:00
parent 98b772f959
commit bb2d57b27b
14 changed files with 1080 additions and 109 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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, '&lt;') // 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
},
})