score:6
the first block fails because wrapper.instance().handlesubmit is not a jest mock function; it is whatever the class method defines it as.
the second block fails because handlesubmit, while it is a jest mock function, is not tied to your wrapper component at all. it is a local variable. when you simulate the click, it is again calling the actual implementation.
in order to accomplish what you are trying to do, you have to do something like this
it('handlessubmit when submit button is clicked', () => {
const handlesubmit = jest.fn();
wrappercomponent.prototype.handlesubmit = handlesubmit;
const wrapper = shallow(<wrappercomponent />);
wrapper.find(button).simulate('click');
expect(handlesubmit).tohavebeencalled();
})
where wrappercomponent is the component you are testing.
the above ought to work, but you can sometimes accomplish something similar in a nicer way. depending on your component's implementation, it is oftentimes easier to test that the functionality in your handlesubmit method is called rather than the handlesubmit method itself. for instance, if my component was something like
class testcomponent extends react.component {
constructor(props) {
super(props)
this.state = { clicked: false }
this.onclick = this.onclick.bind(this)
}
onclick() {
this.props.onclick()
this.setstate({ clicked: true })
}
render() {
return (
<button onclick={ this.onclick }>
{ 'click me' }
</button>
)
}
}
i could test it by doing
it('calls onclick props and sets clicked state to true when clicked', () => {
const onclick = jest.fn();
const testcomp = shallow(<testcomponent onclick={ onclick } />);
wrapper.find('button').simulate('click');
expect(onclick).tohavebeencalled();
expect(testcomp.state('clicked')).tobe(true)
})
i generally prefer this type of test because i don't have to overwrite prototype, and it is actually testing that the click triggers the logic i expect. the original test really only covers that i am passing this.handlesubmit as an onclick prop to a button component and nothing more.
Source: stackoverflow.com
Related Query
- testing custom react methods with jest and enzyme
- How to mock React component methods with jest and enzyme
- Testing debounced function in React component with Jest and Enzyme
- Testing React Router with Jest and Enzyme
- React testing with Jest and Enzyme (in Symfony) got "Syntax Error: Unexpected token import"
- React testing state of component with Jest and Enzyme
- Getting started testing React components with Enzyme and Jest
- Testing react app with jest and enzyme token problem
- React testing with Jest and Enzyme @react-google-maps/api returns TypeError: Cannot read property 'maps' of undefined
- unit test custom hook with jest and react testing library
- Mock a custom service with jest and enzyme for a react component
- Testing component function in React with Enzyme and Jest
- How to mock third-party library custom events with jest and react testing library
- Faced error during testing with Jest and Enzyme in React
- How to test a className with the Jest and React testing library
- Testing with React's Jest and Enzyme when simulated clicks call a function that calls a promise
- Testing with React's Jest and Enzyme when async componentDidMount
- Testing changes to React component state and spying on instance methods using enzyme
- Testing react-router v4 with Jest and Enzyme
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- _this.store.getState is not a function when testing react component with enzyme and mocha
- Testing component with react-router v4, Jest and Enzyme
- how can I test if useState hook has been called with jest and react testing library?
- Testing react router v4 with jest enzyme
- Testing react context with jest and react-testing-library
- Unit test method that calls clearInterval with Jest and Enzyme on React
- How to test redux state update with react testing library and jest
- Testing React Async with Jest and create-react-app
- Testing react-router Redirects with Jest and Enzyme
- TypeError: dispatch is not a function when testing with react-create-app jest and enzyme
More Query from same tag
- 'command not found: jest'
- SVG logos not showing up in Chrome
- Module not found: Error: Can't resolve ReactJs
- Passing data to parent in React
- Show search result component in another route
- Why am I not rendering my state to the screen?
- how to show loading in percent, while loading from src folder
- How to conditionally render routes with react-router-dom?
- How to add anchor tag in the string passing to checkbox in react
- this.props and prevProps are equal
- Render HTML Symbols
- How to do authentication with React JS, Redux and Express?
- How to update the state in different mobile screens using React Hooks
- TypeError: Response is not a constructor in Expo SDK 33
- how get value of XYZ if it's available in object into array into object looks like {[{XYZ}}} in Reactjs?
- React Router: Location "/" Not Recognized?
- How are the process.env.NODE_ENV checks removed from ReactJS production builds?
- React (Lists and Keys) mapping appends old items to new items on re render
- Firebase Functions Can't set headers after they are sent
- javascript find character in string from reverse
- How to don't rerender root app component after every <a href> from menu (with BrowserRouter/Switch)
- ReactJS properties validation
- How to get the parameter after question mark in react router?
- What does import * in javascript stand for?
- ReactJS: Typescript .map is not a function
- Error: Objects are not valid as a React child (found: object with keys {zip})
- Year picker without dates and months
- How to slow down animation time in Recharts?
- How to use setTimeout within a .map() in reactJS
- How to correctly use a specific array value in react