-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Unable to test React with Material-UI Hidden element #2179
Comments
I'm having the same issue. Any solutions? |
What is |
That just prints out the HTML of the given comp for diagnostic purposes. I sed it to check if it was indeed picking up the correct component. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@asnaseer-resilient that doesn't actually print out the HTML, that prints out the enzyme react tree, which contains non-html components - can you please provide the actual full output of |
I have already provided the full output in my report above - repeated here:
|
I guess I'm a bit confused, because you're using Also, your test is looking for an ID of "'im-also-here", but you can clearly see that it is not present in that debug output. Why would you expect |
Yes - that is the point. The hiddenComp should be present as the test is being run with a Window width of 1024 (as displayed in the console output). So both comp.debug() and the test itself fail to "see" this component. Recall that I stated "If I run It's a fairly simple setup so you should be able to reproduce this in your environment which might help in diagnosing why this is failing. |
I believe jsdom has issues with correctly reporting viewport sizes. Can you share the exact component code that responds to the window width? |
I have included that code in the report as follows:
|
what about |
|
This is the contents of
|
Hi, I have the same problem. When I want to test hidden nested components, I don't find any nested elements. But I want to keep the JS implementation. Please give me some help with this |
OK, so the implication is that something about Material UI's Hidden component's JS implementation conflicts with jsdom, or, uses hooks in a way that doesn't work with That leads me here: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Hidden/HiddenJs.js It's using This seems to be using media queries. https://github.com/jsdom/jsdom/blob/d6f8a97b5fb7709d6ad0215c1ae95fd4cab58489/lib/jsdom/level2/style.js#L29 and testing-library/jest-dom#113 both strongly suggest that jsdom does not support media queries - which means that it's impossible to test this implementation outside of a real browser (a suggestion like this: jsdom/jsdom#2342 (comment) which has you manually set the width may work, but probably won't work with media queries) Thus, I'm going to close this since it's either a bug in jsdom, or a flaw in Material UI's choice of implementation. I'd suggest using shallow rendering anyways - testing Material UI itself is not something you really need to care about, since that's what its own tests cover. |
Thanks for the investigation - much appreciated. In response to your last comment - I am not actually testing MaterialUI, what I am testing is that the developer has coded to spec, i.e. certain elements should be visible on a page when on a desktop browser but not when on a mobile device. I will raise this as an issue on both the MaterialUI and jsdom github repos to see if either of them can shed any further light on how to fix this. |
I found a trick after some investigation. You can hack materialUI by passing a custom theme to your And if you read comments about So, for me, I use it like this and my mounted component generate my const theme = createMuiTheme({ props: { MuiWithWidth: { initialWidth: 'xs' } } })
const wrapper = createMount()(<MuiThemeProvider theme={theme}>
<MyComponentWithHidden />
</MuiThemeProvider>); |
@ljharb You are right. We encourage the usage of a media query polyfill with jsdom in the documentation https://material-ui.com/components/use-media-query/#testing. And thanks for looking at it <3! |
Thanks for confirming!! |
My solution to test content - wrapped in Hidden element: it('should display correct score of the selected question while landing on question player', () => {
const topPanelScoreHiddenElement = questionPlayer.find('div[data-testid="assessment-top-panel-score"]').find(Hidden).at(0)
const topPanelScore = mount(<>{topPanelScoreHiddenElement.prop('children')}</>)
expect(topPanelScore.find('h4').text())
.toEqual('2 / 12')
}) |
Your solution work for me, thank you!
And we can see the element in the snapshot!
|
It seems that as of MUI v5, this solution (which worked great for us for a while! thanks!) no longer works unfortunately, as it seems they have entirely removed WithWidth, so it's no longer possible to specify it with this props solution during theme creation. They did change their theming structure but there is no equivalent alternative in this new format to specify anything for withWidth in this way, since withWidth is gone. In our code base we had to switch a lot of components to using css media queries in their classes, since those do respond to |
@japser36, do you have an example of the changes you made? I am struggling with this same issue, and changing |
That solution in the MUI docs worked for me:
|
For anybody using Typescript:
or
Or with
|
Did you find any solution to do the test with the sx prop? Thanks in advance |
I have a managed to reproduce my issue in a very simple React app that I created via
npx create-react-app xxx
. I then installedmaterial-ui/core
,enzyme
, andenzyme-adapter-react-16
resulting in thispackage.json
:I then modified the default
App.js
to this:and modified the default
App.test.js
to this:Current behavior
When I run the tests using
npm test
I get this output:Expected behavior
If I run
npm start
then I correctly see bothHello World!
andGoodbye World!
elements.However, from the output above you can see that the test fails to find the element protected by the material-ui element. The output shows that the test is running with a window of size 1024px by 768px. I would therefore have expected this element to have been visible and therefore found by the above test.
Your environment
Mac OS Version 10.14.5 (18F132)
API
Version
Adapter
The text was updated successfully, but these errors were encountered: