Jest Enzyme测试一个在render方法中返回null的React组件 [英] Jest Enzyme test a React component that returns null in render method
本文介绍了Jest Enzyme测试一个在render方法中返回null的React组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个在某些条件下在渲染中返回null的组件:
I have a component that returns null in render under certain conditions:
render() {
if (this.props.isHidden) {
return null;
}
return <div>test</div>;
}
我想检查当isHidden为真且jest时组件是否为null酶:
I want to check if the component is null when isHidden is true with jest and enzyme:
describe('myComp', () => {
it('should not render if isHidden is true', () => {
const comp = shallow(<myComp isHidden={true} />);
expect(comp.children().length).toBe(0);
});
});
这有效,但有没有更惯用的方式来编写这个测试?测试呈现为null的组件是非常常见的情况。
This works but is there a more idiomatic way to write this test ? Testing for components that render as null is quite a common scenario.
推荐答案
expect(comp.type()).toEqual(null)
就是这样!
或: expect(comp.get(0))。toBeFalsy()
这篇关于Jest Enzyme测试一个在render方法中返回null的React组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文