我有一个组件在某些条件下在渲染中返回 null:
render() {
if (this.props.isHidden) {
return null;
}
return <div>test</div>;
}
我想用 Jest 和 enzyme 检查当 isHidden 为 true 时组件是否为空:
describe('myComp', () => {
it('should not render if isHidden is true', () => {
const comp = shallow(<myComp isHidden={true} />);
expect(comp.children().length).toBe(0);
});
});
这可行,但是有没有更惯用的方法来编写这个测试?测试呈现为 null 的组件是很常见的情况。
最佳答案
ShallowWrapper
有一个 isEmptyRender()
功能:
expect(comp.isEmptyRender()).toBe(true)
关于javascript - Jest Enzyme 测试在 render 方法中返回 null 的 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47259061/