Skip to content

Instantly share code, notes, and snippets.

@DdMad
Created March 30, 2017 12:30
Show Gist options
  • Save DdMad/2f96f4e61592e6d603d25e70913eb2e4 to your computer and use it in GitHub Desktop.
Save DdMad/2f96f4e61592e6d603d25e70913eb2e4 to your computer and use it in GitHub Desktop.
Create mocha enzyme test with React, React-Redux and Material-UI
import { expect } from 'chai';
import React from 'react';
import { mount } from 'enzyme';
import YourComponent from './YourComponent';
import {TableHeaderColumn} from 'material-ui/Table';
import configureStore from 'redux-mock-store';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
const middlewares = [];
const mockStore = configureStore(middlewares);
function setup() {
const initialState = { ... };
const store = mockStore(initialState);
const props = { ... };
const shallowWrapper = shallow(<YourComponent {...props} />);
const mountWrapper = mount(<YourComponent {...props} />, {
context: {
muiTheme: getMuiTheme(),
store: store
},
childContextTypes: {
muiTheme: React.PropTypes.object.isRequired,
store: React.PropTypes.object.isRequired
}
});
return {
props,
shallowWrapper,
mountWrapper
}
}
describe("<YourComponent />", ()=> {
it('renders 3 <TableHeaderColumn /> components', () => {
const { mountWrapper } = setup();
expect(wrapper.find(TableHeaderColumn)).to.have.length(3);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment