Suppress JSDOM scrollTo and navigation error messages

This change stubs window.scrollTo and several window navigation
functions (window.location = '...', window.location.href = '...', and
window.location.reload()) with noops to suppress JSDOM messages
warning that these functions aren't implemented. E.g. these ones:

Error: Not implemented: scrollTo
Error: Not implemented: navigation (except hash changes)

While potentially useful if you're expecting those browser APIs to
work in a JSDOM environment, they noise up our Jest logs real good.

Test plan:
  - Tests still pass

Change-Id: I9166d0ee05273cc07069b62a1a041eeb335fbfe6
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/275919
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Robin Kuss <rkuss@instructure.com>
Reviewed-by: Charley Kline <ckline@instructure.com>
QA-Review: Robin Kuss <rkuss@instructure.com>
Product-Review: Jeff Largent <jeff.largent@instructure.com>
This commit is contained in:
Jeff Largent 2021-10-13 17:01:21 -04:00
parent 8126ba4d17
commit 67ecc41b76
1 changed files with 30 additions and 0 deletions

View File

@ -287,3 +287,33 @@ if (!('matchMedia' in window)) {
if (!('scrollIntoView' in window.HTMLElement.prototype)) {
window.HTMLElement.prototype.scrollIntoView = () => {}
}
// Suppress errors for APIs that exist in JSDOM but aren't implemented
Object.defineProperty(window, 'scrollTo', {configurable: true, writable: true, value: () => {}})
const locationProperties = Object.getOwnPropertyDescriptors(window.location)
Object.defineProperty(window, 'location', {
configurable: true,
enumerable: true,
get: () =>
Object.defineProperties(
{},
{
...locationProperties,
href: {
...locationProperties.href,
// Prevents JSDOM errors from doing window.location.href = ...
set: () => {}
},
reload: {
configurable: true,
enumerable: true,
writeable: true,
// Prevents JSDOM errors from doing window.location.reload()
value: () => {}
}
}
),
// Prevents JSDOM errors from doing window.location = ...
set: () => {}
})