Created
June 13, 2018 19:20
-
-
Save remarkablemark/8e5a247a663db40f2a2abe420ac43234 to your computer and use it in GitHub Desktop.
How to spy on window resize event in test.
This file contains 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
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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What did you mean on strings 17-19?