Gle*_*leb 10 reactjs jestjs reactjs-testutils
我的组件中有以下代码
var rect = ReactDOM.findDOMNode(this).getBoundingClientRect();
Run Code Online (Sandbox Code Playgroud)
我在组件中使用d3js并渲染图形.但是当我运行测试时,有任何svg标签.我认为它发生是因为所有rect的字段等于0.
这是浏览器中console.log(rect)的输出:
ClientRect {top:89,right:808,bottom:689,left:8,width:800 ...}
当我运行测试时:
{bottom:0,height:0,left:0,right:0,top:0,width:0}
那么有没有办法设置元素的大小?
alu*_*yov 25
我的解决方案是模拟getBoundingClientRect
(我目前正在使用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);
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6364 次 |
最近记录: |