How to set window size in jest test?
See original GitHub issueHow should I setup a test where a different component is rendered depending on screen size?
This is the component:
<Media query={`(max-width: ${theme.screenSizes.sm.max}px)`}>
{matches => (matches ? this.renderMobileView() : this.renderDesktopView())}
</Media>
It only executes renderDesktopView but I’d like to also test this.renderMobileView().
I’ve added this to my test global.window.innerWidth = 300; with no success
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:17
Top Results From Across the Web
Figuring out how to mock the window size changing for a react ...
I'm fairly new to testing and I'm trying to figure out a way I can write a test that resizes the window and...
Read more >Mock window events and properties in Jest - Alex Boffey
Here is the final test suite testing that useWindowSize correctly reads from window and updates when a resize event happens on window ....
Read more >[Solved]-How to test different screen widths using Jest-Reactjs
You can manually set the window width and height and fire the resize event as needed. Here is an example: comp.js import *...
Read more >Jest / Enzyme – How to test at different viewports - iTecNote
It is possible to simulate a window resize using jsdom by manually setting window.innerWidth and window.innerHeight and firing the resize event. Here is...
Read more >How to test responsiveness in Jest? Please help!! - Reddit
Navbar'; // stimulate different screen width beforeAll(() ... moxios.install(); }); test('Rendered navbar with 700px width', () => { window.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I found a solution, maybe not the prettiest one, but it works. Method
matchMediatakes media-query string as a parameter, so I am just matching it to the one I want to test:@edorivai I am experiencing another issue, close to this one, maybe you can help me out.
I have component which has two
<Media>components, one for mobile media query and another for desktop.When I test it with jest, using your snippet above (
matches: true,) it renders both components, obviously. How can I test, for example, mobile view, so only one component, the one for mobile query, is rendered? I tried solution with settingwindow.innerWidthand then triggeringresizeevent, but it doesn’t help.