如何使用 jest+enzyme 在 react-native 中选择一个元素(文本、视图)(主要是当文本/视图嵌套在组件的深处时)?

Posted

技术标签:

【中文标题】如何使用 jest+enzyme 在 react-native 中选择一个元素(文本、视图)(主要是当文本/视图嵌套在组件的深处时)?【英文标题】:How to select an element (Text, View) in react-native with jest+enzyme (mostly when Text/View are nested deep-down in the component)? 【发布时间】:2021-12-26 14:26:34 【问题描述】:

我知道酶中有一种叫做find 的东西,但我想知道如何通过某些属性选择反应原生元素

【问题讨论】:

【参考方案1】:

您可以通过id等任何属性选择它,例如,

用于在testing.js中选择以下文本

<Text id="myid">hello </Text>

您可以执行以下操作

it('selects Text with id attr', () => 
  let wrapper = shallow(<Testing />);
  let text = wrapper.findWhere(node => node.name() === 'Text' && node.prop('id') === 'myid');
  console.log(text.debug());
)

【讨论】:

以上是关于如何使用 jest+enzyme 在 react-native 中选择一个元素(文本、视图)(主要是当文本/视图嵌套在组件的深处时)?的主要内容,如果未能解决你的问题,请参考以下文章