【发布时间】:2021-09-27 06:49:00
【问题描述】:
我有一个 javascript 函数来检查是否是 html
元素,el,通过检查是一定的大小:
function isOverflow(element: string): boolean {
const el = document.getElementById(element);
return el.scrollHeight > el.clientHeight
}
我想测试我的功能。如何设置或模拟 scrollHeight 和 clientHeight?
it('test', () => {
const el = document.createElement("p")
el.setAttribute("id", "overflow")
//How to mock these? This doesn't work "Cannot assign to 'scrollHeight' because it is a read-only property"
el.clientHeight = 2;
el.scrollHeight = 1;
expect(component.isOverflow("overflow")).toBe(true);
})
【问题讨论】:
标签: javascript html typescript jestjs mocking