summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorSaivan <savian@me.com>2018-05-22 16:50:56 +1000
committerSaivan <savian@me.com>2018-05-22 16:50:56 +1000
commita41c1496ba72796676085a6ade7a0fecb4d5b4ab (patch)
tree283069f3190f922c44764746a0c2ed34bbdc1315 /spec
parent69b4111399015e1ad5ba28951bb264bafea21861 (diff)
parent837cc126d35dfa1387d0073fdb7f45fa36543f4b (diff)
downloadsvg.js-a41c1496ba72796676085a6ade7a0fecb4d5b4ab.tar.gz
svg.js-a41c1496ba72796676085a6ade7a0fecb4d5b4ab.zip
Added a few more use cases and such
Diffstat (limited to 'spec')
-rw-r--r--spec/SpecRunner.html5
-rw-r--r--spec/lib/RAFPlugin.js82
-rw-r--r--spec/spec/animator.js49
-rw-r--r--spec/spec/morphing.js67
4 files changed, 199 insertions, 4 deletions
diff --git a/spec/SpecRunner.html b/spec/SpecRunner.html
index d64d930..7c69811 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">
@@ -53,7 +54,8 @@
<!-- include spec files here... -->
- <!-- <script src="spec/adopter.js"></script>
+ <!--
+ <script src="spec/adopter.js"></script>
<script src="spec/arrange.js"></script>
<script src="spec/array.js"></script>
<script src="spec/bare.js"></script>
@@ -104,5 +106,6 @@
+ <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 7e79c99..1bc2cd6 100644
--- a/spec/spec/morphing.js
+++ b/spec/spec/morphing.js
@@ -3,6 +3,34 @@ describe('Morphing', function () {
describe('constructors', function () {
+ it('SVG.Morphable with Number', function () {
+ var morpher = new SVG.Morphable().from(10).to(5)
+
+ expect(morpher instanceof SVG.Morphable).toBe(true)
+ expect(morpher.type()).toBe(SVG.Number)
+ expect(morpher.at(0.5) instanceof SVG.Number).toBe(true)
+ expect(morpher.at(0.5).valueOf()).toBe(7.5)
+ })
+
+ it('SVG.Morphable with String', function () {
+ var morpher = new SVG.Morphable().from('foo').to('bar')
+
+ expect(morpher instanceof SVG.Morphable).toBe(true)
+ expect(morpher.type()).toBe(SVG.Morphable.NonMorphable)
+ expect(morpher.at(0.5) instanceof SVG.Morphable.NonMorphable).toBe(true)
+ expect(morpher.at(0.5).valueOf()).toBe('foo')
+ expect(morpher.at(1).valueOf()).toBe('bar')
+ })
+
+ it('SVG.Morphable with Object', function () {
+ var morpher = new SVG.Morphable().from({a:5, b: 10}).to({a: 10, b: 20})
+
+ expect(morpher instanceof SVG.Morphable).toBe(true)
+ expect(morpher.type()).toBe(SVG.Morphable.ObjectBag)
+ expect(morpher.at(0.5) instanceof Object).toBe(true)
+ expect(morpher.at(0.5).valueOf()).toEqual(jasmine.objectContaining({a: 7.5, b: 15}))
+ })
+
it(`Creates a morphable out of an SVG.Number`, function () {
var morpher = new SVG.Number(5).to(10)
@@ -79,9 +107,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, translateX: 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)
@@ -99,4 +124,40 @@ describe('Morphing', function () {
expect(morpher.at(0.5).valueOf()).toEqual(jasmine.objectContaining({a: 7.5, b: 15}))
})
})
+
+ describe('from()', function () {
+ it('sets the type of the runner', function () {
+ var morpher = new SVG.Morphable().from(5)
+ expect(this.type()).toBe(SVG.Number)
+ })
+
+ it('sets the from attribute to an array representation of the morphable type', function () {
+ var morpher = new SVG.Morphable().from(5)
+ expect(this.from()).toEqual(jasmine.arrayContaining([5]))
+ })
+ })
+
+ describe('type()', function () {
+ it('sets the type of the runner', function () {
+ var morpher = new SVG.Morphable().type(SVG.Number)
+ expect(this._type).toBe(SVG.Number)
+ })
+
+ it('gets the type of the runner', function () {
+ var morpher = new SVG.Morphable().type(SVG.Number)
+ expect(this.type()).toBe(SVG.Number)
+ })
+ })
+
+ describe('to()', function () {
+ it('sets the type of the runner', function () {
+ var morpher = new SVG.Morphable().to(5)
+ expect(this.type()).toBe(SVG.Number)
+ })
+
+ it('sets the from attribute to an array representation of the morphable type', function () {
+ var morpher = new SVG.Morphable().to(5)
+ expect(this.to()).toEqual(jasmine.arrayContaining([5]))
+ })
+ })
})