简体   繁体   中英

react-router v4 mocha testing with enzyme shallow not working

I have this simple react class component that I want to unit test. Before I implemented react-router and wrapped the component in withRouter and started using Link I was easily able to render the component during mocha tests using enzyme's shallow function. However this is not the case anymore, as the react-router 's components depend on a context.

The component:

const React = require('react');
const { Link, withRouter } = require('react-router-dom');
const createReactClass = require('create-react-class');
const { Divider, Drawer, IconButton, MenuList, MenuItem, ListItem, ListItemText } = require('@material-ui/core');
const { ChevronLeft } = require('@material-ui/icons');

const h = React.createElement;

const DrawerMenu = createReactClass({
  renderMenuItems: function ({ actConfig, pathname }) {
    const chapterList = actConfig.chapterList;
    return (chapterList || []).map(chapter => {
      const path = `/${chapter.idName}`;
      return h(MenuItem, {
        key: chapter.idName,
        component: Link,
        to: path,
        selected: path === pathname
      },
      h(ListItemText, {}, chapter.title));
    });
  },

  render: function () {
    const { actConfig, open, location: { pathname } } = this.props;
    return (
      h(Drawer, { open },
        h('div', {},
          h(MenuList, {},
            h(ListItem, { key: 'header' },
              h(ListItemText, {}, actConfig.title),
              h(IconButton, { onClick: this.props.toggleDrawer },
                h(ChevronLeft, {}))),
            h(Divider, {}),
            this.renderMenuItems({ actConfig, pathname }))))
    );
  }
});

module.exports = withRouter(DrawerMenu);

The test:

const React = require('react');
const sinon = require('sinon');
const { MemoryRouter } = require('react-router');
const DrawerMenu = require('./drawerMenu');

const h = React.createElement;

describe('drawerMenu', function () {
  const props = {
    open: true,
    toggleDrawer: sinon.spy(),
    actConfig: {
      title: 'test act title',
      chapterList: [{ title: 'chapter 1', idName: 'some-id-1' }, { title: 'chapter 2', idName: 'some-id-2' }]
    }
  };

  it('render', function () {
    const w = shallow(
      h(MemoryRouter, {},
        h(DrawerMenu, props)));
    console.log(w.debug());
  });
});

I have tried to wrap the component in a MemoryRouter as supposed to, and this does have an effect, but it does not render what I am used to. Normally when doing w.debug() i would get the full html/jsx output of the component and all its children. Allowing me to perform very handy assertions like w.find(SomeComponent).assert.something .

The output I get from the console.log statement:

<Router history={{...}}>
  <withRouter() open={true} toggleDrawer={[Function]} actConfig={{...}} />
</Router>

I have been reading here (Using MemoryRouter with enzyme shallow testing) and looking at this one react-router-enzyme-context I have tried both, but passing a second argument to the shallow(h(DrawerMenu), context) doesn't seem to have any effect, the output is the same as without it:

<ContextConsumer>
  [function]
</ContextConsumer>

I have also tried using mount instead of shallow, but it does not output anything at all, and preferably I want to use shallow anyway.

I feel like I am somewhat close, or on to something, just missing the last piece..

Found out that the imported wrapped component has a property WrappedComponent that is just the component itself before wrapped in withRouter().

Following worked for testing the rendering of the component:

const React = require('react');
const DrawerMenu = require('./drawerMenu').WrappedComponent;

const h = React.createElement;

describe('drawerMenu', function () {
  it('render', function () {
    const w = shallow(h(DrawerMenu, someProps));
    console.log(w.debug());
  });
});

Output:

<WithStyles(ForwardRef(Drawer)) open={true}>
  <div>
    <ForwardRef(MenuList)>
      <WithStyles(ForwardRef(ListItem))>
        <WithStyles(ForwardRef(ListItemText))>
          test act title
        </WithStyles(ForwardRef(ListItemText))>
        <WithStyles(ForwardRef(IconButton)) onClick={[Function]}>
          <ChevronLeftIcon />
        </WithStyles(ForwardRef(IconButton))>
      </WithStyles(ForwardRef(ListItem))>
      <WithStyles(ForwardRef(Divider)) />
      <WithStyles(ForwardRef(MenuItem)) component={{...}} to="/chapter1" selected={false}>
        .
        .
        .
      </ForwardRef(MenuList)>
    </ForwardRef(MenuList)>
  </div>
</WithStyles(ForwardRef(Drawer))>

But keep in mind, testing the routing is not possible this way, only testing the inner component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM