Unable to set context when rendering child components

I'm trying to test a custom Material-ui Change the component with Enzyme, but getting the following error:

ERROR: 'Warning: Failed context type: Required context 'muiTheme' was not specified in 'ChildComponent'.

Context I tried to install in accordance with this . The component that I want to achieve and test is a child component.

const root = shallow(<RootComponent />, {context: {muiTheme}, childContextTypes: {muiTheme: React.PropTypes.object}})
const child = root.find(ChildComponent)
child.render() // <--- this line is throwing the error

update: this is related

+4
source share
2 answers

I'm not sure if this is a solution, but it is one step closer to the goal.

const root = mount(<RootComponent />, {
  context: {muiTheme},
  childContextTypes: {muiTheme: React.PropTypes.object}
})
const child = root.find(ChildComponent)

Notice I use mountinstead shallow. The problem is that I can no longer use child.find({prop: 'value'})- return 0 elements ...

+2

<muiThemeProvider>.

, :

import React from 'react';
import { mount } from 'enzyme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import Input from './Input';

describe('<Input />', () => {
    const mountWithTheme = (node) => mount(<MuiThemeProvider>{node}</MuiThemeProvider>);

    it('calls componentDidMount', () => {
        sinon.spy(Input.prototype, 'componentDidMount');
        mountWithTheme(<Input />);
        expect(Input.prototype.componentDidMount.calledOnce).to.equal(true);
    });
});
+1

Source: https://habr.com/ru/post/1657083/


All Articles