react-nativejestjsenzymejest-enzyme

How to select an element (Text, View) in react-native with jest+enzyme (mostly when Text/View are nested deep-down in the component)?


I know of something called find in enzyme, but I'd like to know how to select a react-native element by some attribute


Solution

  • You can select it by any attribute like id, e.g,

    for selecting the following Text in testing.js

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

    you can do the following

    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());
    })