score:4
Accepted answer
// GIVEN
const clickCallback = sinon.spy();
const actualNode = shallow(<YourComponentName onClick={clickCallback}/>);
// WHEN
actualNode.find(".simple-button").simulate("click");
// THEN
sinon.assert.called(clickCallback);
score:1
import React from 'react';
import { shallow } from 'enzyme';
import QuizModeSelector from '../index';
import Menu from '../Menu';
import MenuItem from '../MenuItem';
describe('<QuizModeSelector />', () => {
const quizMode = 'pc';
const onSetQuizMode = jest.fn();
const props = {
quizMode, onSetQuizMode,
};
const renderedComponent = shallow(<QuizModeSelector {...props} />);
it('should render a <Menu> tag', () => {
expect(renderedComponent.type()).toEqual(Menu);
});
it('should contain 2 MenuItems', () => {
const items = renderedComponent.find(MenuItem);
expect(items).toHaveLength(2);
});
it('should render a pc MenuItem', () => {
expect(renderedComponent.containsMatchingElement(
<MenuItem
selected={quizMode === 'pc'}
>
Notes
</MenuItem>
)).toEqual(true);
});
it('should render a pitch MenuItem', () => {
expect(renderedComponent.containsMatchingElement(
<MenuItem
selected={quizMode === 'pitch'}
>
Pitch
</MenuItem>
)).toEqual(true);
});
it('should handle click events', () => {
renderedComponent.find(MenuItem).first().simulate('click');
expect(onSetQuizMode).toHaveBeenCalledWith('pc');
renderedComponent.find(MenuItem).last().simulate('click');
expect(onSetQuizMode).toHaveBeenLastCalledWith('pitch');
});
});
Source: stackoverflow.com
Related Query
- Trouble testing button onClick handler with Enzyme
- testing button class with enzyme
- How to add extra onClick handler along with the user's onClick on the button in react?
- Testing window resize handler on React.Component with Jest & Enzyme
- How can I test a button which uses props.history for it's onClick with react testing library?
- Maintaining href "open in new tab" with an onClick handler in React
- Nested components testing with Enzyme inside of React & Redux
- Accessing the State of a Functional Component with React Hooks when Testing with Enzyme
- Testing React portals with enzyme
- 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
- onClick handler not registering with ReactDOMServer.renderToString
- Storybook JS Interaction Testing with Enzyme - State Changes being Overidden
- Testing functions inside stateless React component with Enzyme
- Testing react-router v4 with Jest and Enzyme
- React testing component prop change with enzyme
- Testing debounced function in React component with Jest and Enzyme
- _this.store.getState is not a function when testing react component with enzyme and mocha
- Testing react-intl components with enzyme
- How to getByRole a button with image in React Testing Library
- Set URL params while testing with Jest & Enzyme
- Testing the `React.createRef` api with Enzyme
- Testing recompose's HOC with enzyme
- Testing component with react-router v4, Jest and Enzyme
- Enzyme render fails when importing image with webpack while testing with Jest
- Testing React Component className with enzyme
- How to test Material-UI's Responsive UI (e.g. Hidden, Grid, Breakpoints) in React with Enzyme or React Testing Library
- Testing react router v4 with jest enzyme
- Testing React Router with Jest and Enzyme
- Testing debounced method in React with Enzyme
More Query from same tag
- How to import dependencies in reactjs class?
- Map array of dictionaries to select dropdown?
- Run set interval when click from another component React
- Proper way to navigate with React Native, Redux, and Navigator
- Trying to upload an image using Firebase storage and Firestore Database - encountering a map error
- need to refresh page after axios call
- How do I implement a hamburger dropdown in my React navbar
- How to pass props without rendering the component?
- What is the 'condition' type for react component children props?
- Cannot import d3-tip or d3-hexbin into React component
- If Statement with Expression inside .map() within React Return()
- ReactJS: Pass parameter from rails to react router to component
- onChange event inside loop is only passing 0th index
- React App Crashing Due to undefined PropTypes.node
- how to stop re-rendering of child component if parent update the context or state in react js?
- React: accessible unordered list of buttons
- react-dates solution to select single date from calendar
- React Avatar Editor
- Using dictionary to map against an API response and create array of objects, trying to remove duplicates
- How do I call useQuery in a map function?
- Have text above icon in React JS
- React router dom version update
- Handling API error responses with axios (multiple files)
- I want to access my state variable from one component to other
- Can this class be changed into a React stateless function?
- Errors with Flow, ReactJS and ES6 classes
- Disable Snackbar animation [Material-ui]
- Using Promises with React Redux
- Add item on the list when pressing Enter and clear input field
- Makestyles hook is not working in Material Ui