gpt4 book ai didi

javascript - 使用 enzyme 浅层进行 react 路由器 v4 Mocha 测试不起作用

转载 作者:行者123 更新时间:2023-11-28 03:24:12 24 4
gpt4 key购买 nike

我有一个简单的 react 类组件,我想对其进行单元测试。在我实现react-router并将组件包装在withRouter中并开始使用Link之前,我可以在mocha测试期间使用enzyme的shallow函数轻松渲染组件。然而,情况不再是这样了,因为 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 而不是shallow,但它根本不输出任何内容,而且最好我还是想使用shallow。

我觉得我已经接近或接近某件事了,只是错过了最后一部分..

最佳答案

发现导入的包装组件有一个属性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))>

但请记住,通过这种方式测试路由是不可能的,只能测试内部组件。

关于javascript - 使用 enzyme 浅层进行 react 路由器 v4 Mocha 测试不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58821322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com