Request/Fx: Specs for Class.Thenable integration.
[mootools.git] / Specs / Fx / Fx.js
blobcd535e5dc08f5fc0e5eead0a80e6d9da36615af3
1 /*
2 ---
3 name: Fx
4 requires: ~
5 provides: ~
6 ...
7 */
9 describe('Fx', function(){
11         beforeEach(function(){
12                 this.clock = sinon.useFakeTimers();
13         });
15         afterEach(function(){
16                 this.clock.reset();
17                 this.clock.restore();
18         });
20         Object.each(Fx.Transitions, function(value, transition){
21                 if (transition == 'extend') return;
23                 it('should start a Fx and call the onComplete event with ' + transition + ' as timing function', function(){
24                         var onComplete = sinon.spy(),
25                                 onStart = sinon.spy();
27                         var fx = new Fx({
28                                 duration: 500,
29                                 transition: Fx.Transitions[transition],
30                                 onComplete: onComplete,
31                                 onStart: onStart
32                         });
34                         expect(onStart.called).to.equal(false);
36                         fx.start(10, 20);
38                         this.clock.tick(100);
39                         expect(onStart.called).to.equal(true);
40                         expect(onComplete.called).to.equal(false);
42                         this.clock.tick(1000);
43                         expect(onComplete.called).to.equal(true);
44                 });
45         });
47         it('should cancel a Fx', function(){
48                 var onCancel = sinon.spy();
50                 var fx = new Fx({
51                         duration: 500,
52                         transition: 'sine:in:out',
53                         onCancel: onCancel
54                 });
56                 fx.start();
57                 expect(onCancel.called).to.equal(false);
59                 fx.cancel();
60                 expect(onCancel.called).to.equal(true);
61         });
63         it('should set the computed value', function(){
64                 var FxLog = new Class({
65                         Extends: Fx,
66                         set: function(current){
67                                 this.foo = current;
68                         }
69                 });
71                 var fx = new FxLog({
72                         duration: 1000
73                 }).start(0, 10);
75                 this.clock.tick(2000);
76                 expect(fx.foo).to.equal(10);
77         });
79         it('should pause and resume', function(){
80                 var FxLog = new Class({
81                         Extends: Fx,
82                         set: function(current){
83                                 this.foo = current;
84                         }
85                 });
87                 var fx = new FxLog({
88                         duration: 2000
89                 }).start(0, 1);
91                 this.clock.tick(1000);
93                 var value;
95                 fx.pause();
96                 value = fx.foo;
97                 expect(fx.foo).to.be.greaterThan(0);
98                 expect(fx.foo).to.be.lessThan(1);
100                 this.clock.tick(1000);
102                 expect(fx.foo).to.equal(value);
103                 fx.resume();
105                 this.clock.tick(2000);
106                 expect(fx.foo).to.equal(1);
107         });
109         it('should chain the Fx', function(){
110                 var counter = 0;
111                 var fx = new Fx({
112                         duration: 500,
113                         onComplete: function(){
114                                 counter++;
115                         },
116                         link: 'chain'
117                 });
119                 fx.start().start();
121                 this.clock.tick(1000);
122                 this.clock.tick(1000);
124                 expect(counter).to.equal(2);
125         });
127         it('should cancel the Fx after a new Fx:start with the link = cancel option', function(){
128                 var onCancel = sinon.spy();
130                 var fx = new Fx({
131                         duration: 500,
132                         onCancel: onCancel,
133                         link: 'cancel'
134                 });
136                 fx.start().start();
138                 this.clock.tick(1000);
139                 expect(onCancel.called).to.equal(true);
140         });
142         it('should return the paused state', function(){
143                 var fx = new Fx({
144                         duration: 500
145                 }).start();
147                 expect(fx.isPaused()).to.equal(false);
149                 this.clock.tick(300);
150                 fx.pause();
152                 expect(fx.isPaused()).to.equal(true);
154                 fx.resume();
155                 this.clock.tick(600);
156                 expect(fx.isPaused()).to.equal(false);
157         });
161 describe('Fx (thenable)', function(){
163         beforeEach(function(){
164                 this.fx = new Fx({
165                         duration: 1,
166                         transition: 'sine:in:out'
167                 });
169                 var self = this;
170                 this.onFulfilled = sinon.spy(function(){ self.expectations.apply(self, arguments); });
171                 this.onRejected = sinon.spy(function(){ self.expectations.apply(self, arguments); });
173                 this.fx.then(this.onFulfilled, this.onRejected);
174         });
176         it('should fulfill when completed', function(done){
177                 this.fx.start(10, 20);
179                 expect(this.onRejected.called).to.equal(false);
180                 expect(this.onFulfilled.called).to.equal(false);
182                 this.expectations = function(){
183                         var error;
184                         try {
185                                 expect(this.onRejected.called).to.equal(false);
186                                 expect(this.onFulfilled.called).to.equal(true);
187                                 expect(this.onFulfilled.args[0][0]).to.equal(null);
188                         } catch (thrown){
189                                 error = thrown;
190                         }
191                         done(error);
192                 }
193         });
195         it('should reject when cancelled', function(done){
196                 this.fx.start();
198                 expect(this.onFulfilled.called).to.equal(false);
199                 expect(this.onRejected.called).to.equal(false);
201                 this.fx.cancel();
203                 this.expectations = function(){
204                         var error;
205                         try {
206                                 expect(this.onFulfilled.called).to.equal(false);
207                                 expect(this.onRejected.called).to.equal(true);
208                                 expect(this.onRejected.args[0][0]).to.equal(this.fx);
209                         } catch (thrown){
210                                 error = thrown;
211                         }
212                         done(error);
213                 }
214         });