我有这个要进行单元测试的简单的React类组件。在实现react-router并将组件包装在withRouter
中并开始使用Link
之前,我很容易能够在Mocha测试期间使用酶的浅层函数来渲染组件。但是,情况不再如此,因为react-router
的组件取决于上下文。
组件:
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);
测试:
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());
});
});
我已经按照预期尝试将组件包装在MemoryRouter
中,这确实有效果,但是它并没有呈现我习惯的样子。通常,在执行w.debug()
时,我将获得组件及其所有子组件的完整html / jsx输出。请允许我执行非常方便的断言,例如w.find(SomeComponent).assert.something
。
我从console.log语句获得的输出:
<Router history={{...}}>
<withRouter() open={true} toggleDrawer={[Function]} actConfig={{...}} />
</Router>
我一直在阅读here (Using MemoryRouter with enzyme shallow testing)并看着这个react-router-enzyme-context我已经尝试了两者,但是将第二个参数传递给shallow(h(DrawerMenu), context)
似乎没有任何效果,输出就像没有它一样:
<ContextConsumer>
[function]
</ContextConsumer>
我也尝试使用mount
代替浅输入法,但是它根本不输出任何东西,最好还是使用浅输入法。
我觉得我有点接近,或者接近某个东西,只是错过了最后一块。
答案 0 :(得分:0)
发现导入的包装组件具有属性WrappedComponent
这只是组件本身,然后才被withRouter()包裹。
以下内容用于测试组件的呈现:
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());
});
});
输出:
<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))>
但是请记住,用这种方法不可能测试布线,只能测试内部组件。