Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simulate for onClick not working in enzyme

This is a cancel button

<div className="cancelFileBtn" onClick={this.props.cancelFileSending}>

I need to simulate its click,I tried the following test

wrapper.find('.cancelFileBtn').simulate('click');

But the click function is still undefined...Did I miss anything else? and it will be very helpful if anyone can mention any changes if exist in simulating

<SendMessageButton onClick={this.props.handleClickSendMessage} loadingFile={this.props.loadingFile}/>
like image 303
Thomas John Avatar asked Sep 22 '16 11:09

Thomas John


1 Answers

Can't tell much without seeing more codes, hope this helps:

const wrapper = mount(<Component />);
const cancelBtn = wrapper.find('.cancelFileBtn');

// Test that the button is truthy
expect(cancelBtn).to.have.length(1);

// Simulation
cancelBtn.simulate('click');
// or
cancelBtn.props().onClick();

// Test the output
expect(...).to.equal(...);
like image 181
Jee Mok Avatar answered Sep 20 '22 16:09

Jee Mok