Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Enzyme find second (or nth) node

What you want is the .at(index) method: .at(index) .

So, for your example:

expect(component.find('MyInnerComponent').at(1)).toHaveProp('title', 'Good-bye');


If you are to test certain things on each one also consider iterating through the matched set:

component.find('MyInnerComponent').forEach( (node) => {
    expect(node.prop('title')).toEqual('Good-bye')
})

 const component = wrapper.find('MyInnerComponent').at(1); 
 //at(1) index of element 0 to ~

 expect(component.prop('title')).to.equal('Good-bye');

TL;DR;

use findAll along with .at(1)

const innerComponent = component.findAll('MyInnerComponent').at(1);
expect(innerComponent).toHaveProp('title', 'Good-bye');