Simulate for onClick does not work in the enzyme

This is a cancel button.

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

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

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

But the click function is still undefined ... Am I missing anything else? and it will be very useful if anyone can point out any changes if they exist in the simulation

<SendMessageButton onClick={this.props.handleClickSendMessage} loadingFile={this.props.loadingFile}/>
+4
source share
1 answer

Can't learn 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(...);
0
source

Source: https://habr.com/ru/post/1655466/


All Articles