一位同事实现了一个 ScrollToTop 组件,它有:
componentDidMount() {
window && window.scrollTo && window.scrollTo(0, 0)
}
在某些情况下简单地将页面滚动到顶部。
但是,在运行测试时,测试会定期出错,并显示未实现:window.scrollTo(底部的完整错误堆栈)。
我知道 window.scrollTo 在节点上下文或开玩笑测试中不存在。如何抑制在非浏览器上下文中滚动的尝试?或者我应该如何开玩笑地处理这个全局?
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Not implemented: window.scrollTo
at module.exports (/src/argos-r/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
at /src/argos-r/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/Window.js:594:7
at ScrollToTop.scrollTo [as componentDidMount] (/src/argos-r/src/components/ScrollToTop.js:6:45)
at commitLifeCycles (/src/argos-r/node_modules/react-dom/cjs/react-dom.development.js:17334:22)
at commitAllLifeCycles (/src/argos-r/node_modules/react-dom/cjs/react-dom.development.js:18736:7)
at HTMLUnknownElement.callCallback (/src/argos-r/node_modules/react-dom/cjs/react-dom.development.js:149:14)
at invokeEventListeners (/src/argos-r/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:193:27)
at HTMLUnknownElementImpl._dispatch (/src/argos-r/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
at HTMLUnknownElementImpl.dispatchEvent (/src/argos-r/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
at HTMLUnknownElementImpl.dispatchEvent (/src/argos-r/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/nodes/HTMLElement-impl.js:30:27) undefined
console.error node_modules/react-dom/cjs/react-dom.development.js:506