Problem with testing React functions "onClick" "onChange", etc
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.
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."
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.