score:1
Accepted answer
You can't spy handleClick
event handler since it's defined in functional scope, it's private.
Your best test component behavior rather than implementation, such as what method is called inside the event handler, you should not go to mock and assert whether these methods are called, but what changes have occurred, such as what happens to the component, UI, DOM tree, etc.
But in your example, the handleClick
event handler does not do anything, just calls console.log
, then you can only assert whether the console.log
is called indirectly assert whether the handleclick
is called.
// arrange
const logSpy = jest.spyOn(console, 'log')
// act
// ...
// assert
expect(logSpy).toHaveBeenCalled()
Source: stackoverflow.com
Related Query
- How to test a function inside React functional component using jest.spyOn with React Testing Library
- How to test a function call after state receive value inside expression in React function component using Jest
- How to test the state of a functional component in React with Jest (No Enzyme)
- How to test with jest and typescript with types a basic react function component
- How to test react functional component correctly using jest and enzyme?
- How to test a handle function call with a react functional component with hooks
- How to test functional component with async callback inside useEffect using snapshots
- How to test React component with children using jest and enzyme?
- How to test the style inside the CSS class for React component using Jest and Enzyme?
- How to test if state of a component is changed by the function using jest and Enzyme in React TDD
- Testing React Functional Component with Hooks using Jest
- Test a React Component function with Jest
- Spying on React functional component method with jest and enzyme; Cannot spyOn on a primitive value
- mock useDispatch in jest and test the params with using that dispatch action in functional component
- How to test methods inside functional component using enzyme as instance() returns null for shallow wrapper?
- How to use jest.spyOn with React function component using Typescript
- Mock React useRef or a function inside a functional component with enzyme and jest?
- React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
- How to test below component in React using JEST
- How to test class instance inside a function with Jest
- How to test events created with mousetrap in react using jest
- How to test a React component that has Router, Redux and two HOCs... with Jest and Enzyme?
- How to solve closures issue when using React functional component with React.memo?
- How to test a React component that uses context like in react-router 2.0 with Jest 0.8.x
- How does React Context work by using a function inside of a component
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- Stateful React component with async function failing Jest test
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- React Test. how to test function inside react component
- How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js?
More Query from same tag
- How to dispatch actions via a timer using Redux
- I'm trying to render a input form with an onClick listener in react, but it isn't working
- Get the id details from an array which was extracted from another array
- Using Import In NodeJS server
- jest.mock - Cannot find module in React test file
- Resolve promises in order of resolve/execution time?
- How to display on select the first group value selected first ? (with 2 different select)
- ReactJS Updating Array
- Browserify-ed code on a server: how to get "window", "location" and other "window" properties and typically browser objects?
- How do you do conditional hovering in styled components?
- React.js pass multiple json values to one child parameter
- How to read and modify a state in useEffect IAW 'exhaustive-deps' lint rule
- textarea onChange not updating the state
- How can I add a Custom component in a Data object?
- Get text content from node in React
- TypeError: Cannot read property 'onMouse' of undefined React Class Component
- How to map multiple states to props by using Redux in React?
- How do I handle complex objects in ReactJS?
- Class assign in React
- how to get dropdown displayed text?
- WebStorm type checking React props using type definition
- How to see actual React error in browser log in ASP.NET MVC application when using Visual Studio
- Please explain this abbreviated ES6 JSX declaration
- How to fix moment.js in reactjs?
- Suggestions on text input in Redux-form
- Flask get a multi-dimensional array posted to it as an array
- ChunkLoadError: Loading chunk XY failed. - Randomly getting fatal on PRODUCTION
- React give mistake before less-than symbol
- Cannot submit form data to DatoCMS using Gatsby.js
- Deactivate drag and drop for table in react