Skip to content

Instantly share code, notes, and snippets.

@oshell
Created November 17, 2019 20:54
Show Gist options
  • Save oshell/db0096e92ab8e6e8f34cd58e026bc9e7 to your computer and use it in GitHub Desktop.
Save oshell/db0096e92ab8e6e8f34cd58e026bc9e7 to your computer and use it in GitHub Desktop.
react test with shallow rendering and event mock
import React from 'react';
import { shallow } from 'enzyme';
import App from './App';
it('button click changes color of box', () => {
const app = shallow(<App />);
expect(app.find('.box').length).toEqual(1);
// cache button element
const button = app.find('button').last();
// pass mocked event object
button.simulate('click', {
target: {
getAttribute: function() {
return button.props()['data-color']
}
}
});
expect(app.find('.box.red').length).toEqual(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment