React Enzyme find second (or nth) node

后端 未结 3 1182
礼貌的吻别
礼貌的吻别 2021-01-30 11:58

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

Simplified here for the purposes of this question...

function MyOuterComponent() {
         


        
相关标签:
3条回答
  • 2021-01-30 12:30

    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')
    })
    
    0 讨论(0)
  • 2021-01-30 12:34
     const component = wrapper.find('MyInnerComponent').at(1); 
     //at(1) index of element 0 to ~
    
     expect(component.prop('title')).to.equal('Good-bye');
    
    0 讨论(0)
  • 2021-01-30 12:57

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

    So, for your example:

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

    0 讨论(0)
提交回复
热议问题