Skip to content

Instantly share code, notes, and snippets.

@justusromijn
Created June 11, 2019 18:46
Show Gist options
  • Save justusromijn/d7869164b3e6746fc45266f31193b47f to your computer and use it in GitHub Desktop.
Save justusromijn/d7869164b3e6746fc45266f31193b47f to your computer and use it in GitHub Desktop.
Simple React Button Unit Test
import { mount } from 'enzyme';
import * as React from 'react';
import { Button } from './button';
describe('<Button />', () => {
describe('By default', () => {
let component;
beforeAll(() => {
component = mount(<Button label="foo" />);
});
it('renders with a primary flavour', () => {
expect(component.html()).toMatchSnapshot();
});
it('does nothing on clicks', () => {
let clickEvent = () => {
component.find('button').simulate('click');
};
expect(clickEvent).not.toThrow();
});
});
describe('Using props', () => {
let component;
let clickFn = jest.fn();
beforeAll(() => {
component = mount(<Button label="foo" flavour="bar" onClick={clickFn} />);
});
it('renders with the passed flavour', () => {
expect(component.html()).toMatchSnapshot();
});
it('calls the onClick prop function when clicked', () => {
expect(clickFn).not.toHaveBeenCalled();
component.find('button').simulate('click');
expect(clickFn).toHaveBeenCalled();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment