Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ferrannp opened this issue Jan 27, 2015 · 2 comments
Closed

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

ferrannp opened this issue Jan 27, 2015 · 2 comments

Comments

@ferrannp
Copy link
Contributor

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.

@abhinavrastogi
Copy link

Possible duplicate of #207

Quote from #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."

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants