【发布时间】:2017-10-01 19:06:51
【问题描述】:
我正在编写一个 JS 测试来检查图像的 css 类是否被正确应用。该类是根据图像的naturalHeight 属性计算的。但是因为这是一个JS测试,没有DOM,所以naturalHeight在测试中总是为0。如何使用 Jest 和 Enzyme 正确测试/模拟它?
组件的简化版:
class Image extends React.Component {
public render () {
return (
<img
src={this.props.src}
ref={el => this.imageElement = el }
className={ this._computeStyle() }/>
)
}
private _computeStyle() {
if (this.imageElement.naturalHeight > 100){
return 'big';
}
return 'small'
}
}
尝试测试:
it('has class "big" if > 100 pixels', () => {
let wrapper = mount(<Image src={largeImage} />);
expect(wrapper.find('img').hasClass('big')).toBe(true);
});
【问题讨论】:
标签: javascript reactjs enzyme jestjs