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

react component mock method not called #4696

Closed
bchenSyd opened this issue Oct 14, 2017 · 6 comments
Closed

react component mock method not called #4696

bchenSyd opened this issue Oct 14, 2017 · 6 comments

Comments

@bchenSyd
Copy link
Contributor

everything is latest

    "jest": "^21.1.0",
    "react": "^16.0.0",
    "react-dom": "^16.0.0"
    "react-test-renderer": "^16.0.0",
    "enzyme": "^3.1.0",
    "enzyme-adapter-react-16": "^1.0.1",

I have a component and a test like below

//component
class ToggleCheckbox  extends Component{
   onChange=()=>{}
   render(){
      return <div> <button onClick={this.onChange} >click</button> </div> 
   }
}

//test
  it('spy', () => {
    //arrange
    const spy = jest.fn().mockImplementation( ()=>{
      console.log(`spy called`);
    })
    const wrapper = shallow(<ToggleCheckbox />);   
    wrapper.instance().onChange = spy
    wrapper.update(); //Forces a re-render.
    //act
    // wrapper.instance().onChange()
    wrapper.find('button').simulate('click');

    //assert
    expect(spy).toBeCalled(); // this will fail but I don't know why;
  });

the assertion will fail but I don't know why.

I know i'm testing jest , or enzyme here rather than testing my code -- as we are doing shallow and no event gets fired in the test; but I want to know what is going on under the hood;
thanks

@bchenSyd
Copy link
Contributor Author

I actually tried the mount cases well. it didn't work either

@cpojer
Copy link
Member

cpojer commented Oct 15, 2017

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions. Thank you :)

@cpojer cpojer closed this as completed Oct 15, 2017
@bchenSyd
Copy link
Contributor Author

hi chris,
would you mind to have a look at the test in my questoin? it's quite a basic one and won't take you more than 5 minutes that I can guarantee . it's quite a common test case but I can't find an answer from google.
thanks

@bchenSyd
Copy link
Contributor Author

enzymejs/enzyme#944

@bchenSyd
Copy link
Contributor Author

wrapper.setProps({ term: '36' });
Garath

@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 13, 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

2 participants