score:6
try this line:
window = object.assign(window, { innerwidth: 500 });
score:36
if you are getting the typescript error message
cannot assign to 'innerwidth' because it is a read-only property.
try:
object.defineproperty(window, 'innerwidth', {writable: true, configurable: true, value: 200})
score:73
using jest and enzyme you can do the following. jest has jsdom baked in. in your tests jest provides the window
object and it is represented by global
(i think that the default window.innerwidth
set by jest is 1024px):
test('test something when the viewport changes.', () => {
// mount the component to test.
const component = mount(<componenttotest/>);
...
// change the viewport to 500px.
global.innerwidth = 500;
// trigger the window resize event.
global.dispatchevent(new event('resize'));
...
// run your assertion.
});
Source: stackoverflow.com
Related Query
- Figuring out how to mock the window size changing for a react component test
- React & Jest, how to test changing state and checking for another component
- How to test the style inside the CSS class for React component using Jest and Enzyme?
- How to have different mock implementations for different test scenarios using the Shallow component Jest/Enzyme?
- How to test for the string '...loading' in React component that uses styled-components?
- How to test style for a React component attribute with Enzyme
- How to mock out sub-components when unit testing a React component with Jest
- How to TEST async calls made in componentDidMount that set the state of React Component
- How can I write a unit test for a react component that calls reduxjs's mapStateToProps?
- react - how do I get the size of a component child elements and reposition them
- How to test the state of a functional component in React with Jest (No Enzyme)
- How can I test a React Hooks component by changing useState
- How do I test the fallback component for the ErrorBoundary?
- Changing the CSS for a React component has effect on all other pages
- How can mock the value of a state and data in my react test
- How can I test css properties for a React component using react-testing-library?
- React : How to stop re-rendering component or making an API calls on router change for the same route
- React testing - How can I mock or inject data in the store to be provided as props to component
- How is an argument for a callback in React being passed from the child component to the parent component?
- How to check the value of a nested React component in a unit test with Enzyme and Jest
- How do I limit the size of my React component and add scrolling?
- react component - change child content on resize of the react element (when width gets to narrow) - not the window size via fixed breakpoints
- How to get full test coverage for the react code
- How do I test a React component that calls a function on its parent, which changes props on the component?
- How to delay the redirect of a React Router Link component for 1 second?
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to Unit test the rendering of a child component in the parent component using react testing library?
- how to mock npm uniqid for react unit test when using Jest?
- How to create a test for a screen that uses the same hook several times? React Testing Library
- How to mock React Context for testing component with jest
More Query from same tag
- Problem with nested objects array formik and handleChange in childComponent
- How to use href in Material-ui
- check state of another functional component
- React Router - Auth check renders blank page
- React Hooks Too many re-renders in pagination with redux
- Routing to specific part of other component in React
- I want to create a meshed table like structure in react
- Create "pages" of steps with Material UI/React Stepper (like Table Pagination)
- How can i define a default value for react-select v1
- react router 4 404 path in case of dynamic routes
- How to display React app in full page always?
- react table sort a column by another column
- How to get the code inside codemirror on Onblur Operation
- How to add custom color theme in nivo chart settings
- How to scroll up while selecting rowsperpage in material UI?
- How to highlight only selected row in a large table set in React?
- How to use PyCharm and WebStorm in the same editor
- How to select the array item (array of objects) based upon a particular attribute
- How to properly enumerate object elements in a state array?
- Use javascript library inside of TypeScript React web app
- How to create customRouteWrapper for Route in React Router v6?
- Sending data from the client to the server and displaying to the terminal using `sockets`
- Not getting route params after navigating via react-router-4
- Creating and pushing new object into react state array
- Override default getstream component styles into NextJS Component
- Show/Hide multiple buttons with ReactJS
- How to create new element and render data each time the data is fetched in react
- error onchange is not function render conditional
- Uncaught SyntaxError: Unexpected token < in dist/index.html
- Text is getting cut off in android for react native