jest icon indicating copy to clipboard operation
jest copied to clipboard

Problem with testing React functions "onClick" "onChange", etc

Open ferrannp opened this issue 11 years ago • 2 comments

So I have a simple component with a checkbox:

<input
  type="checkbox"
  className="my-checkbox"
  onChange={this._toggle}
/>

and my function:

_toggle: function () {
  this._anotherFunction();
},

In Jest I have:

  it('should call toggle function', function ()
  {
    myComponent._toggle = jest.genMockFn();
    myComponent._anotherFunction = jest.genMockFn();

    var input = TestUtils.findRenderedDOMComponentWithClass(myComponent, "my-checkbox");
    TestUtils.Simulate.change(input);

    expect(myComponent._toggle).toBeCalled(); //Fails
    expect(myComponent._anotherFunction).toBeCalled(); //Succeed
  });

Is there any reason for the failure of _toggle.toBeCalled() ? It's actually been called.

ferrannp avatar Jan 27 '15 15:01 ferrannp

Possible duplicate of https://github.com/facebook/jest/issues/207

Quote from https://github.com/facebook/jest/issues/207: "When a method is called with an event listener in a React component, the original method is always called and not the mocked one."

abhinavrastogi avatar Jan 30 '15 06:01 abhinavrastogi

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar May 14 '21 16:05 github-actions[bot]