Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / test / unit / spec / lang / Array.js
1 describe("Ext.Array", function() {
2     var array;
3
4     describe("Ext.Array.indexOf", function() {
5         var indexOf = Ext.Array.indexOf;
6
7         describe("without from argument", function() {
8             beforeEach(function() {
9                 array = [1, 2, 3, 4, 5, 6];
10             });
11
12             afterEach(function(){
13                 array = null;
14             });
15
16             it("should always return -1 on an empty array", function(){
17                 expect(indexOf([], 1)).toEqual(-1);
18             });
19
20             it("should return -1 if them it doesn't exist", function() {
21                 expect(indexOf(array, 7)).toEqual(-1);
22             });
23
24             it("should return the matching index if found", function() {
25                 expect(indexOf(array, 4)).toEqual(3);
26             });
27
28             it("should return the first matching index if found", function(){
29                 array.push(1);
30                 expect(indexOf(array, 1)).toEqual(0);
31             });
32         });
33
34         describe("with from argument", function() {
35             beforeEach(function() {
36                 array = [1, 2, 3, 4, 5, 6, 7];
37             });
38
39             it("should return the matched index if found", function() {
40                 expect(indexOf(array, 5, 3)).toEqual(4);
41             });
42
43             it("should return the matched index if found", function() {
44                 expect(indexOf(array, 5, 4)).toEqual(4);
45             });
46
47             it("should return -1 if the item doesn't exist after the passed from value", function() {
48                 expect(indexOf(array, 5, 5)).toEqual(-1);
49             });
50         });
51
52     });
53     describe("removing items", function() {
54         var remove = Ext.Array.remove,
55             myArray;
56
57         it("should do nothing when removing from an empty array", function() {
58             myArray = [];
59
60             expect(function() {
61                 remove(myArray, 1);
62             }).not.toRaiseExtError();
63
64             expect(myArray).toEqual([]);
65         });
66
67         describe("when removing an item inside an array", function() {
68             beforeEach(function() {
69                 myArray = [1, 2, 3, 4, 5];
70
71                 remove(myArray, 1);
72             });
73
74             it("should remove the item", function() {
75                 expect(myArray).toEqual([2, 3, 4, 5]);
76             });
77
78             it("should update the index of the following items", function() {
79                 expect(myArray[1]).toEqual(3);
80                 expect(myArray[2]).toEqual(4);
81                 expect(myArray[3]).toEqual(5);
82             });
83
84             it("should remove only using a strict type check", function(){
85                 remove(myArray, '2');
86                 expect(myArray).toEqual([2, 3, 4, 5]);
87             });
88         });
89     });
90
91     describe("contains", function() {
92         var contains = Ext.Array.contains;
93
94         it("should always return false with an empty array", function(){
95             expect(contains([], 1)).toBe(false);
96         });
97
98         it("should return false if an item does not exist in the array", function() {
99             expect(contains([1, 2, 3], 10)).toBe(false);
100         });
101
102         it("should return true if an item exists in the array", function() {
103             expect(contains([8, 9, 10], 10)).toBe(true);
104         });
105
106         it("should only match with strict type checking", function(){
107             expect(contains([1, 2, 3, 4, 5], '1')).toBe(false);
108         });
109     });
110
111     describe("include", function(){
112         var include = Ext.Array.include,
113             myArray;
114
115         it("should always add to an empty array", function(){
116             myArray = [];
117             include(myArray, 1);
118             expect(myArray).toEqual([1]);
119         });
120
121         it("should add the item if it doesn't exist", function(){
122             myArray = [1];
123             include(myArray, 2);
124             expect(myArray).toEqual([1, 2]);
125         });
126
127         it("should always add to the end of the array", function(){
128             myArray = [9, 8, 7, 6];
129             include(myArray, 10);
130             expect(myArray).toEqual([9, 8, 7, 6, 10]);
131         });
132
133         it("should match using strict type checking", function(){
134             myArray = ['1'];
135             include(myArray, 1);
136             expect(myArray).toEqual(['1', 1]);
137         });
138
139         it("should not modify the array if the value exists", function(){
140             myArray = [4, 5, 6];
141             include(myArray, 7);
142             expect(myArray).toEqual([4, 5, 6, 7]);
143         });
144     });
145
146     describe("clone", function(){
147         var clone = Ext.Array.clone;
148
149         it("should clone an empty array to be empty", function(){
150             expect(clone([])).toEqual([]);
151         });
152
153         it("should clone an array with items", function(){
154             expect(clone([1, 3, 5])).toEqual([1, 3, 5]);
155         });
156
157         it("should create a new reference", function(){
158             var arr = [1, 2, 3];
159             expect(clone(arr)).not.toBe(arr);
160         });
161
162         it("should do a shallow clone", function(){
163             var o = {},
164                 arr = [o],
165                 result;
166
167             result = clone(arr);
168             expect(result[0]).toBe(o);
169         });
170     });
171
172     describe("clean", function(){
173         var clean = Ext.Array.clean;
174
175         it("should return an empty array if cleaning an empty array", function(){
176             expect(clean([])).toEqual([]);
177         });
178
179         it("should remove undefined values", function(){
180             expect(clean([undefined])).toEqual([]);
181         });
182
183         it("should remove null values", function(){
184             expect(clean([null])).toEqual([]);
185         });
186
187         it("should remove empty strings", function(){
188             expect(clean([''])).toEqual([]);
189         });
190
191         it("should remove empty arrays", function(){
192             expect(clean([[]])).toEqual([]);
193         });
194
195         it("should remove a mixture of empty values", function(){
196             expect(clean([null, undefined, '', []])).toEqual([]);
197         });
198
199         it("should remove all occurrences of empty values", function(){
200             expect(clean([null, null, null, undefined, '', '', '', undefined])).toEqual([]);
201         });
202
203         it("should leave non empty values untouched", function(){
204             expect(clean([1, 2, 3])).toEqual([1, 2, 3]);
205         });
206
207         it("should remove only the empty values", function(){
208             expect(clean([undefined, null, 1, null, 2])).toEqual([1, 2]);
209         });
210
211         it("should preserve order on removal", function(){
212             expect(clean([1, null, 2, null, null, null, 3, undefined, '', '', 4])).toEqual([1, 2, 3, 4]);
213         });
214     });
215
216     describe("unique", function(){
217         var unique = Ext.Array.unique;
218
219         it("should return an empty array if run on an empty array", function(){
220             expect(unique([])).toEqual([]);
221         });
222
223         it("should return a new reference", function(){
224             var arr = [1, 2, 3];
225             expect(unique(arr)).not.toBe(arr);
226         });
227
228         it("should return a copy if all items are unique", function(){
229             expect(unique([6, 7, 8])).toEqual([6, 7, 8]);
230         });
231
232         it("should only use strict typing to match", function(){
233             expect(unique([1, '1'])).toEqual([1, '1']);
234         });
235
236         it("should preserve the order when removing", function(){
237             expect(unique([1, 2, 1, 3, 1, 1, 1, 6, 5, 1])).toEqual([1, 2, 3, 6, 5]);
238         });
239     });
240
241     describe("map", function(){
242         var map = Ext.Array.map,
243             emptyFn = function(v){
244                 return v;
245             };
246
247         it("should return an empty array if run on an empty array", function(){
248             expect(map([], function(){})).toEqual([]);
249         });
250
251         it("should return a new reference", function(){
252             var arr = [1, 2];
253             expect(map(arr, emptyFn)).not.toBe(arr);
254         });
255
256         it("should execute the function for each item in the array", function(){
257             expect(map([1, 2, 3, 4, 5], function(v){
258                 return v * 2;
259             })).toEqual([2, 4, 6, 8, 10]);
260         });
261
262         it("should get called with the correct scope", function(){
263             var scope = {},
264                 realScope;
265             map([1, 2, 3, 4, 5], function(){
266                 realScope = this;
267             }, scope);
268             expect(realScope).toBe(scope);
269         });
270
271         it("should get called with the argument, index and array", function(){
272             var item,
273                 index,
274                 arr,
275                 data = [1];
276
277             map(data, function(){
278                 item = arguments[0];
279                 index = arguments[1];
280                 arr = arguments[2];
281             });
282             expect(item).toEqual(1);
283             expect(index).toEqual(0);
284             expect(arr).toBe(data);
285         });
286     });
287
288     describe("from", function(){
289         var from = Ext.Array.from;
290
291         it("should return an empty array for an undefined value", function(){
292             expect(from(undefined)).toEqual([]);
293         });
294
295         it("should return an empty array for a null value", function(){
296             expect(from(null)).toEqual([]);
297         });
298
299         it("should convert an array", function(){
300             expect(from([1, 2, 3])).toEqual([1, 2, 3]);
301         });
302
303         it("should preserve the order", function(){
304             expect(from(['a', 'string', 'here'])).toEqual(['a', 'string', 'here']);
305         });
306
307         it("should convert a single value to an array", function(){
308             expect(from(true)).toEqual([true]);
309             expect(from(700)).toEqual([700]);
310         });
311
312         it("should convert arguments to an array", function(){
313             var test, fn = function(){
314                 test = from(arguments);
315             };
316             fn(1, 2, 3);
317             expect(test instanceof Array).toBeTruthy();
318             expect(test).toEqual([1, 2, 3]);
319         });
320
321         it("should convert a DOM collection to an array", function(){
322             var ct = document.body.appendChild(document.createElement('div')),
323                 node1 = ct.appendChild(document.createElement('div')),
324                 node2 = ct.appendChild(document.createElement('div')),
325                 node3 = ct.appendChild(document.createElement('div')),
326                 collection = ct.getElementsByTagName('div'),
327                 result = from(collection);
328
329             expect(result instanceof Array).toBeTruthy();
330             expect(result).toEqual([node1, node2, node3]);
331             document.body.removeChild(ct);
332
333         });
334     });
335
336     describe("toArray", function(){
337         var toArray = Ext.Array.toArray;
338
339         it("should convert an array", function(){
340             expect(toArray([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
341         });
342
343         it("should convert a string", function(){
344             expect(toArray('12345')).toEqual(['1', '2', '3', '4', '5']);
345         });
346
347         it("should create a new reference", function(){
348             var arr = [6, 7, 8];
349             expect(toArray(arr)).not.toBe(arr);
350         });
351
352         it("should convert arguments", function(){
353             var test, fn = function(){
354                 test = toArray(arguments);
355             };
356             fn(-1, -2, -3);
357             expect(test instanceof Array).toBeTruthy();
358             expect(test).toEqual([-1, -2, -3]);
359         });
360
361         it("should convert a DOM collection", function(){
362             var ct = document.body.appendChild(document.createElement('div')),
363                 node1 = ct.appendChild(document.createElement('div')),
364                 node2 = ct.appendChild(document.createElement('div')),
365                 node3 = ct.appendChild(document.createElement('div')),
366                 collection = ct.getElementsByTagName('div'),
367                 result = toArray(collection);
368
369             expect(result instanceof Array).toBeTruthy();
370             expect(result).toEqual([node1, node2, node3]);
371             document.body.removeChild(ct);
372         });
373
374         describe("start/end parameters", function(){
375             it("should default to whole of the array", function(){
376                 expect(toArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
377             });
378
379             it("should work with only the start parameter specified", function(){
380                 expect(toArray([1, 2, 3, 4, 5, 6], 2)).toEqual([3, 4, 5, 6]);
381             });
382
383             it("should work with only the end parameter specified", function(){
384                 expect(toArray([1, 2, 3, 4, 5, 6], null, 4)).toEqual([1, 2, 3, 4]);
385             });
386
387             it("should work with both params specified", function(){
388                 expect(toArray([1, 2, 3, 4, 5, 6], 2, 4)).toEqual([3, 4]);
389             });
390
391             it("should work with nagative end", function(){
392                 expect(toArray([1, 2, 3, 4, 5, 6], 2, -1)).toEqual([3, 4, 5]);
393             });
394         });
395     });
396
397     describe("pluck", function(){
398         var pluck = Ext.Array.pluck;
399         it("should return an empty array when an empty array is passed", function(){
400             expect(pluck([], 'prop')).toEqual([]);
401         });
402
403         it("should pull the properties from objects in the array", function(){
404             var arr = [{prop: 1}, {prop: 2}, {prop: 3}];
405             expect(pluck(arr, 'prop')).toEqual([1, 2, 3]);
406         });
407
408         it("should return a new reference", function(){
409             var arr = [{prop: 1}, {prop: 2}, {prop: 3}];
410             expect(pluck(arr, 'prop')).not.toBe(arr);
411         });
412
413         it("should work on a DOM collection", function(){
414             var ct = document.body.appendChild(document.createElement('div')),
415                 i = 0,
416                 node;
417
418             for(; i < 5; ++i) {
419                 node = ct.appendChild(document.createElement('div'));
420                 node.className = 'node' + i;
421             }
422
423             expect(pluck(ct.getElementsByTagName('div'), 'className')).toEqual(['node0', 'node1', 'node2', 'node3', 'node4']);
424             document.body.removeChild(ct);
425         });
426     });
427
428     describe("filter", function(){
429         var filter = Ext.Array.filter,
430             trueFn = function(){
431                 return true;
432             };
433
434         it("should return an empty array if filtering an empty array", function(){
435             expect(filter([], trueFn)).toEqual([]);
436         });
437
438         it("should create a new reference", function(){
439             var arr = [1, 2, 3];
440             expect(filter(arr, trueFn)).not.toBe(arr);
441         });
442
443         it("should add items if the filter function returns true", function(){
444             expect(filter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(val){
445                 return val % 2 == 0;
446             })).toEqual([2, 4, 6, 8, 10]);
447         });
448
449         it("should add items if the filter function returns a truthy value", function(){
450             expect(filter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(val){
451                 if (val % 2 == 0) {
452                     return 1;
453                 }
454             })).toEqual([2, 4, 6, 8, 10]);
455         });
456
457         it("should not add items if the filter function returns a falsy value", function(){
458             expect(filter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(val){
459                 return 0;
460             })).toEqual([]);
461         });
462
463         it("should pass the correct parameters", function(){
464             var values = [],
465                 indexes = [],
466                 arrs = [],
467                 data = [1, 2, 3];
468
469             filter([1, 2, 3], function(val, index, arr){
470                 values.push(val);
471                 indexes.push(index);
472                 arrs.push(arr);
473             });
474
475             expect(values).toEqual([1, 2, 3]);
476             expect(indexes).toEqual([0, 1, 2]);
477             expect(arrs).toEqual([data, data, data]);
478         });
479
480         it("should do a shallow copy", function(){
481             var o1 = {prop: 1},
482                 o2 = {prop: 2},
483                 o3 = {prop: 3};
484
485             expect(filter([o1, o2, o3], trueFn)).toEqual([o1, o2, o3]);
486         });
487
488         it("should execute in scope when passed", function(){
489             var scope = {},
490                 actual;
491
492             expect(filter([1, 2, 3], function(){
493                 actual = this;
494             }, scope));
495             expect(actual).toBe(scope);
496         });
497     });
498
499     describe("forEach", function(){
500         var forEach = Ext.Array.forEach;
501
502         it("should not execute on an empty array", function(){
503             var count = 0;
504             forEach([], function(){
505                 ++count;
506             });
507             expect(count).toEqual(0);
508         });
509
510         it("should execute for each item in the array", function(){
511             var count = 0;
512             forEach([1, 2, 3, 4, 5], function(){
513                 ++count;
514             });
515             expect(count).toEqual(5);
516         });
517
518         it("should execute in the appropriate scope", function(){
519             var scope = {},
520                 actual;
521
522             forEach([1, 2, 3], function(){
523                 actual = this;
524             }, scope);
525
526             expect(actual).toBe(scope);
527         });
528
529         it("should pass the appropriate params to the callback", function(){
530             var values = [],
531                 indexes = [],
532                 arrs = [],
533                 data = [1, 2, 3];
534
535             forEach(data, function(val, index, arr){
536                 values.push(val);
537                 indexes.push(index);
538                 arrs.push(arr);
539             });
540
541             expect(values).toEqual([1, 2, 3]);
542             expect(indexes).toEqual([0, 1, 2]);
543             expect(arrs).toEqual([data, data, data]);
544         });
545     });
546
547     describe("each", function(){
548         var each = Ext.Array.each;
549
550         describe("return values", function(){
551             xit("should return 0 if the passed value is empty", function(){
552                 expect(each([])).toEqual(0);
553             });
554
555             it("should return the stopping index if iteration is halted", function(){
556                 expect(each([1, 2, 3], function(val){
557                     return val != 2;
558                 })).toEqual(1);
559             });
560
561             it("should return true if iteration is not stopped", function(){
562                 expect(each([4, 5, 6], function(){
563                     return true;
564                 })).toBeTruthy();
565             });
566         });
567
568         describe("scope/parameters", function(){
569             it("should execute in the specified scope", function(){
570                 var scope = {},
571                     actual;
572
573                 each([1, 2, 3], function(){
574                     actual = this;
575                 }, scope);
576                 expect(actual).toBe(scope);
577             });
578
579             it("should pass the item, index and array", function(){
580                 var values = [],
581                     indexes = [],
582                     arrs = [],
583                     data = [1, 2, 3];
584
585                 each(data, function(val, index, arr){
586                     values.push(val);
587                     indexes.push(index);
588                     arrs.push(arr);
589                 });
590
591                 expect(values).toEqual([1, 2, 3]);
592                 expect(indexes).toEqual([0, 1, 2]);
593                 expect(arrs).toEqual([data, data, data]);
594             });
595         });
596
597         describe("stopping iteration", function(){
598             it("should not stop iteration by default", function(){
599                 var count = 0;
600                 each([1, 2, 3, 4, 5], function(){
601                     ++count;
602                 });
603                 expect(count).toEqual(5);
604             });
605
606             it("should not stop unless an explicit false is returned", function(){
607                 var count = 0;
608                 each([1, 2, 3, 4, 5], function(){
609                     ++count;
610                     return null;
611                 });
612                 expect(count).toEqual(5);
613             });
614
615             it("should stop immediately if false is returned", function(){
616                 var count = 0;
617                 each([1, 2, 3, 4, 5], function(v){
618                     ++count;
619                     return v != 2;
620                 });
621                 expect(count).toEqual(2);
622             });
623         });
624
625         describe("other collection types", function(){
626             it("should iterate arguments", function(){
627                 var test, values = [], fn = function(){
628                     test = each(arguments, function(val){
629                         values.push(val)
630                     });
631                 };
632                 fn(1, 2, 3);
633                 expect(values).toEqual([1, 2, 3]);
634             });
635
636             it("should iterate over a DOM collection", function(){
637                 var ct = document.body.appendChild(document.createElement('div')),
638                     node1 = ct.appendChild(document.createElement('div')),
639                     node2 = ct.appendChild(document.createElement('div')),
640                     node3 = ct.appendChild(document.createElement('div')),
641                     collection = ct.getElementsByTagName('div'),
642                     result = [];
643
644                 each(collection, function(node){
645                     result.push(node.tagName.toLowerCase());
646                 });
647
648                 expect(result).toEqual(['div', 'div', 'div']);
649                 document.body.removeChild(ct);
650             });
651         });
652
653         it("should iterate once over a single, non empty value", function(){
654             var count = 0;
655             each('string', function(){
656                 ++count;
657             });
658             expect(count).toEqual(1);
659         });
660     });
661
662     describe("every", function(){
663         var every = Ext.Array.every;
664
665         describe("scope/params", function(){
666             it("should execute in the specified scope", function(){
667                 var scope = {},
668                     actual;
669
670                 every([1, 2, 3], function(){
671                     actual = this;
672                 }, scope);
673                 expect(actual).toBe(scope);
674             });
675
676             it("should pass the item, index and array", function(){
677                 var values = [],
678                     indexes = [],
679                     arrs = [],
680                     data = [1, 2, 3];
681
682                 every(data, function(val, index, arr){
683                     values.push(val);
684                     indexes.push(index);
685                     arrs.push(arr);
686                     return true;
687                 });
688
689                 expect(values).toEqual([1, 2, 3]);
690                 expect(indexes).toEqual([0, 1, 2]);
691                 expect(arrs).toEqual([data, data, data]);
692             });
693         });
694
695         it("should return true on an empty array", function(){
696             expect(every([], function(){})).toBeTruthy();
697         });
698
699         it("should throw an exception if no fn is passed", function(){
700             expect(function(){
701                 every([1, 2, 3]);
702             }).toRaiseExtError();
703         });
704
705         it("should stop as soon as a false value is found", function(){
706             var count = 0,
707                 result;
708
709             result = every([true, true, false, true], function(v){
710                 ++count;
711                 return v;
712             });
713             expect(count).toEqual(3);
714             expect(result).toBeFalsy();
715         });
716
717         it("should return true if all values match the function", function(){
718             expect(every([1, 2, 3, 4, 5, 6, 7, 8, 9], function(v){
719                 return v < 10;
720             })).toBeTruthy();
721         });
722     });
723
724     describe("some", function(){
725         var some = Ext.Array.some;
726
727         describe("scope/params", function(){
728             it("should execute in the specified scope", function(){
729                 var scope = {},
730                     actual;
731
732                 some([1, 2, 3], function(){
733                     actual = this;
734                 }, scope);
735                 expect(actual).toBe(scope);
736             });
737
738             it("should pass the item, index and array", function(){
739                 var values = [],
740                     indexes = [],
741                     arrs = [],
742                     data = [1, 2, 3];
743
744                 some(data, function(val, index, arr){
745                     values.push(val);
746                     indexes.push(index);
747                     arrs.push(arr);
748                     return true;
749                 });
750
751                 expect(values).toEqual([1]);
752                 expect(indexes).toEqual([0]);
753                 expect(arrs).toEqual([data]);
754             });
755         });
756
757         it("should return false on an empty array", function(){
758             expect(some([], function(){})).toBeFalsy();
759         });
760
761         it("should throw an exception if no fn is passed", function(){
762             expect(function(){
763                 some([1, 2, 3]);
764             }).toRaiseExtError();
765         });
766
767         it("should stop as soon as a matching value is found", function(){
768             var count = 0,
769                 result;
770
771             result = some([1, 2, 3, 4], function(val){
772                 ++count;
773                 return val == 3;
774             });
775             expect(count).toEqual(3);
776             expect(result).toBeTruthy();
777         });
778
779         it("should return false if nothing matches the matcher function", function(){
780             var count = 0,
781                 result;
782
783             result = some([1, 2, 3, 4, 5, 6, 7, 8, 9], function(val){
784                 ++count;
785                 return val > 9;
786             });
787             expect(count).toEqual(9);
788             expect(result).toBeFalsy();
789         });
790     });
791
792     describe("merge", function(){
793         var merge = Ext.Array.merge;
794
795         it("should return an empty array if run on an empty array", function(){
796             expect(merge([])).toEqual([]);
797         });
798
799         it("should return a new reference", function(){
800             var arr = [1, 2, 3];
801             expect(merge(arr)).not.toBe(arr);
802         });
803
804         it("should return a copy if all items are unique", function(){
805             expect(merge([6, 7, 8])).toEqual([6, 7, 8]);
806         });
807
808         it("should only use strict typing to match", function(){
809             expect(merge([1, '1'])).toEqual([1, '1']);
810         });
811
812         it("should accept two or more arrays and return a unique union with items in order of first appearance", function(){
813             expect(merge([1, 2, 3], ['1', '2', '3'], [4, 1, 5, 2], [6, 3, 7, '1'], [8, '2', 9, '3'])).toEqual([1, 2, 3, '1', '2', '3', 4, 5, 6, 7, 8, 9]);
814         });
815     });
816
817     describe("intersect", function(){
818         var intersect = Ext.Array.intersect;
819
820         it("should return an empty array if no arrays are passed", function(){
821             expect(intersect()).toEqual([]);
822         });
823
824         it("should return an empty array if one empty array is passed", function(){
825             expect(intersect([])).toEqual([]);
826         });
827
828         it("should return a new reference", function(){
829             var arr = [1, 2, 3];
830             expect(intersect(arr)).not.toBe(arr);
831         });
832
833         it("should return a copy if one array is passed", function(){
834             expect(intersect([6, 7, 8])).toEqual([6, 7, 8]);
835         });
836
837         it("should return an intersection of two or more arrays with items in order of first appearance", function(){
838             expect(intersect([1, 2, 3], [4, 3, 2, 5], [2, 6, 3])).toEqual([2, 3]);
839         });
840
841         it("should return an empty array if there is no intersecting values", function(){
842             expect(intersect([1, 2, 3], [4, 5, 6])).toEqual([]);
843         });
844
845         it("should contain the unique set of intersected values only", function(){
846             expect(intersect([1, 1, 2, 3, 3], [1, 1, 2, 3, 3])).toEqual([1, 2, 3]);
847         });
848
849         it("should only use strict typing to match", function(){
850             expect(intersect([1], ['1'])).toEqual([]);
851         });
852     });
853
854     describe("difference", function(){
855         var difference = Ext.Array.difference;
856
857         it("should return a set difference of two arrays with items in order of first appearance", function(){
858             expect(difference([1, 2, 3, 4], [3, 2])).toEqual([1, 4]);
859         });
860
861         it("should return the first array unchanged if there is no difference", function(){
862             expect(difference([1, 2, 3], [4, 5, 6])).toEqual([1, 2, 3]);
863         });
864
865         it("should return a new reference", function(){
866             var arr = [1, 2, 3];
867             expect(difference(arr, [3, 2])).not.toBe(arr);
868         });
869
870         it("should remove multiples of the same value from the first array", function(){
871             expect(difference([1, 2, 3, 2, 4, 1], [2, 1])).toEqual([3, 4]);
872         });
873
874         it("should only use strict typing to match", function(){
875             expect(difference([1], ['1'])).toEqual([1]);
876         });
877     });
878
879     describe("sort", function() {
880        var sarray, narray;
881        beforeEach(function() {
882           sarray = ['bbb', 'addda', 'erere', 'fff', 'de3'];
883           narray = [1,3,2,4,6,7];
884
885        });
886
887        describe("with strings", function() {
888            it("should be able to sort an array without sortFn", function() {
889                 Ext.Array.sort(sarray);
890                 expect(sarray).toEqual(['addda', 'bbb', 'de3', 'erere', 'fff']);
891            });
892
893
894            it("should be able to use a sortFn that returns a Number", function() {
895                 Ext.Array.sort(sarray, function(a,b){ 
896                     if (a === b) {
897                         return 0;
898                     } 
899                     return  a > b ? 1: -1;
900                 });
901                 expect(sarray).toEqual(['addda', 'bbb', 'de3', 'erere', 'fff']);
902            });
903        });
904
905        describe("with numbers", function() {
906            it("should be able to sort an array without sortFn", function() {
907                 Ext.Array.sort(narray);
908                 expect(narray).toEqual([1,2,3,4,6,7]);
909            });
910
911
912            it("should be able to use a sortFn that returns a Number", function() {
913                 Ext.Array.sort(narray, function(a,b){
914                     return a - b;
915                 });
916                 expect(narray).toEqual([1,2,3,4,6,7]);
917            });
918        });
919     });
920
921     describe("min", function() {
922         describe("numbers", function() {
923             it("without comparisonFn", function() {
924                 expect(Ext.Array.min([1,2,3,4,5,6])).toEqual(1);
925             });
926
927             it("with comparisonFn", function() {
928                 expect(Ext.Array.min([1,2,3,4,5,6], function(a, b) { return a < b ? 1 : -1 })).toEqual(6);
929             });
930         });
931     });
932
933     describe("max", function() {
934         describe("numbers", function() {
935             it("without comparisonFn", function() {
936                 expect(Ext.Array.max([1,2,3,4,5,6])).toEqual(6);
937             });
938
939             it("with comparisonFn", function() {
940                 expect(Ext.Array.max([1,2,3,4,5,6], function(a, b) { return a < b ? 1 : -1 })).toEqual(1);
941             });
942         });
943     });
944
945     describe("sum", function() {
946         it("should return 21", function() {
947             expect(Ext.Array.sum([1,2,3,4,5,6])).toEqual(21);
948         });
949     });
950
951     describe("mean", function() {
952         it("should return 3.5", function() {
953             expect(Ext.Array.mean([1,2,3,4,5,6])).toEqual(3.5);
954         });
955     });
956 });