diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/SpecRunner.html | 4 | ||||
-rw-r--r-- | spec/lib/RAFPlugin.js | 82 | ||||
-rw-r--r-- | spec/spec/animator.js | 49 | ||||
-rw-r--r-- | spec/spec/morphing.js | 3 |
4 files changed, 135 insertions, 3 deletions
diff --git a/spec/SpecRunner.html b/spec/SpecRunner.html index 5cd5f4f..52b4950 100644 --- a/spec/SpecRunner.html +++ b/spec/SpecRunner.html @@ -10,6 +10,7 @@ <script src="lib/jasmine-2.6.0/jasmine.js"></script> <script src="lib/jasmine-2.6.0/jasmine-html.js"></script> <script src="lib/jasmine-2.6.0/boot.js"></script> + <script src="lib/RAFPlugin.js"></script> <link rel="stylesheet" href="fixtures/fixture.css"> @@ -52,6 +53,7 @@ <!-- include spec files here... --> + <!-- <script src="spec/adopter.js"></script> <script src="spec/arrange.js"></script> <script src="spec/array.js"></script> @@ -95,6 +97,8 @@ <script src="spec/use.js"></script> <script src="spec/utils.js"></script> <script src="spec/viewbox.js"></script> + --> + <script src="spec/animator.js"></script> </body> </html> diff --git a/spec/lib/RAFPlugin.js b/spec/lib/RAFPlugin.js new file mode 100644 index 0000000..fefdda6 --- /dev/null +++ b/spec/lib/RAFPlugin.js @@ -0,0 +1,82 @@ +/** + * 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; + }; + + /** + * 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); +}()); diff --git a/spec/spec/animator.js b/spec/spec/animator.js new file mode 100644 index 0000000..a331c16 --- /dev/null +++ b/spec/spec/animator.js @@ -0,0 +1,49 @@ +describe('SVG.Animator', function () { + + beforeEach(function () { + jasmine.RequestAnimationFrame.install() + SVG.Animator.timer = jasmine.RequestAnimationFrame.mockPerf + }) + + afterEach(function () { + jasmine.RequestAnimationFrame.uninstall() + SVG.Animator.timer = jasmine.RequestAnimationFrame.realPerf + }) + + describe('timeout()', function () { + it('calls a function after a specific time', function () { + + var spy = jasmine.createSpy('tester') + var id = SVG.Animator.timeout(spy, 100) + + jasmine.RequestAnimationFrame.tick(99) + expect(spy).not.toHaveBeenCalled() + jasmine.RequestAnimationFrame.tick() + expect(spy).toHaveBeenCalled() + }) + }) + + describe('cancelTimeout()', function () { + it('cancels a timeout which was created with timeout()', function () { + var spy = jasmine.createSpy('tester') + var id = SVG.Animator.timeout(spy, 100) + SVG.Animator.cancelTimeout(id) + + expect(spy).not.toHaveBeenCalled() + jasmine.RequestAnimationFrame.tick(100) + expect(spy).not.toHaveBeenCalled() + }) + }) + + describe('frame()', function () { + it('calls a function at the next animationFrame', function () { + var spy = jasmine.createSpy('tester') + + SVG.Animator.frame(spy) + expect(spy).not.toHaveBeenCalled() + jasmine.RequestAnimationFrame.tick() + expect(spy).toHaveBeenCalled() + }) + }) + +}) diff --git a/spec/spec/morphing.js b/spec/spec/morphing.js index 31279ee..632eaf5 100644 --- a/spec/spec/morphing.js +++ b/spec/spec/morphing.js @@ -79,9 +79,6 @@ describe('Morphing', function () { it(`Creates a morphable out of an SVG.Morphable.TransformBag`, function () { var morpher = new SVG.Morphable.TransformBag({}).to({rotate: 50, translateX: 20}) - // FIXME: SVG.Matrix does now allow translateX to be passed but decompose returns it!!!!! - console.log(new SVG.Morphable.TransformBag({rotate: 50, tx: 20}).valueOf().decompose()) - expect(morpher instanceof SVG.Morphable).toBe(true) expect(morpher.type()).toBe(SVG.Morphable.TransformBag) expect(morpher.at(0.5) instanceof SVG.Morphable.TransformBag).toBe(true) |