diff --git a/spec/javascripts/packages/document-capture/components/acuant-capture-spec.jsx b/spec/javascripts/packages/document-capture/components/acuant-capture-spec.jsx index 129e0f6c042..8c3b60f8aff 100644 --- a/spec/javascripts/packages/document-capture/components/acuant-capture-spec.jsx +++ b/spec/javascripts/packages/document-capture/components/acuant-capture-spec.jsx @@ -125,7 +125,6 @@ describe('document-capture/components/acuant-capture', () => { const button = await findByText('doc_auth.buttons.upload_picture'); expect(button.classList.contains('usa-button--outline')).to.be.true(); - expect(console).to.have.loggedError(/^Error: Could not load script:/); userEvent.click(button); }); diff --git a/spec/javascripts/spec_helper.js b/spec/javascripts/spec_helper.js index 6165cecea00..2010ddd2818 100644 --- a/spec/javascripts/spec_helper.js +++ b/spec/javascripts/spec_helper.js @@ -17,10 +17,15 @@ chai.use(dirtyChai); global.expect = chai.expect; // Emulate a DOM, since many modules will assume the presence of these globals exist as a side -// effect of their import (focus-trap, classList.js, etc). A URL is provided as a prerequisite to -// managing history API (pushState, etc). +// effect of their import. const dom = createDOM(); global.window = dom.window; +const windowGlobals = Object.fromEntries( + Object.getOwnPropertyNames(window) + .filter((key) => !(key in global)) + .map((key) => [key, window[key]]), +); +Object.assign(global, windowGlobals); global.window.fetch = () => Promise.reject(new Error('Fetch must be stubbed')); global.window.crypto = new Crypto(); // In the future (Node >=15), use native webcrypto: https://nodejs.org/api/webcrypto.html global.window.URL.createObjectURL = createObjectURLAsDataURL; @@ -30,13 +35,6 @@ Object.defineProperty(global.window.Image.prototype, 'src', { this.onload(); }, }); -global.navigator = window.navigator; -global.document = window.document; -global.Document = window.Document; -global.Element = window.Element; -global.Node = window.Node; -global.getComputedStyle = window.getComputedStyle; -global.self = window; useCleanDOM(dom); useConsoleLogSpy(); diff --git a/spec/javascripts/support/dom.js b/spec/javascripts/support/dom.js index 32d64720ee2..215eee1e84c 100644 --- a/spec/javascripts/support/dom.js +++ b/spec/javascripts/support/dom.js @@ -27,7 +27,6 @@ export function createDOM() { : Promise.reject(new Error('Failed to load')); } })(), - runScripts: 'dangerously', }); // JSDOM doesn't implement `offsetParent`, which is used by some third-party libraries to detect @@ -61,6 +60,24 @@ export function createDOM() { .stub(dom.window, 'scrollTo') .callsFake((scrollX, scrollY) => Object.assign(dom.window, { scrollX, scrollY })); + // If a script tag is added to the page, execute its callbacks as a successful or failed load, + // based on whether the `src` is `about:blank`. + new dom.window.MutationObserver((mutations) => { + mutations.forEach((mutation) => { + mutation.addedNodes.forEach((node) => { + if (node instanceof dom.window.HTMLScriptElement) { + if (node.src === 'about:blank') { + if (typeof node.onload === 'function') { + node.onload(); + } + } else if (typeof node.onerror === 'function') { + node.onerror(); + } + } + }); + }); + }).observe(dom.window.document.body, { childList: true, subtree: true }); + return dom; }