How to pass context down to the Enzyme mount method to test component which includes Material UI component?

后端 未结 2 811
情话喂你
情话喂你 2020-12-30 20:06

I am trying to use mount from Enzyme to test my component in which a several Material UI component are nested. I get this error when running the test:

<

相关标签:
2条回答
  • 2020-12-30 20:08

    this is my handy method to test Material UI with shallow and mount

    ...
    import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'
    import getMuiTheme from 'material-ui/styles/getMuiTheme';
    
    const muiTheme = getMuiTheme();
    const shallowWithContext = (node) => shallow(node, {context: {muiTheme}, childContextTypes: {muiTheme: PropTypes.object}});
    
    const mountWithContext = (node) => mount(
      node, {context: {muiTheme}, childContextTypes: {muiTheme: PropTypes.object}}
    );
    
    
    // now you can do
    const wrapper = shallowWithContext(<Login auth={auth} onChange={() => 'test'} />);

    0 讨论(0)
  • 2020-12-30 20:33

    Try adding childContextTypes in the mount options:

    return mount(
      <SearchBar {...props} />, {
        context: {muiTheme},
        childContextTypes: {muiTheme: React.PropTypes.object}
      }
    );
    

    By doing it you set the Enzyme wrapper to make the muiTheme available to it's children through the context.

    0 讨论(0)
提交回复
热议问题