Skip to content

Instantly share code, notes, and snippets.

@ischenkodv
Created February 28, 2015 20:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ischenkodv/43934774f4509fcb5791 to your computer and use it in GitHub Desktop.
Save ischenkodv/43934774f4509fcb5791 to your computer and use it in GitHub Desktop.
Jasmine plugin to test functions with requestAnimationFrame.
/**
* 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