Created
June 6, 2018 00:09
-
-
Save armand1m/574ba68c3fe9270484ce443f1fcec7de to your computer and use it in GitHub Desktop.
CRA Jest setup tests file with window.mediaMatch a simple mock
This file contains hidden or 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
/** | |
* window.matchMedia mock | |
* | |
* it also exposes a custom global method called setMatchMediaProperties | |
* that accepts an object as parameter to merge with the default MatchMedia properties. | |
*/ | |
(function matchMediaMock() { | |
const defaultMatchMedia = { | |
matches: false, | |
addListener() {}, | |
removeListener() {} | |
}; | |
if (!global.matchMedia) { | |
global.matchMedia = () => (defaultMatchMedia); | |
} | |
global.setMatchMediaProperties = properties => { | |
global.matchMedia = () => Object.assign({}, defaultMatchMedia, properties); | |
}; | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage testing a React Component with Jest: