9 var Local = Local || {};
11 describe('Chain', function(){
13 Local.Chain = new Class({
19 it('callChain should not fail when nothing was added to the chain', function(){
20 var chain = new Local.Chain();
24 it('should pass arguments to the function and return values', function(){
25 var chain = new Local.Chain();
27 chain.chain(function(a, b){
28 var str = "0" + b + a;
32 chain.chain(function(a, b){
33 var str = "1" + b + a;
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"]);
52 it('should chain any number of functions', function(){
53 var chain = new Local.Chain();
56 chain.chain(function(){
62 expect(arr).toEqual([]);
64 expect(arr).toEqual([0]);
65 chain.chain(function(){
69 expect(arr).toEqual([0, 1]);
71 expect(arr).toEqual([0, 1, 2]);
73 expect(arr).toEqual([0, 1, 2]);
76 it('should allow an array of functions', function(){
77 var chain = new Local.Chain();
80 chain.chain([function(){
88 expect(arr).toEqual([]);
90 expect(arr).toEqual([0]);
92 expect(arr).toEqual([0, 1]);
94 expect(arr).toEqual([0, 1, 2]);
96 expect(arr).toEqual([0, 1, 2]);
99 it('each instance should have its own chain', function(){
100 var foo = new Local.Chain();
101 var bar = new Local.Chain();
104 foo.chain(function(){
107 bar.chain(function(){
110 expect(foo.val).toEqual('F');
111 expect(bar.val).toEqual('B');
114 expect(foo.val).toEqual('FOO');
115 expect(bar.val).toEqual('BAR');
118 it('should be able to clear the chain', function(){
124 var chain = new Local.Chain();
125 chain.chain(fn, fn, fn, fn);
128 expect(called).toBeTruthy();
134 expect(called).toBeFalsy();
138 it('should be able to clear the chain from within', function(){
139 var foo = new Local.Chain();
142 foo.chain(function(){
149 expect(test).toEqual(1);
154 var fire = 'fireEvent', create = function(){
158 describe('Events API: Mixin', function(){
160 beforeEach(function(){
162 Local.fn = function(){
163 return Local.called++;
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);
175 it('should add multiple Events to the Class', function(){
179 })[fire]('event1')[fire]('event2');
181 expect(Local.called).toEqual(2);
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);
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);
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');
212 object.removeEvents()[fire]('event1')[fire]('event2');
214 expect(x).toEqual(0);
215 expect(Local.called).toEqual(0);
218 it('should remove events with an object', function(){
219 var object = create();
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);
236 it('should remove an event immediately', function(){
237 var object = create();
241 var three = function(){
245 object.addEvent('event', function(){
247 this.removeEvent('event', three);
248 }).addEvent('event', function(){
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]);
259 it('should be able to remove itself', function(){
260 var object = create();
264 var one = function(){
265 object.removeEvent('event', one);
268 var two = function(){
269 object.removeEvent('event', two);
272 var three = function(){
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]);
287 describe('Options Class', function(){
289 Local.OptionsTest = new Class({
290 Implements: [Options, Events],
297 initialize: function(options){
298 this.setOptions(options);
302 it('should set options', function(){
303 var myTest = new Local.OptionsTest({a: 1, b: 3});
304 expect(myTest.options).not.toEqual(undefined);
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);
315 describe('Options Class with Events', function(){
317 beforeEach(function(){
318 Local.OptionsTest = new Class({
319 Implements: [Options, Events],
322 onEvent1: function(){
325 onEvent2: function(){
330 initialize: function(options){
331 this.setOptions(options);
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(){
342 onEvent3: function(){
347 expect(myTest.$events.event1.length).toEqual(1);
348 expect(myTest.$events.event2.length).toEqual(1);
349 expect(myTest.$events.event3.length).toEqual(1);
354 describe('setOptions', function(){
356 it('should allow to pass the document', function(){
362 initialize: function(options){
363 this.setOptions(options);
368 expect(new A({document: document}).options.document == document).toBeTruthy();