Created
August 5, 2018 20:22
-
-
Save armand1m/e10b3362fd461805d1550519d10c35d9 to your computer and use it in GitHub Desktop.
My usual jest setup file
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
/** | |
* prepareDom method | |
* | |
* just creates the DOM structure needed to render | |
* this application before running tests. | |
* | |
* it is needed to test the `index.js` file of the project. | |
*/ | |
(function prepareDom() { | |
document.body.innerHTML = '<div id="root"></div>'; | |
}()); | |
/** | |
* 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); | |
}; | |
}()); | |
/** | |
* window.requestAnimationFrame mock | |
*/ | |
(function requestAnimationFrameMock() { | |
let lastTime = 0; | |
if (!global.requestAnimationFrame) { | |
global.requestAnimationFrame = (callback) => { | |
const currTime = new Date().getTime(); | |
const timeToCall = Math.max(0, 16 - (currTime - lastTime)); | |
const id = global.setTimeout(() => callback(currTime + timeToCall), timeToCall); | |
lastTime = currTime + timeToCall; | |
return id; | |
}; | |
} | |
if (!global.cancelAnimationFrame) { | |
global.cancelAnimationFrame = id => clearTimeout(id); | |
} | |
}()); | |
/** | |
* window.mockOffsetSize utility | |
* | |
* Exposes a method for the developer to set dynamically window sizes. | |
* Useful when testing components that depend on scroll and window size, | |
* such as `react-virtualized`. | |
*/ | |
(function mockOffsetSize() { | |
// Jest runs in JSDom which doesn't support measurements APIs. | |
global.mockOffsetSize = (width, height) => { | |
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', { | |
configurable: true, | |
value: height, | |
}); | |
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { | |
configurable: true, | |
value: width, | |
}); | |
}; | |
}()); | |
/** | |
* window.localStorage mock | |
* | |
* Exposes a localStorage mock to be used on redux thunk actions while | |
* testing ConnectedComponents | |
*/ | |
(function mockLocalStorage() { | |
const localStorageMock = (() => { | |
let store = {}; | |
return { | |
getItem(key) { | |
return store[key] || null; | |
}, | |
setItem(key, value) { | |
store[key] = value.toString(); | |
}, | |
clear() { | |
store = {}; | |
}, | |
}; | |
})(); | |
Object.defineProperty(window, 'localStorage', { | |
value: localStorageMock, | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment