我有一个组件在特定条件下在渲染时返回null:
render() {
if (this.props.isHidden) {
return null;
}
return <div>test</div>;
}
当isHidden在jest和Ezyme中为true时,我想判断组件是否为null:
describe('myComp', () => {
it('should not render if isHidden is true', () => {
const comp = shallow(<myComp isHidden={true} />);
expect(comp.children().length).toBe(0);
});
});
这是可行的,但是有没有更惯用的方法来编写这个测试呢?测试呈现为null的组件是一种非常常见的场景.