merge: In README, correct path to point to mootools.
[mootools.git] / Specs / Class / Class.Extras.js
blob62704359eedafd69fde5838d6e19c49bc6a41957
1 /*
2 ---
3 name: Class.Extras
4 requires: ~
5 provides: ~
6 ...
7 */
9 var Local = Local || {};
11 describe('Chain', function(){
13         Local.Chain = new Class({
15                 Implements: Chain
17         });
19         it('callChain should not fail when nothing was added to the chain', function(){
20                 var chain = new Local.Chain();
21                 chain.callChain();
22         });
24         it('should pass arguments to the function and return values', function(){
25                 var chain = new Local.Chain();
26                 var arr = [];
27                 chain.chain(function(a, b){
28                         var str = "0" + b + a;
29                         arr.push(str);
30                         return str;
31                 });
32                 chain.chain(function(a, b){
33                         var str = "1" + b + a;
34                         arr.push(str);
35                         return str;
36                 });
37                 var ret;
38                 expect(arr).toEqual([]);
39                 ret = chain.callChain("a", "A");
40                 expect(ret).toEqual("0Aa");
41                 expect(arr).toEqual(["0Aa"]);
43                 ret = chain.callChain("b", "B");
44                 expect(ret).toEqual("1Bb");
45                 expect(arr).toEqual(["0Aa", "1Bb"]);
47                 ret = chain.callChain();
48                 expect(ret).toEqual(false);
49                 expect(arr).toEqual(["0Aa", "1Bb"]);
50         });
52         it('should chain any number of functions', function(){
53                 var chain = new Local.Chain();
54                 var arr = [];
56                 chain.chain(function(){
57                         arr.push(0);
58                 }, function(){
59                         arr.push(1);
60                 });
62                 expect(arr).toEqual([]);
63                 chain.callChain();
64                 expect(arr).toEqual([0]);
65                 chain.chain(function(){
66                         arr.push(2);
67                 });
68                 chain.callChain();
69                 expect(arr).toEqual([0, 1]);
70                 chain.callChain();
71                 expect(arr).toEqual([0, 1, 2]);
72                 chain.callChain();
73                 expect(arr).toEqual([0, 1, 2]);
74         });
76         it('should allow an array of functions', function(){
77                 var chain = new Local.Chain();
78                 var arr = [];
80                 chain.chain([function(){
81                         arr.push(0);
82                 }, function(){
83                         arr.push(1);
84                 }, function(){
85                         arr.push(2);
86                 }]);
88                 expect(arr).toEqual([]);
89                 chain.callChain();
90                 expect(arr).toEqual([0]);
91                 chain.callChain();
92                 expect(arr).toEqual([0, 1]);
93                 chain.callChain();
94                 expect(arr).toEqual([0, 1, 2]);
95                 chain.callChain();
96                 expect(arr).toEqual([0, 1, 2]);
97         });
99         it('each instance should have its own chain', function(){
100                 var foo = new Local.Chain();
101                 var bar = new Local.Chain();
102                 foo.val = "F";
103                 bar.val = "B";
104                 foo.chain(function(){
105                         this.val += 'OO';
106                 });
107                 bar.chain(function(){
108                         this.val += 'AR';
109                 });
110                 expect(foo.val).toEqual('F');
111                 expect(bar.val).toEqual('B');
112                 foo.callChain();
113                 bar.callChain();
114                 expect(foo.val).toEqual('FOO');
115                 expect(bar.val).toEqual('BAR');
116         });
118         it('should be able to clear the chain', function(){
119                 var called;
120                 var fn = function(){
121                         called = true;
122                 };
124                 var chain = new Local.Chain();
125                 chain.chain(fn, fn, fn, fn);
127                 chain.callChain();
128                 expect(called).toBeTruthy();
129                 called = false;
131                 chain.clearChain();
133                 chain.callChain();
134                 expect(called).toBeFalsy();
135                 called = false;
136         });
138         it('should be able to clear the chain from within', function(){
139                 var foo = new Local.Chain();
141                 var test = 0;
142                 foo.chain(function(){
143                         test++;
144                         foo.clearChain();
145                 }).chain(function(){
146                         test++;
147                 }).callChain();
149                 expect(test).toEqual(1);
150         });
154 var fire = 'fireEvent', create = function(){
155         return new Events();
158 describe('Events API: Mixin', function(){
160         beforeEach(function(){
161                 Local.called = 0;
162                 Local.fn = function(){
163                         return Local.called++;
164                 };
165         });
167         it('should add an Event to the Class', function(){
168                 var object = create();
170                 object.addEvent('event', Local.fn)[fire]('event');
172                 expect(Local.called).toEqual(1);
173         });
175         it('should add multiple Events to the Class', function(){
176                 create().addEvents({
177                         event1: Local.fn,
178                         event2: Local.fn
179                 })[fire]('event1')[fire]('event2');
181                 expect(Local.called).toEqual(2);
182         });
184         it('should remove a specific method for an event', function(){
185                 var object = create();
186                 var x = 0, fn = function(){ x++; };
188                 object.addEvent('event', Local.fn).addEvent('event', fn).removeEvent('event', Local.fn)[fire]('event');
190                 expect(x).toEqual(1);
191                 expect(Local.called).toEqual(0);
192         });
194         it('should remove an event and its methods', function(){
195                 var object = create();
196                 var x = 0, fn = function(){ x++; };
198                 object.addEvent('event', Local.fn).addEvent('event', fn).removeEvents('event')[fire]('event');
200                 expect(x).toEqual(0);
201                 expect(Local.called).toEqual(0);
202         });
204         it('should remove all events', function(){
205                 var object = create();
206                 var x = 0, fn = function(){ x++; };
208                 object.addEvent('event1', Local.fn).addEvent('event2', fn).removeEvents();
209                 object[fire]('event1')[fire]('event2');
211                 // Should not fail
212                 object.removeEvents()[fire]('event1')[fire]('event2');
214                 expect(x).toEqual(0);
215                 expect(Local.called).toEqual(0);
216         });
218         it('should remove events with an object', function(){
219                 var object = create();
220                 var events = {
221                         event1: Local.fn,
222                         event2: Local.fn
223                 };
225                 object.addEvent('event1', function(){ Local.fn(); }).addEvents(events)[fire]('event1');
226                 expect(Local.called).toEqual(2);
228                 object.removeEvents(events);
229                 object[fire]('event1');
230                 expect(Local.called).toEqual(3);
232                 object[fire]('event2');
233                 expect(Local.called).toEqual(3);
234         });
236         it('should remove an event immediately', function(){
237                 var object = create();
239                 var methods = [];
241                 var three = function(){
242                         methods.push(3);
243                 };
245                 object.addEvent('event', function(){
246                         methods.push(1);
247                         this.removeEvent('event', three);
248                 }).addEvent('event', function(){
249                         methods.push(2);
250                 }).addEvent('event', three);
252                 object[fire]('event');
253                 expect(methods).toEqual([1, 2]);
255                 object[fire]('event');
256                 expect(methods).toEqual([1, 2, 1, 2]);
257         });
259         it('should be able to remove itself', function(){
260                 var object = create();
262                 var methods = [];
264                 var one = function(){
265                         object.removeEvent('event', one);
266                         methods.push(1);
267                 };
268                 var two = function(){
269                         object.removeEvent('event', two);
270                         methods.push(2);
271                 };
272                 var three = function(){
273                         methods.push(3);
274                 };
276                 object.addEvent('event', one).addEvent('event', two).addEvent('event', three);
278                 object[fire]('event');
279                 expect(methods).toEqual([1, 2, 3]);
281                 object[fire]('event');
282                 expect(methods).toEqual([1, 2, 3, 3]);
283         });
287 describe('Options Class', function(){
289         Local.OptionsTest = new Class({
290                 Implements: [Options, Events],
292                 options: {
293                         a: 1,
294                         b: 2
295                 },
297                 initialize: function(options){
298                         this.setOptions(options);
299                 }
300         });
302         it('should set options', function(){
303                 var myTest = new Local.OptionsTest({a: 1, b: 3});
304                 expect(myTest.options).not.toEqual(undefined);
305         });
307         it('should override default options', function(){
308                 var myTest = new Local.OptionsTest({a: 3, b: 4});
309                 expect(myTest.options.a).toEqual(3);
310                 expect(myTest.options.b).toEqual(4);
311         });
315 describe('Options Class with Events', function(){
317         beforeEach(function(){
318                 Local.OptionsTest = new Class({
319                         Implements: [Options, Events],
321                         options: {
322                                 onEvent1: function(){
323                                         return true;
324                                 },
325                                 onEvent2: function(){
326                                         return false;
327                                 }
328                         },
330                         initialize: function(options){
331                                 this.setOptions(options);
332                         }
333                 });
334         });
336         it('should add events in the options object if class has implemented the Events class', function(){
337                 var myTest = new Local.OptionsTest({
338                         onEvent2: function(){
339                                 return true;
340                         },
342                         onEvent3: function(){
343                                 return true;
344                         }
345                 });
347                 expect(myTest.$events.event1.length).toEqual(1);
348                 expect(myTest.$events.event2.length).toEqual(1);
349                 expect(myTest.$events.event3.length).toEqual(1);
350         });
354 describe('setOptions', function(){
356         it('should allow to pass the document', function(){
358                 var A = new Class({
360                         Implements: Options,
362                         initialize: function(options){
363                                 this.setOptions(options);
364                         }
366                 });
368                 expect(new A({document: document}).options.document == document).toBeTruthy();
369         });