-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[docs] useMediaQuery + test env (matchMedia) #16073
Comments
👋 Thanks for using Material-UI! We use the issue tracker exclusively for bug reports and feature requests, however, If you would like to link from here to your question on SO, it will help others find it. |
Hi @oliviertassinari, I think this may be an actual bug. I encountered it as well. It is at least a regression. I have not solved it yet, but it seems to be introduced by 942fbf2#diff-33a03947dab1f076cc15415d619e4987 Before this commit, |
@koshea How can we reproduce the problem? |
@oliviertassinari see here: https://github.com/koshea/jest-mui-withmobiledialog The mounted component will be empty in version 4.1.1. If you revert to 4.0.2 the test will pass. |
@koshea Thank you for the reproduction. I can confirm the issue. It's an issue with the test environment. The withWidth helper renders null if he can't compute the correct value. We used to rely on In your case, you can workaround the problem with: import mediaQuery from 'css-mediaquery';
function createMatchMedia(width) {
return query => ({
matches: mediaQuery.match(query, { width }),
addListener: () => {},
removeListener: () => {},
});
}
describe('MyTests', () => {
beforeAll(() => {
// Wait for JSDOM support
window.matchMedia = createMatchMedia(window.innerWidth);
}); or use https://github.com/bigslycat/mq-polyfill. |
That solves it and makes complete sense, thank you @oliviertassinari! |
Why was the title of this issue changed? |
@bbehling-trimble We need to document how this match media logic can be tested, for instance, we Jest and react-testing-library. |
@oliviertassinari Well, the issue wasn't originally submitted regarding media queries. This code will still need to be tested when in desktop top view. |
I agree it's unfortunate. Your test environment needs to be consistent. If window.innerWidth returns a value, window.matchMedias should follow it. |
The problem isn't testing for mobile, its testing in desktop mode. We dont do anything with setting window props |
This solution is not working for v5 |
Yes I am also facing the same issue. Did you find a solution to this? |
Do you have any solution for @Mui5? |
Hi, |
When I inject withMobileDialog into a React component, I cannot access members of the component.
I've tried dive, Material UI shallow, mount, enzyme shallow and mount to no avail.
Without injecting withMobileDialog, I can access the members without a problem.
How do I access members on the component when using withMobileDialog()
Component
Test
The text was updated successfully, but these errors were encountered: