I have a component which is wrapped in both a Material-UI withStyles
HOC and a React memo
HOC.
I am unable to test this component as I am unable to call dive()
:
ShallowWrapper::dive() can only be called on components
The only option I am currently aware of is to independently export Demo
and export default withStyles(styles)(Demo)
. This allows me to test the component that isn't wrapped in withStyles
. I would like to avoid this method.
If I remove memo(), I am able to test the component. Likewise, if I remove withStyles(), I am also able to test the component. The combination of these HOCs render my component un-testable.
What are some available strategies to effectively test this component?
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);
});
});
Full working Sandbox