我有一个名为Foo
的组件,render
函数是这样的:
render() {
if (!this.props.something) {
return null;
}
return (
<div>
<Bar />
<Baz />
</div>
);
}
我想测试Foo
组件以查看是否呈现了div
:
test('is div rendered', () => {
const component = shallow(
<Foo something={false} />
);
expect(component.find('div').at(0).exists()).toBeFalsy();
});
但测试结果为true
。如何使用shallow
渲染方法测试div
是否存在?
答案 0 :(得分:1)
当你使用.find()。at()时,它会每次都返回一个对象。期望断言应该是这样的:
expect(component.find('div').node).toBe(undefined);