我有一个组件,包含在Material-UI withStyles
HOC和React memo
HOC中。
我无法测试此组件,因为我无法调用dive()
:
ShallowWrapper::dive() can only be called on components
我目前唯一知道的选择是独立export Demo
和export default withStyles(styles)(Demo)
。这允许我测试未包含在withStyles
中的组件。我想避免这种方法。
如果我删除memo(),我可以测试组件。同样,如果我删除withStyles(),我也可以测试该组件。这些HOC的组合使我的组件无法测试。
有效测试此组件的可用策略有哪些?
demo.js
import React, { memo } from "react";
import MUIIconButton from "@material-ui/core/IconButton";
import { withStyles } from "@material-ui/core/styles";
import Tooltip from "@material-ui/core/Tooltip";
import Typography from "@material-ui/core/Typography";
const styles = () => ({
root: {
backgroundColor: "red"
/* more styles... */
}
});
const Demo = memo(({ label, classes }) => (
<div className={classes.root}>
<Tooltip disableFocusListener title={label}>
<Typography>label</Typography>
</Tooltip>
</div>
));
export default withStyles(styles)(Demo);
demo.test.js
import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { configure, shallow } from "enzyme";
import Demo from "./demo";
import MUIIconButton from "@material-ui/core/IconButton";
import Tooltip from "@material-ui/core/Tooltip";
configure({ adapter: new Adapter() });
describe("Demo", () => {
it("Should have a tooltip with label", () => {
const tooltip = "My tooltip";
const el = shallow(<Demo label={tooltip} />).dive();
expect(el.find(Tooltip).props().title).toEqual(tooltip);
});
});
完整的沙盒
正如skyboyer建议的那样,你应该只是export
记忆功能。您可以import
默认导出HOC
并使用mount
,但您需要模拟classes
对象以匹配它在组件中的使用方式。
工作示例:https://codesandbox.io/s/4r492qvoz9
组件/演示/ demo.js
import React, { memo } from "react";
import MUIIconButton from "@material-ui/core/IconButton";
import { withStyles } from "@material-ui/core/styles";
import Tooltip from "@material-ui/core/Tooltip";
import Typography from "@material-ui/core/Typography";
const styles = () => ({
root: {
backgroundColor: "red"
/* more styles... */
}
});
export const Demo = memo(({ label, classes }) => {
return (
<div className={classes.root}>
<Tooltip disableFocusListener title={label}>
<Typography>label</Typography>
</Tooltip>
</div>
);
});
export default withStyles(styles)(Demo);
components / Demo / __ tests __ / demo.test.js如果需要查看DOM
结构,那么只需使用console.log(wrapper.debug());
- 例如console.log(mountHOComponent.debug());
)
import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { configure, shallow, mount } from "enzyme";
import { Demo } from "../demo";
import HOCDemo from "../demo";
configure({ adapter: new Adapter() });
const initialProps = {
label: "My tooltip",
classes: {
root: "component-example"
}
};
const shallowWrapper = shallow(<Demo {...initialProps} />);
const mountWrapper = mount(<Demo {...initialProps} />);
const mountHOComponent = mount(<HOCDemo {...initialProps} />);
describe("Demo", () => {
afterAll(() => {
shallowWrapper.unmount();
mountWrapper.unmount();
});
it("shallowWrap renders a tooltip with label", () => {
expect(shallowWrapper.find("WithStyles(Tooltip)").props().title).toBe(
initialProps.label
);
});
it("mountWrap renders a tooltip with label", () => {
expect(mountWrapper.find("Tooltip").props().title).toBe(initialProps.label);
});
it("mountHOComponent renders a tooltip with label", () => {
expect(mountHOComponent.find("Tooltip").props().title).toBe(
initialProps.label
);
});
});
现在已经修复了enzyme-adapter-react-16 v1.13.0,它增加了memo dive()支持。这是一个分叉沙箱,其依赖关系已更新,以显示两种测试方法(潜水和导出解决方法)现在都已通过。
当我用备忘录包装时,我得到一个看起来像这样的形状
import MemoizedFoo from './Foo'
console.log(MemoizedFoo)
{ '$$typeof': Symbol(react.memo),
type:
{ [Function: Foo]
displayName: 'Foo',
defaultProps: { theme: {} } },
compare: null }
所以在我的开玩笑测试中,我可以通过引用类型键来获取内部组件
import MemoizedFoo from './Foo'
const Foo = MemoizedFoo.type
describe() { it() { shallow(Foo) ...etc } }
这对于浅单元测试非常有用。
如果我正在安装父组件并寻找孩子在场,你可以这样做:
wrapper = mount(Layout)
wrapper.find('Memo(Foo)')