React Test Renderer Simulating Clicks on Elements

前端 未结 2 935
太阳男子
太阳男子 2021-01-07 22:32

I\'m testing a React component using Jest v16.0.1, react-test-renderer v15.4.0 and react-addons-test-utils v15.4.0.

The component has rendered a button:



        
2条回答
  •  心在旅途
    2021-01-07 22:58

    I have found a solution. Since you are using react, I assume that the onClick handler function is passed to the button as a part of the props. So you can access it through button's props.

    component.root.findByType('button').props.onClick();
    

    Or if you have more than one button, you can do this:

    component.root.findByProps({ className: "btn btn-lg btn-primary btn-danger" }).props.onClick();
    

提交回复
热议问题