简体   繁体   中英

enzyme find and select first instance of element

I am trying to siumlate a click on the first of three boxes.

My react code looks like this

const Boxes = (props) => {

   return (
     <div className="container">
        <div onClick={props.showMessage} className="box">One</div>
        <div onClick={props.showMessage} className="box">Two</div>
        <div onClick={props.showMessage} className="box">Three</div>
     </div>
   );
}

Then my jest code looks like this

it("shows a message if Box One is selected", () => {
    let wrapper = shallow(
        <Boxes
            {...props}
        />
    );

    wrapper.find('.box').simulate('click');
    expect(wrapper.contains('Box One Clicked')).toEqual(true);

});

This gives me the following error message

Method “simulate” is only meant to be run on a single node. 3 found instead.

Then when I try the following it doesn't work either and the test fails.

wrapper.find('.box').first().simulate('click');
expect(wrapper.contains('Box One Clicked')).toEqual(true);

有多个.box项目,您必须选择其中一个,比如第一个:

wrapper.find('.box').at(0).simulate('click');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM