Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if function is called react and enzyme

I am trying to test one of the methods in my react component. It is being called after a button click so I have the simulation in place with enzyme

 it('clone should call handleCloneClick when clicked', () => {
        const cloneButton = wrapper.find('#clone-btn');
        cloneButton.simulate('click');
 });

My component method is here:

_handleCloneClick(event) {
    event.preventDefault();
    event.stopPropagation();

    this.props.handleClone(this.props.user.id);
}

The _handleCloneClick is being called when the user clicks on the button thats in the simulation, how can I go about testing that its been called successfully?

like image 351
Munsterberg Avatar asked Nov 03 '16 03:11

Munsterberg


People also ask

How do you check if a function is called in React Jest?

To check if a component's method is called, we can use the jest. spyOn method to check if it's called. We check if the onclick method is called if we get the p element and call it.

Can we use both Enzyme and React testing library?

There are many React testing libraries on the market, but two of the most popular are React Testing Library (RTL) and Enzyme. Both of them are used for testing react components, and both provide the tools needed for testing some application, but they have two different approaches.

What is Enzyme testing React?

Enzyme is a JavaScript Testing utility for React that makes it easier to test your React Components' output. You can also manipulate, traverse, and in some ways simulate runtime given the output. Enzyme's API is meant to be intuitive and flexible by mimicking jQuery's API for DOM manipulation and traversal.

How do you test for Jest and enzymes?

Both Jest and Enzyme are meant to test the react applications. Jest can be used with any other Javascript framework, but Enzyme is meant to run on react only. Jest can be used without Enzyme, and snapshots can be created and tested perfectly fine. But the Enzyme adds additional functionality to it.


1 Answers

There are two options, either you should spy on _handleCloneClick of component's prototype, before you render the component:

export default class cloneButton extends Component {
  constructor(...args) {
    super(...args);
    this. _handleCloneClick = this. _handleCloneClick.bind(this);
  }

  _handleCloneClick() {
    event.preventDefault();
    event.stopPropagation();

    this.props.handleClone(this.props.user.id);
  }

  render() {
    return (<button onClick={this. _handleCloneClick}>Clone</button>);
  }
}

And in your test:

it('clone should call handleCloneClick when clicked', () => {
  sinon.spy(cloneButton.prototype, '_handleCloneClick');
  const wrapper = mount(<cloneButton/>);
  wrapper.find('#clone-btn').simulate('click');
  expect(spy).toHaveBeenCalled() //adept assertion to the tool you use
});

Or, you can try to set a spy after rendering the component and invoke wrapper.update() afterwards:

it('clone should call handleCloneClick when clicked', () => {      
  const wrapper = mount(<cloneButton/>);
  sinon.spy(wrapper.instance(), "_handleCloneClick");
  wrapper.update();
  wrapper.find('#clone-btn').simulate('click');
  expect(spy).toHaveBeenCalled() //adept assertion to the tool you use
});
like image 179
Alexandr Lazarev Avatar answered Oct 19 '22 12:10

Alexandr Lazarev