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
withStyles break enzyme tests #9266
Comments
I also notice this in "real world" usage beside the jest test. For example I use this kind of logic on my <LoadingWrapper loading={loading}>
<UseDataElement data={data} />
</LoadingWrapper> With this bug the |
Mh I'll get similar errors when trying some of your intern testing code. For example when trying this: I'll get this error:
Maybe I should use your testing scripts instead of the create-react-app ones. |
@Skaronator I was having a ton of issues testing my application. Once I found Mui's docs on testing and used their Shallow and Mount I had no issues. |
@Skaronator The issue isn't about the children elements but with the intermediary element, it's creating. The
|
@codepressd how did you manage to get testing working? I keep getting issues like find isn't a function. I cannot target my clickable objects for the life of me. |
@FahdW I'm using
|
@Skaronator Thanks for this! How would you test if the function itself was contained in the component? I am guessing you call a spy, so far jest's spy has been underwhelming and I have yet to get it to work. I was going to send a spy onto my dispatch to make sure that some of the internal functions are firing off dispatch calls. But this is good to point me in the right direction. |
@FahdW You might find it helpful to look at some of Material-UI's own tests. |
@mbrookes Thanks will take a look at your source, hopefully find something to help me. |
I had some trouble getting my head around why Once I did that, I realised that I needed to switch from |
I found that material-ui was not breaking my tests even when using withStyles and that this worked just fine using plain old enzyme: import { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
// CODE OMITTED HERE
it('renders title matches it with containsMatchingElement', () => {
const wrapper = shallow(<ElfHeader />);
const target = <Typography>Address Maven</Typography>;
expect(wrapper.dive().containsMatchingElement(target)).toBe(true);
}); Here was the JSX I wanted to match: <Typography variant="title" color="inherit" className={classes.flex}>
Address Maven
</Typography> And here was the messy code material-ui produced: <WithStyles(Typography) variant="title" color="inherit" className="ElfHeader-flex-100">
Address Maven
</WithStyles(Typography)> Nevertheless, my test passed. I say this only because this thread is a bit misleading for those who get errors and don't know their source. I guess my point is simply that in most cases, your tests should pass. Note this line from the docs: "You can take advantage of them if you so choose." Not that you have to use them, but use them if you find them convenient. |
The |
Why this issue is closed? Cannot go past through it... |
I'm having the same issue as Matteo. When testing the output in Jest i get <WithStyles(MaterialComponent) when I want to test for <MaterialComponent For example if I want to see if a Drawer component is present I have to test for <WithStyles(Drawer. Is there a way to get the test render to be the unwrapped component? or is the only way to test for the <WithStyles(... version? |
It shouldn't be too long before we start/try using hooks over higher-order component internally for the styles. I'm not sure what is the implication for shallow rendering. It will most likely solve this class of issues. |
I solved my issue, was an obvious fix. I was using the component name in quotes eg .find('Avatar') instead of importing it into the test and calling it directly .find(Avatar) - no quotes. That worked. |
@jondbm Solved the same way |
Seems like when using the withStyles HOC it'll actually break my Jest test. It creates children elements where it shouldn't.
Here is the code to reproduce it:
I use the
create-react-test
and simply runyarn test
. (they use Jest and enzyme IIRC)The text was updated successfully, but these errors were encountered: