- fixed a spec. Class:implement doesnt accept multiple arguments anymore.
[mootools/dkf.git] / Specs / Class / Class.Extras.js
blob493ed1224cfdd60b78c1f1842df9cf0cb24176bc
1 /*
2 Script: Class.Extras.js
3         Specs for Class.Extras.js
5 License:
6         MIT-style license.
7 */
9 var Local = Local || {};
11 describe("Chain Class", {
13         "before all": function(){
14                 Local.Chain = new Class({
16                         Implements: Chain
18                 });
19         },
21         "callChain should not fail when nothing was added to the chain": function(){
22                 var chain = new Local.Chain();
23                 chain.callChain();
24         },
26         "should pass arguments to the function and return values": function(){
27                 var chain = new Local.Chain();
28                 var arr = [];
29                 chain.chain(function(a, b){
30                         var str = "0" + b + a;
31                         arr.push(str);
32                         return str;
33                 });
34                 chain.chain(function(a, b){
35                         var str = "1" + b + a;
36                         arr.push(str);
37                         return str;
38                 });
39                 var ret;
40                 value_of(arr).should_be([]);
41                 ret = chain.callChain("a", "A");
42                 value_of(ret).should_be("0Aa");
43                 value_of(arr).should_be(["0Aa"]);
45                 ret = chain.callChain("b", "B");
46                 value_of(ret).should_be("1Bb");
47                 value_of(arr).should_be(["0Aa", "1Bb"]);
49                 ret = chain.callChain();
50                 value_of(ret).should_be(false);
51                 value_of(arr).should_be(["0Aa", "1Bb"]);
52         },
54         "should chain any number of functions": function(){
55                 var chain = new Local.Chain();
56                 var arr = [];
58                 chain.chain(function(){
59                         arr.push(0);
60                 }, function(){
61                         arr.push(1);
62                 });
64                 value_of(arr).should_be([]);
65                 chain.callChain();
66                 value_of(arr).should_be([0]);
67                 chain.chain(function(){
68                         arr.push(2);
69                 });
70                 chain.callChain();
71                 value_of(arr).should_be([0, 1]);
72                 chain.callChain();
73                 value_of(arr).should_be([0, 1, 2]);
74                 chain.callChain();
75                 value_of(arr).should_be([0, 1, 2]);
76         },
78         "should allow an array of functions": function(){
79                 var chain = new Local.Chain();
80                 var arr = [];
82                 chain.chain([function(){
83                         arr.push(0);
84                 }, function(){
85                         arr.push(1);
86                 }, function(){
87                         arr.push(2);
88                 }]);
90                 value_of(arr).should_be([]);
91                 chain.callChain();
92                 value_of(arr).should_be([0]);
93                 chain.callChain();
94                 value_of(arr).should_be([0, 1]);
95                 chain.callChain();
96                 value_of(arr).should_be([0, 1, 2]);
97                 chain.callChain();
98                 value_of(arr).should_be([0, 1, 2]);
99         },
101         "each instance should have its own chain": function(){
102                 var foo = new Local.Chain();
103                 var bar = new Local.Chain();
104                 foo.val = "F";
105                 bar.val = "B";
106                 foo.chain(function(){
107                         this.val += 'OO';
108                 });
109                 bar.chain(function(){
110                         this.val += 'AR';
111                 });
112                 value_of(foo.val).should_be('F');
113                 value_of(bar.val).should_be('B');
114                 foo.callChain();
115                 bar.callChain();
116                 value_of(foo.val).should_be('FOO');
117                 value_of(bar.val).should_be('BAR');
118         }
123 describe("Events Class", {
125         "before all": function(){
126                 Local.EventsTest = new Class({
127                         Implements: Events,
129                         called: 0,
131                         initialize: function(){
132                                 this.called = 0;
133                         }
134                 });
135         },
137         "before each": function(){
138                 Local.called = 0;
139                 Local.fn = function(){
140                         return Local.called++;
141                 };
142         },
144         "should add an Event to the Class": function(){
145                 var myTest = new Local.EventsTest();
146                 myTest.addEvent("event", Local.fn);
148                 var events = myTest.$events;
149                 var myEvent = events["event"];
150                 value_of(myEvent).should_not_be(undefined);
151                 value_of(myEvent.contains(Local.fn)).should_be_true();
152         },
154         "should add multiple Events to the Class": function(){
155                 var myTest = new Local.EventsTest();
156                 myTest.addEvents({
157                         "event1": Local.fn,
158                         "event2": Local.fn
159                 });
161                 var events = myTest.$events;
162                 var myEvent1 = events["event1"];
163                 value_of(myEvent1).should_not_be(undefined);
164                 value_of(myEvent1.contains(Local.fn)).should_be_true();
166                 var myEvent2 = events["event2"];
167                 value_of(myEvent2).should_not_be(undefined);
168                 value_of(myEvent2.contains(Local.fn)).should_be_true();
169         },
171         "should add an internal event": function(){
172                 var myTest = new Local.EventsTest();
173                 myTest.addEvent("internal", Local.fn, true);
175                 var events = myTest.$events;
176                 var myEvent = events["internal"];
177                 value_of(myEvent).should_not_be(undefined);
178                 value_of(myEvent.contains(Local.fn)).should_be_true();
179                 value_of(myEvent[0].internal).should_be_true();
180         },
182         "should remove a specific method for an event": function(){
183                 var myTest = new Local.EventsTest();
184                 var fn = function(){ return true; };
185                 myTest.addEvent("event", Local.fn);
186                 myTest.addEvent("event", fn);
187                 myTest.removeEvent("event", Local.fn);
189                 var events = myTest.$events;
190                 var myEvent = events["event"];
191                 value_of(myEvent).should_not_be(undefined);
192                 value_of(myEvent.contains(fn)).should_be_true();
193         },
195         "should remove an event and its methods": function(){
196                 var myTest = new Local.EventsTest();
197                 var fn = function(){ return true; };
198                 myTest.addEvent("event", Local.fn);
199                 myTest.addEvent("event", fn);
200                 myTest.removeEvents("event");
202                 var events = myTest.$events;
203                 value_of(events["event"].length).should_be(0);
204         },
206         "should remove all events": function(){
207                 var myTest = new Local.EventsTest();
208                 var fn = function(){ return true; };
209                 myTest.addEvent("event1", Local.fn);
210                 myTest.addEvent("event2", fn);
211                 myTest.removeEvents();
213                 var events = myTest.$events;
214                 value_of(events["event1"].length).should_be(0);
215                 value_of(events["event2"].length).should_be(0);
216         },
218         "should remove events with an object": function(){
219                 var myTest = new Local.EventsTest();
220                 var events = {
221                         event1: Local.fn.create(),
222                         event2: Local.fn.create()
223                 };
224                 myTest.addEvent('event1', Local.fn.create()).addEvents(events);
225                 myTest.fireEvent('event1');
226                 value_of(Local.called).should_be(2);
227                 myTest.removeEvents(events);
228                 myTest.fireEvent('event1');
229                 value_of(Local.called).should_be(3);
230                 myTest.fireEvent('event2');
231                 value_of(Local.called).should_be(3);
232         }
236 describe("Options Class", {
238         "before all": function(){
239                 Local.OptionsTest = new Class({
240                         Implements: Options,
242                         initialize: function(options){
243                                 this.setOptions(options);
244                         }
245                 });
246         },
248         "should set options": function(){
249                 var myTest = new Local.OptionsTest({ a: 1, b: 2});
250                 value_of(myTest.options).should_not_be(undefined);
251         },
253         "should override default options": function(){
254                 Local.OptionsTest.implement({
255                         options: {
256                                 a: 1,
257                                 b: 2
258                         }
259                 });
260                 var myTest = new Local.OptionsTest({a: 3, b: 4});
261                 value_of(myTest.options.a).should_be(3);
262                 value_of(myTest.options.b).should_be(4);
263         },
265         "should add events in the options object if class has implemented the Events class": function(){
266                 Local.OptionsTest.implement(new Events).implement({
267                         options: {
268                                 onEvent1: function(){
269                                         return true;
270                                 },
271                                 onEvent2: function(){
272                                         return false;
273                                 }
274                         }
275                 });
276                 var myTest = new Local.OptionsTest({
277                         onEvent3: function(){
278                                 return true;
279                         }
280                 });
281                 var events = myTest.$events;
282                 value_of(events).should_not_be(undefined);
283                 value_of(events["event1"].length).should_be(1);
284                 value_of(events["event2"].length).should_be(1);
285                 value_of(events["event3"].length).should_be(1);
286         }