score:22
Accepted answer
this is the case scenario you want to test. if the componentdidmount is being called, check that it has been called only once, or as many times you want.
your test. i have the explanation in comments below
// inside your general `describe`
let wrapper;
const props = {
// your props goes here..
};
beforeeach(() => {
wrapper = shallow(<yourcomponent {...props} />);
});
it('should check `componentdidmount()`', () => {
const instance = wrapper.instance(); // you assign your instance of the wrapper
jest.spyon(instance, 'randomfunction'); // you spy on the randomfunction
instance.componentdidmount();
expect(instance.randomfunction).tohavebeencalledtimes(1); // you check if the condition you want to match is correct.
});
you can abstract, this case to do more complex things, but the basic gist of it is the above one. if you have a more detailed or better solution, please post it. thanks!!
Source: stackoverflow.com
Related Query
- Jest & Enzyme | Test PropTypes Function in componentDidMount
- Testing with React's Jest and Enzyme when simulated clicks call a function that calls a promise
- How to unit test useEffect cleanUp return function using Jest and Enzyme
- Jest/Enzyme | Test a function call in componentDidMount
- Function call inside useEffect not firing in jest test
- Jest unit test - How do I call through async function in a setTimeout repeating function
- TypeError: Cannot call a class as a function when running Jest test
- jest enzyme test method in componentDidMount
- Test a callback Function using jest and enzyme in ReactJS?
- Jest - Call React Component function without Enzyme
- Mock function not being called in Jest + Enzyme test
- How to call actual callback function in Jest unit test
- Jest | Enzyme how to test if function inside a component is being called during onClick event?
- React jest enzyme function called test
- Unit test reactjs component function with Jest and Enzyme
- Test call back function sent to a child element using enzyme
- How can I test a function of a function component that uses React.useState() using jest enzyme testing?
- Test function return different case scenario - Jest enzyme
- jest enzyme test method in componentDidMount fails but console shows that it is working
- How to write test cases for a function using jest and enzyme
- How to test filterBooks function in this component?? (React - Jest - Enzyme )
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- Jest test function call inside useEffect
- How to test a function call after state receive value inside expression in React function component using Jest
- i was writing test cases for actions but having received anonymous function while using dispach using jest and enzyme
- Can I test a function mapping data into a child component using Jest and Enzyme
- asynchronous function test onChange event fails in enzyme and jest
- Jest test fails : TypeError: window.matchMedia is not a function
- Jest Enzyme test a React component that returns null in render method
- Test a React Component function with Jest
More Query from same tag
- Same route hiding component issue
- React - on click text inside component, image appears
- Deploy an application with dockerised frontend and dockerise backend images
- Why am i getting unexpected token for '<>' error in the following piece of react code
- Why isn't my event listener callback using the correct state?
- State is not defined
- Can i use npm link to attach React components?
- React & AWS Amplify - User does not have delivery config set to turn on SMS_MFA
- Conditionally Rendering Material UI Styles?
- babel-loader not working, Unexpected token '<'
- GeoChart with react-google-charts doesn't work
- Uncaught TypeError: Cannot set property 'props' of undefined when storing component in useState
- how to communicate between a root React component and the rest of the app using Redux?
- button not working in React
- how to make a vertical slide of text in React
- Changing a Component inside another Component/react-icons
- How to add login authentication and session to ReactJS React Router and Redux with MongoDB + NodeJS Express?
- CSS Flex - Set same width with TailwindCSS
- Array.includes returning conflicting results on react component load
- react routing two cmponents
- Decode a Uint8Array into a JSON
- How to parse DOM (REACT)
- Calculating SVG bounding boxes with React?
- In React components implemented with hooks, how can the latest component state values be read from within setInterval?
- Stop child update after parent unmounted
- Express server throws weird "cannot read properties of undefined reading "name" error.."
- CSS, imposing lines and shapes onto a button?
- loading while fetching firestore data
- React Custom Component Styling
- React - my map function on my array component is not defined when I try to declare it somewhere else