2 Script: Class.Extras.js
3 Specs for Class.Extras.js
9 var Local = Local || {};
11 describe("Chain Class", {
13 "before all": function(){
14 Local.Chain = new Class({
21 "callChain should not fail when nothing was added to the chain": function(){
22 var chain = new Local.Chain();
26 "should pass arguments to the function and return values": function(){
27 var chain = new Local.Chain();
29 chain.chain(function(a, b){
30 var str = "0" + b + a;
34 chain.chain(function(a, b){
35 var str = "1" + b + a;
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"]);
54 "should chain any number of functions": function(){
55 var chain = new Local.Chain();
58 chain.chain(function(){
64 value_of(arr).should_be([]);
66 value_of(arr).should_be([0]);
67 chain.chain(function(){
71 value_of(arr).should_be([0, 1]);
73 value_of(arr).should_be([0, 1, 2]);
75 value_of(arr).should_be([0, 1, 2]);
78 "should allow an array of functions": function(){
79 var chain = new Local.Chain();
82 chain.chain([function(){
90 value_of(arr).should_be([]);
92 value_of(arr).should_be([0]);
94 value_of(arr).should_be([0, 1]);
96 value_of(arr).should_be([0, 1, 2]);
98 value_of(arr).should_be([0, 1, 2]);
101 "each instance should have its own chain": function(){
102 var foo = new Local.Chain();
103 var bar = new Local.Chain();
106 foo.chain(function(){
109 bar.chain(function(){
112 value_of(foo.val).should_be('F');
113 value_of(bar.val).should_be('B');
116 value_of(foo.val).should_be('FOO');
117 value_of(bar.val).should_be('BAR');
123 describe("Events Class", {
125 "before all": function(){
126 Local.EventsTest = new Class({
131 initialize: function(){
137 "before each": function(){
139 Local.fn = function(){
140 return Local.called++;
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();
154 "should add multiple Events to the Class": function(){
155 var myTest = new Local.EventsTest();
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();
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();
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();
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);
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);
218 "should remove events with an object": function(){
219 var myTest = new Local.EventsTest();
221 event1: Local.fn.create(),
222 event2: Local.fn.create()
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);
236 describe("Options Class", {
238 "before all": function(){
239 Local.OptionsTest = new Class({
242 initialize: function(options){
243 this.setOptions(options);
248 "should set options": function(){
249 var myTest = new Local.OptionsTest({ a: 1, b: 2});
250 value_of(myTest.options).should_not_be(undefined);
253 "should override default options": function(){
254 Local.OptionsTest.implement({
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);
265 "should add events in the options object if class has implemented the Events class": function(){
266 Local.OptionsTest.implement(new Events).implement({
268 onEvent1: function(){
271 onEvent2: function(){
276 var myTest = new Local.OptionsTest({
277 onEvent3: function(){
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);