我有一个 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);
})
弄清楚了:
Object.defineProperty(HTMLElement.prototype, 'scrollHeight', { configurable: true, value: 500 })
Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, value: 10 })
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)