Jestjs 开玩笑地测试ScrollIntoView
使用scrollIntoView的函数Jestjs 开玩笑地测试ScrollIntoView,jestjs,Jestjs,使用scrollIntoView的函数 export const scrollDown = () => { document.querySelector('.bottom').scrollIntoView({ behavior: 'smooth' }); } 我这里有个测试是这样的 describe('scrollDown', () => { let scrollIntoViewMock = jest.fn(); window.H
export const scrollDown = () => {
document.querySelector('.bottom').scrollIntoView({
behavior: 'smooth'
});
}
我这里有个测试是这样的
describe('scrollDown', () => {
let scrollIntoViewMock = jest.fn();
window.HTMLElement.prototype.scrollIntoView = scrollIntoViewMock;
scrollDown()
expect(scrollIntoViewMock).toBeCalled();
})
但测试失败,出现TypeError:无法设置未定义的属性“scrollIntoView”
该测试来自另一个SO对scrollIntoView测试问题的回答。任何帮助都将不胜感激。您需要在
文档
中添加一个HTMLElement
和classbottom
:
const scrollDown=()=>{
document.querySelector('.bottom').scrollIntoView({
行为:“平滑”
});
}
测试('向下滚动',()=>{
document.body.innerHTML='';
const scrollIntoViewMock=jest.fn();
HTMLElement.prototype.scrollIntoView=scrollIntoViewMock;
向下滚动();
expect(scrollIntoViewMock).toBeCalledWith({behavior:'smooth'});//成功!
})