更新时间:2023-11-24 17:04:58
我的解决方案是模拟getBoundingClientRect
(我目前正在使用jest 16.0.1)
My solution is to mock getBoundingClientRect
(I'm currently using jest 16.0.1)
describe('Mock `getBoundingClientRect`', () => {
beforeEach(() => {
Element.prototype.getBoundingClientRect = jest.fn(() => {
return {
width: 120,
height: 120,
top: 0,
left: 0,
bottom: 0,
right: 0,
}
});
});
it('should mock `getBoundingClientRect`', () => {
const element = document.createElement('span');
const rect = element.getBoundingClientRect();
expect(rect.width).toEqual(120);
});
});