Created
February 28, 2015 20:24
-
-
Save ischenkodv/43934774f4509fcb5791 to your computer and use it in GitHub Desktop.
Jasmine plugin to test functions with requestAnimationFrame.
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
/** | |
* Jasmine RequestAnimationFrame: a set of helpers for testing funcionality | |
* that uses requestAnimationFrame under the Jasmine BDD framework for JavaScript. | |
*/ | |
;(function() { | |
var index = 0, | |
callbacks = {}; | |
function MockRAF(global) { | |
var realRAF = global.requestAnimationFrame, | |
realCAF = global.cancelAnimationFrame; | |
/** | |
* Mock for window.requestAnimationFrame | |
*/ | |
var mockRAF = function(fn) { | |
if (typeof fn !== 'function') { | |
throw new Error('You should pass a function to requestAnimationFrame'); | |
} | |
index++; | |
callbacks[index] = fn; | |
return index; | |
}; | |
/** | |
* Mock for window.cancelAnimationFrame | |
*/ | |
var mockCAF = function(requestID) { | |
delete callbacks[requestID]; | |
}; | |
/** | |
* Install request animation frame mocks. | |
*/ | |
this.install = function() { | |
global.requestAnimationFrame = mockRAF; | |
global.cancelAnimationFrame = mockCAF; | |
}; | |
/** | |
* Uninstall request animation frame mocks. | |
*/ | |
this.uninstall = function() { | |
global.requestAnimationFrame = realRAF; | |
global.cancelAnimationFrame = realCAF; | |
}; | |
/** | |
* Simulate animation frame readiness. | |
*/ | |
this.tick = function() { | |
var fns = callbacks, fn, i; | |
callbacks = {}; | |
for (i in fns) { | |
fn = fns[i]; | |
fn.call(global); | |
} | |
}; | |
} | |
jasmine.RequestAnimationFrame = new MockRAF(window); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment