Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / test / unit / spec / Ext-mess.backup
1 describe("Ext-mess", function() {
2     describe("Ext.id", function(){
3         var el;
4         describe("if element passed as first argument is different of document or window", function() {
5             beforeEach(function() {
6                 el = document.createElement("div"); // do not use the Ext.getBody().createChild() method because it call Ext.id
7                 Ext.getBody().appendChild(el);
8             });
9
10             it("should generate an unique id for the element with default prefix ext-gen", function() {
11                 expect(Ext.id(el)).toEqual("ext-gen" + Ext.idSeed);
12             });
13
14             it("should generate an unique id for the element with custom prefix", function() {
15                 var prefix = "nico-yhwh";
16                 expect(Ext.id(el, prefix)).toEqual(prefix + Ext.idSeed);
17             });
18
19             it("should not override existing id", function() {
20                 var id = "unchanged";
21                 el.id = id;
22                 expect(Ext.id(el)).toEqual(id);
23             });
24         });
25
26         describe("if element passed as first argument is document", function() {
27             it("should return Ext.documentId", function() {
28                 expect(Ext.id(document)).toEqual(Ext.documentId);
29             });
30         });
31
32         describe("if element passed as first argument is window", function() {
33             it("should return Ext.windowId", function() {
34                 expect(Ext.id(window)).toEqual(Ext.windowId);
35             });
36         });
37     });
38
39     xdescribe("Ext.repaint", function() {
40         it("should create a mask in the body", function(){
41             var body = Ext.getBody();
42
43             spyOn(Ext, "getBody").andCallThrough();
44             spyOn(body, "createChild").andCallThrough();
45
46             Ext.repaint();
47
48             expect(Ext.getBody).toHaveBeenCalled();
49             expect(body.createChild).toHaveBeenCalledWith({cls: "x-mask x-mask-transparent", tag: "div"});
50         });
51     });
52
53     describe("Ext.destroy", function() {
54         var o1, o2, o3;
55
56         beforeEach(function() {
57             o1 = jasmine.createSpyObj("o1", ["destroy"]);
58
59             o2 = jasmine.createSpyObj("o2", ["destroy"]);
60
61             o3 = jasmine.createSpyObj("o3", ["dest"]);
62
63         });
64
65         it("should destroy an object", function() {
66             Ext.destroy(o1);
67
68             expect(o1.destroy).toHaveBeenCalled();
69         });
70
71         it("should no destroy an object without a destroy method", function() {
72             Ext.destroy(o3);
73
74             expect(o3.dest).not.toHaveBeenCalled();
75         });
76
77         it("should destroy an array of objects", function() {
78             Ext.destroy([o1, o2, o3]);
79
80             expect(o1.destroy).toHaveBeenCalled();
81             expect(o2.destroy).toHaveBeenCalled();
82             expect(o3.dest).not.toHaveBeenCalled();
83         });
84
85         it("should destroy multiple objects", function() {
86             Ext.destroy(o1, o2, o3);
87
88             expect(o1.destroy).toHaveBeenCalled();
89             expect(o2.destroy).toHaveBeenCalled();
90             expect(o3.dest).not.toHaveBeenCalled();
91         });
92
93         it("should remove dom if object is an Ext.element", function() {
94            var el = Ext.getBody().createChild({id: "to_destroy"});
95
96            Ext.destroy(el);
97
98            expect(Ext.fly("to_destroy")).toBeNull();
99         });
100     });
101
102     describe("Ext.each", function() {
103          var itFn;
104
105         beforeEach(function() {
106             itFn = jasmine.createSpy("on itFn");
107         });
108
109         describe("each object", function() {
110             var o;
111
112             beforeEach(function() {
113                 o = {
114                     n1: 11,
115                     n2: 13,
116                     n3: 18
117                 };
118             });
119
120             describe("if itFn does not return false", function() {
121                 beforeEach(function() {
122                     Ext.each(o, itFn);
123                 });
124
125                 it("should call the each function 3 times", function () {
126                     expect(itFn.callCount).toEqual(3);
127                 });
128
129                 it("should call the each function with correct arguments", function () {
130                     expect(itFn.calls[0].args).toEqual(["n1", 11, o]);
131                     expect(itFn.calls[1].args).toEqual(["n2", 13, o]);
132                     expect(itFn.calls[2].args).toEqual(["n3", 18, o]);
133                 });
134             });
135
136             describe("if itFn return false", function() {
137                 beforeEach(function() {
138                     itFn.andReturn(false);
139                     Ext.each(o, itFn);
140                 });
141
142                 it("should stop iteration if function return false", function() {
143                     itFn.andReturn(false);
144
145                     expect(itFn.calls.length).toEqual(1);
146                 });
147             });
148         });
149
150         describe("do nothing on an empty object", function() {
151             var o;
152
153             beforeEach(function() {
154                 o = {};
155                 Ext.each(o, itFn);
156             });
157
158             it("should not call the each function", function () {
159                 expect(itFn).not.toHaveBeenCalled();
160             });
161
162         });
163
164         describe("each array", function() {
165             var arr;
166
167             beforeEach(function() {
168                 arr = [6, 7, 8, 9];
169             });
170
171             describe("if itFn does not return false", function() {
172                 beforeEach(function() {
173                     Ext.each(arr, itFn);
174                 });
175
176                 it("should call the each function 4 times", function () {
177                     expect(itFn.callCount).toEqual(4);
178                 });
179
180                 it("should call the each function with correct arguments", function () {
181                     expect(itFn.calls[0].args).toEqual([6, 0, arr]);
182                     expect(itFn.calls[1].args).toEqual([7, 1, arr]);
183                     expect(itFn.calls[2].args).toEqual([8, 2, arr]);
184                     expect(itFn.calls[3].args).toEqual([9, 3, arr]);
185                 });
186              });
187
188             describe("if itFn return false", function() {
189                 beforeEach(function() {
190                     itFn.andReturn(false);
191                     Ext.each(arr, itFn);
192                 });
193
194                 it("should stop iteration if function return false", function() {
195                     itFn.andReturn(false);
196
197                     expect(itFn.calls.length).toEqual(1);
198                 });
199             });
200         });
201
202         describe("do nothing on an empty array", function() {
203             var arr;
204
205             beforeEach(function() {
206                 arr = [];
207                 Ext.each(arr, itFn);
208             });
209
210             it("should not call the each function", function () {
211                 expect(itFn).not.toHaveBeenCalled();
212             });
213
214         });
215
216         describe("simple cases", function() {
217             it("should work with an array of numbers", function() {
218                 var sum = 0;
219
220                 Ext.each([1, 2, 3, 4], function(val){
221                     sum += val;
222                 });
223
224                 expect(sum).toEqual(10);
225             });
226
227             it("should work with an array of strings", function() {
228                 var str = '';
229
230                 Ext.each(["S", "e", "n", "c", "h", "a"], function(s){
231                     str += s;
232                 });
233
234                 expect(str).toEqual("Sencha");
235             });
236
237             it("should pass index correctly", function() {
238                 var arr = [];
239                 Ext.each([1, 2, 3, 4, 5, 6], function(val, idx){
240                     arr.push(idx);
241                 });
242                 expect(arr).toEqual([0, 1, 2, 3, 4, 5]);
243             });
244
245             it("should work with a non array parameter", function() {
246                 var sum = 0;
247
248                 Ext.each(5, function(num){
249                     sum += num;
250                 });
251
252                 expect(sum).toEqual(5);
253             });
254         });
255
256         describe("more complex cases that need a spy", function() {
257             var eachFn;
258
259             beforeEach(function() {
260                 eachFn = jasmine.createSpy();
261             });
262
263             it("should not run eachFn with an empty array", function() {
264                 Ext.each([], eachFn);
265
266                 expect(eachFn).not.toHaveBeenCalled();
267             });
268
269             it("should not run eachFn with null as first param", function() {
270                 Ext.each(null, eachFn);
271
272                 expect(eachFn).not.toHaveBeenCalled();
273             });
274
275             it("should iterate over NodeLists", function() {
276                 Ext.each(document.getElementsByTagName('body'), eachFn);
277
278                 expect(eachFn).toHaveBeenCalled();
279             });
280
281             it("should stop when function called with each item return false", function() {
282                 eachFn.andCallFake(function(v) {
283                     if (v === 5) {
284                         return false;
285                     }
286                 });
287
288                 Ext.each([1, 2, 3, 4, 5, 6], eachFn);
289
290                 expect(eachFn.callCount).toEqual(5);
291             });
292
293             it("should runfunction called with each item with correct scope", function() {
294
295                 Ext.each([1, 2], eachFn, fakeScope);
296
297                 expect(eachFn.calls[0].object).toBe(fakeScope);
298                 expect(eachFn.calls[1].object).toBe(fakeScope);
299             });
300         });
301
302
303     });
304
305     describe("Ext.iterate", function() {
306         var itFn;
307
308         beforeEach(function() {
309             itFn = jasmine.createSpy();
310         });
311
312         describe("iterate object", function() {
313             var o;
314
315             beforeEach(function() {
316                 o = {
317                     n1: 11,
318                     n2: 13,
319                     n3: 18
320                 };
321             });
322
323             describe("if itFn does not return false", function() {
324                 beforeEach(function() {
325                     Ext.iterate(o, itFn);
326                 });
327
328                 it("should call the iterate function 3 times", function () {
329                     expect(itFn.callCount).toEqual(3);
330                 });
331
332                 it("should call the iterate function with correct arguments", function () {
333                     expect(itFn.calls[0].args).toEqual(["n1", 11, o]);
334                     expect(itFn.calls[1].args).toEqual(["n2", 13, o]);
335                     expect(itFn.calls[2].args).toEqual(["n3", 18, o]);
336                 });
337             });
338
339             describe("if itFn return false", function() {
340                 beforeEach(function() {
341                     itFn.andReturn(false);
342                     Ext.iterate(o, itFn);
343                 });
344
345                 it("should stop iteration if function return false", function() {
346                     itFn.andReturn(false);
347
348                     expect(itFn.calls.length).toEqual(1);
349                 });
350             });
351         });
352
353         describe("do nothing on an empty object", function() {
354             var o;
355
356             beforeEach(function() {
357                 o = {};
358                 Ext.iterate(o, itFn);
359             });
360
361             it("should not call the iterate function", function () {
362                 expect(itFn).not.toHaveBeenCalled();
363             });
364
365         });
366
367         describe("iterate array", function() {
368             var arr;
369
370             beforeEach(function() {
371                 arr = [6, 7, 8, 9];
372             });
373
374             describe("if itFn does not return false", function() {
375                 beforeEach(function() {
376                     Ext.iterate(arr, itFn);
377                 });
378
379                 it("should call the iterate function 4 times", function () {
380                     expect(itFn.callCount).toEqual(4);
381                 });
382
383                 it("should call the iterate function with correct arguments", function () {
384                     expect(itFn.calls[0].args).toEqual([6, 0, arr]);
385                     expect(itFn.calls[1].args).toEqual([7, 1, arr]);
386                     expect(itFn.calls[2].args).toEqual([8, 2, arr]);
387                     expect(itFn.calls[3].args).toEqual([9, 3, arr]);
388                 });
389              });
390
391             describe("if itFn return false", function() {
392                 beforeEach(function() {
393                     itFn.andReturn(false);
394                     Ext.iterate(arr, itFn);
395                 });
396
397                 it("should stop iteration if function return false", function() {
398                     itFn.andReturn(false);
399
400                     expect(itFn.calls.length).toEqual(1);
401                 });
402             });
403         });
404
405         describe("do nothing on an empty array", function() {
406             var arr;
407
408             beforeEach(function() {
409                 arr = [];
410                 Ext.iterate(arr, itFn);
411             });
412
413             it("should not call the iterate function", function () {
414                 expect(itFn).not.toHaveBeenCalled();
415             });
416
417         });
418     });
419
420
421     describe("Ext.num", function() {
422         it("should work with an integer", function() {
423             expect(Ext.num(3)).toEqual(3);
424         });
425
426         it("should work with a negative integer", function() {
427             expect(Ext.num(-7)).toEqual(-7);
428         });
429
430         it("should work with a float", function() {
431             expect(Ext.num(5.43)).toEqual(5.43);
432         });
433
434         it("should work with a negative float", function() {
435             expect(Ext.num(-9.8)).toEqual(-9.8);
436         });
437
438         it("should work with Math.PI", function() {
439             expect(Ext.num(Math.PI)).toEqual(Math.PI);
440         });
441
442         it("should return undefined with null", function() {
443             expect(Ext.num(null)).toBeUndefined();
444         });
445
446         it("should work with null, with defaults", function() {
447             expect(Ext.num(null, 4)).toEqual(4);
448         });
449
450         it("should return undefined with undefined", function() {
451             expect(Ext.num(undefined)).toBeUndefined();
452         });
453
454         it("should work with undefined, with defaults", function() {
455             expect(Ext.num(undefined, 42)).toEqual(42);
456         });
457
458         it("should return undefined with boolean", function() {
459             expect(Ext.num(true)).toBeUndefined();
460         });
461
462         it("should work with boolean, with defaults", function() {
463             expect(Ext.num(true, 12)).toEqual(12);
464         });
465
466         it("should return undefined with empty string", function() {
467             expect(Ext.num("")).toBeUndefined();
468         });
469
470         it("should work with string argument in the form of a number", function() {
471             expect(Ext.num('666')).toEqual(666);
472         });
473
474         it("should return undefined with a string containing only spaces", function() {
475             expect(Ext.num("     ")).toBeUndefined();
476         });
477
478         it("should return undefined with non empty string", function() {
479             expect(Ext.num("foo")).toBeUndefined();
480         });
481
482         it("should return undefined with empty array", function() {
483             expect(Ext.num([])).toBeUndefined();
484         });
485
486         it("should return undefined with non empty array", function() {
487             expect(Ext.num([1, 2, 3])).toBeUndefined();
488         });
489
490         it("should return undefined with array with a single item", function() {
491             expect(Ext.num([3])).toBeUndefined();
492         });
493     });
494
495     describe("Ext.pluck", function() {
496         it("should return results", function() {
497             var results = Ext.pluck([{
498                 n: 11,
499                 c: 17
500             }, {
501                 n: 13,
502                 p: true
503             }, {
504                 n: 18,
505                 p: false
506             }], 'n');
507
508             expect(results).toEqual([11, 13, 18]);
509         });
510     });
511
512     describe("Ext.toArray", function() {
513         var span1,
514             span2,
515             span3,
516             span4,
517             div,
518             htmlCollection;
519
520         beforeEach(function() {
521             div = Ext.getBody().createChild({tag: "div"});
522             span1 = div.createChild({tag: "span"});
523             span2 = div.createChild({tag: "span"});
524             span3 = div.createChild({tag: "span"});
525             span4 = div.createChild({tag: "span"});
526             htmlCollection = div.dom.getElementsByTagName("span");
527         });
528
529         it("should convert iterable to an array", function() {
530            expect(Ext.toArray(htmlCollection)).toEqual([span1.dom, span2.dom, span3.dom, span4.dom]);
531         });
532
533         it("should convert a part of an iterable to an array", function() {
534            expect(Ext.toArray(htmlCollection, 1, 3)).toEqual([span2.dom, span3.dom]);
535         });
536     });
537
538     xdescribe("Ext.urlAppend", function() {
539         var url = "http://example.com/";
540
541         it("should manage question mark", function() {
542             expect(Ext.urlAppend(url, "test=1")).toEqual("http://example.com/?test=1");
543         });
544
545         it("should manage ampersand", function() {
546             expect(Ext.urlAppend(url+"?test=1","foo=2")).toEqual("http://example.com/?test=1&foo=2");
547         });
548
549         it("should return directly url if content is empty", function() {
550             expect(Ext.urlAppend(url)).toEqual(url);
551         });
552     });
553
554     xdescribe("Ext.urlDecode", function() {
555         it ("should return an empty object if string is empty", function (){
556             expect(Ext.urlDecode("")).toEqual({});
557         });
558
559         it("should decode 2 keys", function(){
560             expect(Ext.urlDecode("foo=1&bar=2")).toEqual({
561                 foo: "1",
562                 bar: "2"
563             });
564         });
565
566         it("should decode 2 keys, one of them an array (overwrite off)", function() {
567             expect(Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", false)).toEqual({
568                 foo: "1",
569                 bar: ['2', '3', '4']
570             });
571         });
572
573         it("should decode 2 keys, one of them an array (overwrite on)", function() {
574             expect(Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", true)).toEqual({
575                 foo: "1",
576                 bar: "4"
577             });
578         });
579     });
580
581     xdescribe("Ext.urlEncode", function() {
582         it("should encode 2 keys", function() {
583             expect(Ext.urlEncode({
584                 foo: "1",
585                 bar: "2"
586             })).toEqual("foo=1&bar=2");
587         });
588
589         it("should encode 2 keys, one of them an array", function() {
590             expect(Ext.urlEncode({
591                 foo: "1",
592                 bar: ['2', '3', '4']
593             })).toEqual("foo=1&bar=2&bar=3&bar=4");
594         });
595
596         it("should encode 2 keys, one of them an array, with pre: test=1", function() {
597             expect(Ext.urlEncode({
598                 foo: "1",
599                 bar: ['2', '3', '4']
600             }, "test=1")).toEqual("test=1&foo=1&bar=2&bar=3&bar=4");
601         });
602     });
603
604     xdescribe("Ext.htmlEncode", function() {
605         it("should call Ext.String.htmlEncode", function() {
606             var val = '';
607             spyOn(Ext.util.Format, "htmlEncode");
608             Ext.htmlEncode(val);
609             expect(Ext.String.htmlEncode).toHaveBeenCalledWith(val);
610         });
611     });
612
613     xdescribe("Ext.htmlEncode", function() {
614         it("should call Ext.String.htmlDecode", function() {
615             var val = '';
616             spyOn(Ext.util.Format, "htmlDecode");
617
618             Ext.htmlDecode(val);
619             expect(Ext.String.htmlDecode).toHaveBeenCalledWith(val);
620         });
621     });
622
623     describe("Ext.getBody", function() {
624         it("should return current document body as an Ext.core.Element", function() {
625             expect(Ext.getBody()).toEqual(Ext.get(document.body)); // see initSandbox in DomSandBox.js for more info
626         });
627     });
628
629     describe("Ext.getHead", function() {
630         it("should return current document head as an Ext.core.Element", function() {
631             expect(Ext.getHead()).toEqual(Ext.get(document.getElementsByTagName("head")[0]));
632         });
633     });
634
635     describe("Ext.getDoc", function() {
636         it("should return the current HTML document object as an Ext.element", function() {
637             expect(Ext.getDoc()).toEqual(Ext.get(document));
638         });
639     });
640
641     describe("Ext.getOrientation", function() {
642         it("should return the current orientation of the mobile device", function() {
643             if (window.innerHeight > window.innerWidth) {
644                 expect(Ext.getOrientation()).toEqual("portrait");
645             } else {
646                 expect(Ext.getOrientation()).toEqual("landscape");
647             }
648         });
649     });
650
651     describe("Ext.getDom", function() {
652         var el1;
653
654         beforeEach(function() {
655             el1 = Ext.getBody().createChild({id: "elone"});
656         });
657
658         it("should return a dom element if an Ext.element is passed as first argument", function() {
659             expect(Ext.getDom(el1)).toEqual(el1.dom);
660         });
661
662         it("should return a dom element if the string (id) passed as first argument", function() {
663             expect(Ext.getDom("elone")).toEqual(el1.dom);
664         });
665     });
666 });