Skip to content

Instantly share code, notes, and snippets.

@IvanTorresEdge
Created March 30, 2020 16:31
Show Gist options
  • Save IvanTorresEdge/b3d711ddc73dab8dd0e5e552a9adbf62 to your computer and use it in GitHub Desktop.
Save IvanTorresEdge/b3d711ddc73dab8dd0e5e552a9adbf62 to your computer and use it in GitHub Desktop.
Testing Responsive Layouts with Jest+React Testing Library
import matchMediaPolyfill from 'mq-polyfill';
matchMediaPolyfill(window);
window.matchMedia('(max-width: 640px)');
window.matchMedia('(max-width: 768px)');
window.matchMedia('(max-width: 1024px)');
window.matchMedia('(max-width: 1280px)');
window.resizeTo = function resizeTo(width, height): void {
Object.assign(this, {
innerWidth: width,
innerHeight: height,
outerWidth: width,
outerHeight: height,
}).dispatchEvent(new this.Event('resize'));
};
test('whatever you're testing', () => {
window.resizeTo(768, 1280);
// ...expect yadah yadah
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment