It is better to test the component separately. The problem is that material-ui skips its details using the React context . You can specify the context of your component in this way:
import React from 'react'; import { mount } from 'enzyme'; import getMuiTheme from 'material-ui/styles/getMuiTheme'; const wrapper = mount( <MyComponent />, { context: { muiTheme: getMuiTheme(), }, childContextTypes: { muiTheme: React.PropTypes.object.isRequired, } } );
Another thing you need to isolate is remove the <Provider> . Instead of testing the connected component, try testing the component itself as described in the Redux docs: Testing the connected components
Soon - export the component and component, and then test the component by passing the details. An example of a component with export:
import { connect } from 'react-redux' // Use named export for unconnected component (for tests) export class MyComponent extends Component { /* ... */ } // Use default export for the connected component (for app) export default connect(mapStateToProps)(MyComponent)
Now you can import the undeclared component into the test file as follows:
import { MyComponent } from './MyComponent';
And the last test might look like this:
import React from 'react'; import { mount } from 'enzyme'; import getMuiTheme from 'material-ui/styles/getMuiTheme'; import { MyComponent } from './MyComponent'; test('test component', () => { const wrapper = mount( <MyComponent />, { context: { muiTheme: getMuiTheme(), }, childContextTypes: { muiTheme: React.PropTypes.object.isRequired, } } ); // Test something const p = wrapper.find(...); expect(...).toEqual(...); // Change props wrapper.setProps({foo: 1}); // test again expect(...).toEqual(...); });
source share