simulate for onClick not working in enzyme

Thomas John picture Thomas John · Sep 22, 2016 · Viewed 11.7k times · Source

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}/>

Answer

Jee Mok picture Jee Mok · Jan 25, 2018

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(...);