【发布时间】:2018-04-25 19:46:59
【问题描述】:
我有一个组件在某些条件下在渲染中返回 null:
render() {
if (this.props.isHidden) {
return null;
}
return <div>test</div>;
}
我想用 jest 和酵素检查当 isHidden 为真时组件是否为空:
describe('myComp', () => {
it('should not render if isHidden is true', () => {
const comp = shallow(<myComp isHidden={true} />);
expect(comp.children().length).toBe(0);
});
});
这可行,但有没有更惯用的方法来编写这个测试?测试呈现为 null 的组件是很常见的场景。
【问题讨论】:
标签: javascript reactjs jestjs enzyme