sfletche sfletche - 1 year ago 134
React JSX Question

React Enzyme find second (or nth) node

I'm testing a React component with Jasmine Enzyme shallow rendering.

Simplified here for the purposes of this question...

function MyOuterComponent() {
return (
<MyInnerComponent title="Hello" />
<MyInnerComponent title="Good-bye" />

has 2 instances of
and I'd like to test the props on each one.

The first one I know how to test. I use

expect(component.find('MyInnerComponent').first()).toHaveProp('title', 'Hello');

However, I'm struggling to test the second instance of

I was hoping something like this would work...

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

or even this...

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

But of course neither of the above work.

I feel like I'm missing the obvious.

But when I look through the docs I don't see an analogous example.


tom tom
Answer Source

What you want is the .at(index) method: http://airbnb.io/enzyme/docs/api/ShallowWrapper/at.html.

So, for your example:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download