-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using enzyme/shallow/mount and a theme with custom variables in a sub component #11864
Comments
This comment has been minimized.
This comment has been minimized.
I think that it would be a good opportunity to add such in the documentation: Component.js import React from "react";
import { withStyles } from "@material-ui/core/styles";
const styles = theme => ({
root: {
color: theme.success.main
}
});
class Component extends React.Component {
render() {
const { classes } = this.props;
return <div className={classes.root}>Dude</div>;
}
}
export default withStyles(styles)(Component); Component.test.js import React from "react";
import { shallow, mount } from "enzyme";
import { unwrap } from "@material-ui/core/test-utils";
import MuiThemeProvider from "@material-ui/core/styles/MuiThemeProvider";
import Component from "./Component";
const ComponentNaked = unwrap(Component);
describe("<Component />", () => {
it("with shallow", () => {
const wrapper = shallow(<ComponentNaked classes={{}} />);
console.log("shallow", wrapper.debug());
});
it("with mount", () => {
const wrapper = mount(
<MuiThemeProvider
theme={{
success: {
main: "#fff"
}
}}
>
<Component />
</MuiThemeProvider>
);
console.log("mount", wrapper.debug());
});
}); |
@oliviertassinari |
@kallebornemark Right, we need to add it 👍 |
@kallebornemark Care to give it a go? |
Have you tried using MUI also has some test utils to help: https://material-ui.com/guides/testing/#api They wrap enzyme's E.g. import * as React from 'react';
import {createShallow} from '@material-ui/core/test-utils';
import Foo from './Foo';
describe('<Foo />', () => {
let shallow;
beforeEach(() => {
shallow = createShallow({untilSelector: 'Foo'});
});
it('should render', () => {
const wrapper = shallow(<Foo />);
console.log(wrapper.debug());
});
}); |
@mynameistechno Thank you very much for the speedy reply. I somehow accidentally deleted my question. For anyone wondering in the future, my question was:
For your response, I tried to use As for the
I tried both:
and then shallow rendered, they all gave me the error. It might be some version imcompatible issue. But after some more research, thanks to your guide, I found a working solution for my current setup.
Thank you again and I hope this would help others. |
You CAN manually provide the context with the custom theme & custom variables to |
@issuehuntfest has funded $40.00 to this issue. See it on IssueHunt |
I had more or less the same problem, but in my case I was using I've adapted your codesandbox here with the test passing. I'm only using Here is the test updated:
I had to do a named export for this to work without passing the styles:
I've tried to fix this following the material-ui testing documentation and @oliviertassinari response, but I was not able to make it work. |
This is more of a general question regarding testing react components. If you use a custom context then you are responsible for either mocking it or ensuring your components are only rendered in the actual context. I guess most of the issues arise from using shallow rendering API which we no longer encourage. We had to refactor way too many test without anything actually breaking because of heavy usage of shallow rendering. We now use almost exclusively |
Agreed. I've also started to migrate away from shallow rendering with enzyme and using dom-testing-library instead. Additionally we use a render wrapper function for rendering in tests that includes all the context we need. Here is a simplified version built on examples from dom-testing-library that add our redux store and mui theme: // test-utils.js
import {render} from 'react-testing-library';
import theme from '../ourCustomTheme';
export function renderWithStore(element, { initialState, store = createStore(reducer, initialState) } = {}){
return {
...render(
<Provider store={store}>
<MuiThemeProvider theme={theme}>{element}</MuiThemeProvider>
</Provider>
),
store
};
} I think we can close this ticket. |
👍 for closing, unless @eps1lon wants to update the documentation. |
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 withwithRoot
(where the theme is added to context). How to easily useshallow
,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
withRoot
I added a custom theme variablesuccess.main
:Component
which uses the theme variable:withRoot
at the top level, if you run the tests, they barf becausetheme.success
is undefinedContext
Easily test sub components without wrapping in too many HOC.
Your Environment
I used codesandbox provided link with latest create-react-app, enzyme, etc.
The text was updated successfully, but these errors were encountered: