Categories
React Answers

How to mock the window size changing for a React component test with JavaScript?

Spread the love

To mock the window size changing for a React component test with JavaScript, we can mock the window.screen.width property.

For instance, we write

jest.spyOn(window.screen, "width", "get").mockReturnValue(1000);

to call jest.spyOn to mock the window.screen.width getter property.

And we call mockReturnValue to set its return value to 1000.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *