Last active
August 3, 2017 12:33
-
-
Save m3g4p0p/536e79933d5d1e1af948a860927cd1de to your computer and use it in GitHub Desktop.
Replace requestAnimationFrame related API methods with mock object for testing. Extended fork from https://gist.github.com/ischenkodv/43934774f4509fcb5791
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
/** | |
* Replace requestAnimationFrame related API methods with mock object for testing. | |
* Extended fork from https://gist.github.com/ischenkodv/43934774f4509fcb5791 | |
* | |
* @type {Object} | |
*/ | |
const mockAnimationFrame = { | |
original: { | |
requestAnimationFrame: window.requestAnimationFrame, | |
cancelAnimationFrame: window.cancelAnimationFrame, | |
performance: window.performance | |
}, | |
install() { | |
this.fns = [] | |
this.currentTick = 0 | |
this.deltaTick = 1 | |
window.requestAnimationFrame = fn => { | |
const handle = this.fns.length | |
this.fns.push(() => { | |
delete this.fns[handle] | |
fn.call(window, window.performance.now()) | |
}) | |
return handle | |
} | |
window.cancelAnimationFrame = handle => { | |
delete this.fns[handle] | |
} | |
window.performance = { | |
now: () => this.currentTick += this.deltaTick | |
} | |
}, | |
uninstall() { | |
Object.assign(window, this.original) | |
}, | |
tick(times = 1) { | |
this.fns.forEach(fn => { | |
if (typeof fn === 'function') { | |
fn() | |
} | |
}) | |
if (times > 1) { | |
return this.tick(times - 1) | |
} | |
return this | |
} | |
} | |
module.exports = mockAnimationFrame |
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
{ | |
"name": "mock-animation-frame", | |
"version": "0.1.0", | |
"description": "Replace requestAnimationFrame related API methods with mock object for testing.", | |
"main": "mockAnimationFrame.js", | |
"repository": "https://gist.github.com/m3g4p0p/536e79933d5d1e1af948a860927cd1de", | |
"author": "m3g4p0p", | |
"license": "MIT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment