Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Created June 13, 2018 19:20
Show Gist options
  • Save remarkablemark/8e5a247a663db40f2a2abe420ac43234 to your computer and use it in GitHub Desktop.
Save remarkablemark/8e5a247a663db40f2a2abe420ac43234 to your computer and use it in GitHub Desktop.
How to spy on window resize event in test.
const spy = jest.fn(); // or `jasmine.createSpy()`
const testWidth = 420;
beforeAll(() => {
window.addEventListener('resize', spy);
});
it('does not fire resize event by default', () => {
expect(spy).not.toHaveBeenCalled();
expect(window.innerWidth).not.toBe(testWidth);
});
describe('when resize event is fired', () => {
beforeAll(() => {
window.innerWidth = testWidth;
window.dispatchEvent(new Event('resize'));
// const resizeEvent = document.createEvent('Event');
// resizeEvent.initEvent('resize', true, true);
// window.dispatchEvent(resizeEvent);
});
it('updates the window width', () => {
expect(spy).toHaveBeenCalled();
expect(window.innerWidth).toBe(testWidth);
});
});
@TangoPJ
Copy link

TangoPJ commented Feb 11, 2022

What did you mean on strings 17-19?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment