Created
March 28, 2022 10:15
-
-
Save publicJorn/ff81a33957ae13ffb211988bfae4cc58 to your computer and use it in GitHub Desktop.
Snippet to test different window sizes in jest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.resizeTo = function resizeTo(width:number, height: number) { | |
Object.assign(this, { | |
innerWidth: width, | |
innerHeight: height, | |
outerWidth: width, | |
outerHeight: height, | |
}).dispatchEvent(new this.Event('resize')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found here: https://spectrum.chat/testing-library/help-react/how-to-set-window-innerwidth-to-test-mobile~70aa9572-b7cc-4397-92f5-a09d75ed24b8
I have this setup like so:
jest.config.js
jest.setup.js
MyComponent.test.tsx
You can also simply put it in the
beforeEach
function, but it's likely you'll need it more often.