summaryrefslogtreecommitdiffstats
path: root/spec/RAFPlugin.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-05 19:02:35 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-05 19:02:35 +0100
commit2f8d32a1f4fbe51c4e6ac381d5d663d8f11fc1f9 (patch)
tree4abb508477aa4b6b811f89731011661783a2225c /spec/RAFPlugin.js
parent4049e2e6361d5ed9120f1edd02ef96ecc138fa6d (diff)
downloadsvg.js-2f8d32a1f4fbe51c4e6ac381d5d663d8f11fc1f9.tar.gz
svg.js-2f8d32a1f4fbe51c4e6ac381d5d663d8f11fc1f9.zip
Linted all files, upgraded all dependencies
Diffstat (limited to 'spec/RAFPlugin.js')
-rw-r--r--spec/RAFPlugin.js84
1 files changed, 84 insertions, 0 deletions
diff --git a/spec/RAFPlugin.js b/spec/RAFPlugin.js
new file mode 100644
index 0000000..2dea383
--- /dev/null
+++ b/spec/RAFPlugin.js
@@ -0,0 +1,84 @@
+/**
+ * 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) {
+ this.realRAF = global.requestAnimationFrame,
+ this.realCAF = global.cancelAnimationFrame,
+ this.realPerf = global.performance,
+ this.nextTime = 0
+
+ var _this = this
+
+ /**
+ * Mock for window.requestAnimationFrame
+ */
+ this.mockRAF = function(fn) {
+ if (typeof fn !== 'function') {
+ throw new Error('You should pass a function to requestAnimationFrame');
+ }
+
+ callbacks[index++] = fn;
+
+ return index;
+ };
+
+ /**
+ * Mock for window.cancelAnimationFrame
+ */
+ this.mockCAF = function(requestID) {
+ callbacks.splice(requestID, 1)
+ };
+
+ this.mockPerf = {
+ now: function () {
+ return _this.nextTime
+ }
+ }
+
+ /**
+ * Install request animation frame mocks.
+ */
+ this.install = function() {
+ global.requestAnimationFrame = _this.mockRAF;
+ global.cancelAnimationFrame = _this.mockCAF;
+ global.performance = _this.mockPerf;
+ };
+
+ /**
+ * Uninstall request animation frame mocks.
+ */
+ this.uninstall = function() {
+ global.requestAnimationFrame = _this.realRAF;
+ global.cancelAnimationFrame = _this.realCAF;
+ global.performance = _this.realPerf;
+ _this.nextTime = 0
+ callbacks = []
+ };
+
+ /**
+ * Simulate animation frame readiness.
+ */
+ this.tick = function(dt) {
+ _this.nextTime += (dt || 1)
+
+ var fns = callbacks, fn, i;
+
+ callbacks = [];
+ index = 0;
+
+ for (i in fns) {
+ fn = fns[i];
+ fn(_this.nextTime);
+ }
+ };
+ }
+
+
+ jasmine.RequestAnimationFrame = new MockRAF(window);
+}());