[英]|ReactNative|-Conditional rendering ?: with Jest && Enzyme
下午好,我有一个结构像这样的组件文件:
class Component ...
render(){
const {array} = this.props
{!array.includes(value) ?
(<View ...props
id="myComponent"/>
....
</View>) :
(<View ...props
id="myOtherComponent"/>
....
</View>)
}
}
在我的测试文件中,我正在做类似的事情:
describe('Testing Component', () => {
test('conditional rendering', () => {
const wrapper = shallow(<Component array={[value]}/>);
expect(wrapper.find(n => n.prop('id') === "myOtherComponent").exists(true))
});
});
但是,即使我修改了为数组发送的props,它也总是返回true
……用于检查嵌套组件是否已实际验证和呈现的关键字是什么?
我认为错误在于您的expect
参数。
findWhere
函数代替find
; exists
方法不应接收参数,因为它仅接收酶的选择器而不是布尔值(您可以在此处了解更多信息); toBeTruthy
调用添加到expect
行。 这是一种与您类似的情况,我们已经对其进行了测试,并且效果很好:
it('tests name', () => {
const mockComponent = shallow(<Component {...props} />);
const textNode = mockComponent.findWhere(n => n.text() === props.name);
expect(textNode.exists()).toBeTruthy();
});
因此,您的测试最终将如下所示:
describe('Testing Component', () => {
test('conditional rendering', () => {
const wrapper = shallow(<Component array={[value]}/>);
const node = wrapper.findWhere(n => n.prop('id') === 'myOtherComponent');
expect(node.exists()).toBeTruthy();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.