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.
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…