Skip to content

Instantly share code, notes, and snippets.

@oshell
Last active November 17, 2019 22:31
Show Gist options
  • Save oshell/165a49cde9a62c443ecea4a575aea1a4 to your computer and use it in GitHub Desktop.
Save oshell/165a49cde9a62c443ecea4a575aea1a4 to your computer and use it in GitHub Desktop.
enzyme test of stateful functional component with async state change
import React from 'react';
import { shallow } from 'enzyme';
import App from './App';
it('button click changes color of box', async () => {
const app = shallow(<App />);
expect(app.find('.box').length).toEqual(1);
// cache button element
const button = app.find('button').last();
const eventMock = {
target: {
getAttribute: function() {
return button.props()['data-color']
}
}
};
// pass mocked event object
await button.props().onClick(eventMock);
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