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');
  });
});

Related Query

More Query from same tag