Using enzyme/shallow/mount and a theme with custom variables in a sub component #11864
mynameistechno posted onGitHub
- This is a v1.x issue (v0.x is no longer maintained).
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior
Component that leverages theme + withStyles with custom variable doesn't fail. I have a component that I wrap with withStyles
. I only wrap the top level index with withRoot
(where the theme is added to context). How to easily use shallow
, mount
, etc. to test a sub-component? I.e. how do I get the theme into the context? I feel like this should be an easy fix, but I can't find a good example for this. See the test results in the codesandbox link.
Current Behavior
Test fails
Steps to Reproduce (for bugs)
See test results in https://codesandbox.io/s/wwkjkxzxj8
- in
withRoot
I added a custom theme variablesuccess.main
:const theme = createMuiTheme({ palette: { primary: { light: purple[300], main: purple[500], dark: purple[700] }, secondary: { light: green[300], main: green[500], dark: green[700] } }, success: { main: "pink" } });
- Created sub-component
Component
which uses the theme variable:import React from "react"; import { withStyles } from "@material-ui/core/styles";
const styles = theme => ({ customClass: { color: theme.success.main } });
class Component extends React.Component { render() { const { classes } = this.props; return <div className={classes.customClass}>Dude</div>; } }
export default withStyles(styles)(Component);
3. Although app runs fine in browser due to `withRoot` at the top level, if you run the tests, they barf because `theme.success` is undefined
Cannot read property 'main' of undefined
TypeError: Cannot read property 'main' of undefined at styles (https://ymv6ow7021.codesandbox.io/src/components/Component.js:26:28) at Object.create (https://ymv6ow7021.codesandbox.io/node_modules/@material-ui/core/styles/getStylesCreator.js:26:35) at WithStyles.attach (https://ymv6ow7021.codesandbox.io/node_modules/@material-ui/core/styles/withStyles.js:269:45) at new WithStyles (https://ymv6ow7021.codesandbox.io/node_modules/@material-ui/core/styles/withStyles.js:143:15) at ReactShallowRenderer.render (https://ymv6ow7021.codesandbox.io/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js:131:26) at eval (https://ymv6ow7021.codesandbox.io/node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:287:35) at withSetStateAllowed (https://ymv6ow7021.codesandbox.io/node_modules/enzyme-adapter-utils/build/Utils.js:94:16) at Object.render (https://ymv6ow7021.codesandbox.io/node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:286:68) at new ShallowWrapper (https://ymv6ow7021.codesandbox.io/node_modules/enzyme/build/ShallowWrapper.js:119:22) at shallow (https://ymv6ow7021.codesandbox.io/node_modules/enzyme/build/shallow.js:19:10) at shallowWithContext (https://ymv6ow7021.codesandbox.io/node_modules/@material-ui/core/test-utils/createShallow.js:35:19) at Object.eval (https://ymv6ow7021.codesandbox.io/src/components/Component.test.js:29:19)
``` 4. How to inject custom theme easily? Thanks!
Context
Easily test sub components without wrapping in too many HOC.
Your Environment
I used codesandbox provided link with latest create-react-app, enzyme, etc.