1 describe("Ext.Array", function() {
4 describe("Ext.Array.indexOf", function() {
5 var indexOf = Ext.Array.indexOf;
7 describe("without from argument", function() {
8 beforeEach(function() {
9 array = [1, 2, 3, 4, 5, 6];
16 it("should always return -1 on an empty array", function(){
17 expect(indexOf([], 1)).toEqual(-1);
20 it("should return -1 if them it doesn't exist", function() {
21 expect(indexOf(array, 7)).toEqual(-1);
24 it("should return the matching index if found", function() {
25 expect(indexOf(array, 4)).toEqual(3);
28 it("should return the first matching index if found", function(){
30 expect(indexOf(array, 1)).toEqual(0);
34 describe("with from argument", function() {
35 beforeEach(function() {
36 array = [1, 2, 3, 4, 5, 6, 7];
39 it("should return the matched index if found", function() {
40 expect(indexOf(array, 5, 3)).toEqual(4);
43 it("should return the matched index if found", function() {
44 expect(indexOf(array, 5, 4)).toEqual(4);
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);
53 describe("removing items", function() {
54 var remove = Ext.Array.remove,
57 it("should do nothing when removing from an empty array", function() {
62 }).not.toRaiseExtError();
64 expect(myArray).toEqual([]);
67 describe("when removing an item inside an array", function() {
68 beforeEach(function() {
69 myArray = [1, 2, 3, 4, 5];
74 it("should remove the item", function() {
75 expect(myArray).toEqual([2, 3, 4, 5]);
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);
84 it("should remove only using a strict type check", function(){
86 expect(myArray).toEqual([2, 3, 4, 5]);
91 describe("contains", function() {
92 var contains = Ext.Array.contains;
94 it("should always return false with an empty array", function(){
95 expect(contains([], 1)).toBe(false);
98 it("should return false if an item does not exist in the array", function() {
99 expect(contains([1, 2, 3], 10)).toBe(false);
102 it("should return true if an item exists in the array", function() {
103 expect(contains([8, 9, 10], 10)).toBe(true);
106 it("should only match with strict type checking", function(){
107 expect(contains([1, 2, 3, 4, 5], '1')).toBe(false);
111 describe("include", function(){
112 var include = Ext.Array.include,
115 it("should always add to an empty array", function(){
118 expect(myArray).toEqual([1]);
121 it("should add the item if it doesn't exist", function(){
124 expect(myArray).toEqual([1, 2]);
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]);
133 it("should match using strict type checking", function(){
136 expect(myArray).toEqual(['1', 1]);
139 it("should not modify the array if the value exists", function(){
142 expect(myArray).toEqual([4, 5, 6, 7]);
146 describe("clone", function(){
147 var clone = Ext.Array.clone;
149 it("should clone an empty array to be empty", function(){
150 expect(clone([])).toEqual([]);
153 it("should clone an array with items", function(){
154 expect(clone([1, 3, 5])).toEqual([1, 3, 5]);
157 it("should create a new reference", function(){
159 expect(clone(arr)).not.toBe(arr);
162 it("should do a shallow clone", function(){
168 expect(result[0]).toBe(o);
172 describe("clean", function(){
173 var clean = Ext.Array.clean;
175 it("should return an empty array if cleaning an empty array", function(){
176 expect(clean([])).toEqual([]);
179 it("should remove undefined values", function(){
180 expect(clean([undefined])).toEqual([]);
183 it("should remove null values", function(){
184 expect(clean([null])).toEqual([]);
187 it("should remove empty strings", function(){
188 expect(clean([''])).toEqual([]);
191 it("should remove empty arrays", function(){
192 expect(clean([[]])).toEqual([]);
195 it("should remove a mixture of empty values", function(){
196 expect(clean([null, undefined, '', []])).toEqual([]);
199 it("should remove all occurrences of empty values", function(){
200 expect(clean([null, null, null, undefined, '', '', '', undefined])).toEqual([]);
203 it("should leave non empty values untouched", function(){
204 expect(clean([1, 2, 3])).toEqual([1, 2, 3]);
207 it("should remove only the empty values", function(){
208 expect(clean([undefined, null, 1, null, 2])).toEqual([1, 2]);
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]);
216 describe("unique", function(){
217 var unique = Ext.Array.unique;
219 it("should return an empty array if run on an empty array", function(){
220 expect(unique([])).toEqual([]);
223 it("should return a new reference", function(){
225 expect(unique(arr)).not.toBe(arr);
228 it("should return a copy if all items are unique", function(){
229 expect(unique([6, 7, 8])).toEqual([6, 7, 8]);
232 it("should only use strict typing to match", function(){
233 expect(unique([1, '1'])).toEqual([1, '1']);
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]);
241 describe("map", function(){
242 var map = Ext.Array.map,
243 emptyFn = function(v){
247 it("should return an empty array if run on an empty array", function(){
248 expect(map([], function(){})).toEqual([]);
251 it("should return a new reference", function(){
253 expect(map(arr, emptyFn)).not.toBe(arr);
256 it("should execute the function for each item in the array", function(){
257 expect(map([1, 2, 3, 4, 5], function(v){
259 })).toEqual([2, 4, 6, 8, 10]);
262 it("should get called with the correct scope", function(){
265 map([1, 2, 3, 4, 5], function(){
268 expect(realScope).toBe(scope);
271 it("should get called with the argument, index and array", function(){
277 map(data, function(){
279 index = arguments[1];
282 expect(item).toEqual(1);
283 expect(index).toEqual(0);
284 expect(arr).toBe(data);
288 describe("from", function(){
289 var from = Ext.Array.from;
291 it("should return an empty array for an undefined value", function(){
292 expect(from(undefined)).toEqual([]);
295 it("should return an empty array for a null value", function(){
296 expect(from(null)).toEqual([]);
299 it("should convert an array", function(){
300 expect(from([1, 2, 3])).toEqual([1, 2, 3]);
303 it("should preserve the order", function(){
304 expect(from(['a', 'string', 'here'])).toEqual(['a', 'string', 'here']);
307 it("should convert a single value to an array", function(){
308 expect(from(true)).toEqual([true]);
309 expect(from(700)).toEqual([700]);
312 it("should convert arguments to an array", function(){
313 var test, fn = function(){
314 test = from(arguments);
317 expect(test instanceof Array).toBeTruthy();
318 expect(test).toEqual([1, 2, 3]);
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);
329 expect(result instanceof Array).toBeTruthy();
330 expect(result).toEqual([node1, node2, node3]);
331 document.body.removeChild(ct);
336 describe("toArray", function(){
337 var toArray = Ext.Array.toArray;
339 it("should convert an array", function(){
340 expect(toArray([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
343 it("should convert a string", function(){
344 expect(toArray('12345')).toEqual(['1', '2', '3', '4', '5']);
347 it("should create a new reference", function(){
349 expect(toArray(arr)).not.toBe(arr);
352 it("should convert arguments", function(){
353 var test, fn = function(){
354 test = toArray(arguments);
357 expect(test instanceof Array).toBeTruthy();
358 expect(test).toEqual([-1, -2, -3]);
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);
369 expect(result instanceof Array).toBeTruthy();
370 expect(result).toEqual([node1, node2, node3]);
371 document.body.removeChild(ct);
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]);
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]);
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]);
387 it("should work with both params specified", function(){
388 expect(toArray([1, 2, 3, 4, 5, 6], 2, 4)).toEqual([3, 4]);
391 it("should work with nagative end", function(){
392 expect(toArray([1, 2, 3, 4, 5, 6], 2, -1)).toEqual([3, 4, 5]);
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([]);
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]);
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);
413 it("should work on a DOM collection", function(){
414 var ct = document.body.appendChild(document.createElement('div')),
419 node = ct.appendChild(document.createElement('div'));
420 node.className = 'node' + i;
423 expect(pluck(ct.getElementsByTagName('div'), 'className')).toEqual(['node0', 'node1', 'node2', 'node3', 'node4']);
424 document.body.removeChild(ct);
428 describe("filter", function(){
429 var filter = Ext.Array.filter,
434 it("should return an empty array if filtering an empty array", function(){
435 expect(filter([], trueFn)).toEqual([]);
438 it("should create a new reference", function(){
440 expect(filter(arr, trueFn)).not.toBe(arr);
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){
446 })).toEqual([2, 4, 6, 8, 10]);
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){
454 })).toEqual([2, 4, 6, 8, 10]);
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){
463 it("should pass the correct parameters", function(){
469 filter([1, 2, 3], function(val, index, arr){
475 expect(values).toEqual([1, 2, 3]);
476 expect(indexes).toEqual([0, 1, 2]);
477 expect(arrs).toEqual([data, data, data]);
480 it("should do a shallow copy", function(){
485 expect(filter([o1, o2, o3], trueFn)).toEqual([o1, o2, o3]);
488 it("should execute in scope when passed", function(){
492 expect(filter([1, 2, 3], function(){
495 expect(actual).toBe(scope);
499 describe("forEach", function(){
500 var forEach = Ext.Array.forEach;
502 it("should not execute on an empty array", function(){
504 forEach([], function(){
507 expect(count).toEqual(0);
510 it("should execute for each item in the array", function(){
512 forEach([1, 2, 3, 4, 5], function(){
515 expect(count).toEqual(5);
518 it("should execute in the appropriate scope", function(){
522 forEach([1, 2, 3], function(){
526 expect(actual).toBe(scope);
529 it("should pass the appropriate params to the callback", function(){
535 forEach(data, function(val, index, arr){
541 expect(values).toEqual([1, 2, 3]);
542 expect(indexes).toEqual([0, 1, 2]);
543 expect(arrs).toEqual([data, data, data]);
547 describe("each", function(){
548 var each = Ext.Array.each;
550 describe("return values", function(){
551 xit("should return 0 if the passed value is empty", function(){
552 expect(each([])).toEqual(0);
555 it("should return the stopping index if iteration is halted", function(){
556 expect(each([1, 2, 3], function(val){
561 it("should return true if iteration is not stopped", function(){
562 expect(each([4, 5, 6], function(){
568 describe("scope/parameters", function(){
569 it("should execute in the specified scope", function(){
573 each([1, 2, 3], function(){
576 expect(actual).toBe(scope);
579 it("should pass the item, index and array", function(){
585 each(data, function(val, index, arr){
591 expect(values).toEqual([1, 2, 3]);
592 expect(indexes).toEqual([0, 1, 2]);
593 expect(arrs).toEqual([data, data, data]);
597 describe("stopping iteration", function(){
598 it("should not stop iteration by default", function(){
600 each([1, 2, 3, 4, 5], function(){
603 expect(count).toEqual(5);
606 it("should not stop unless an explicit false is returned", function(){
608 each([1, 2, 3, 4, 5], function(){
612 expect(count).toEqual(5);
615 it("should stop immediately if false is returned", function(){
617 each([1, 2, 3, 4, 5], function(v){
621 expect(count).toEqual(2);
625 describe("other collection types", function(){
626 it("should iterate arguments", function(){
627 var test, values = [], fn = function(){
628 test = each(arguments, function(val){
633 expect(values).toEqual([1, 2, 3]);
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'),
644 each(collection, function(node){
645 result.push(node.tagName.toLowerCase());
648 expect(result).toEqual(['div', 'div', 'div']);
649 document.body.removeChild(ct);
653 it("should iterate once over a single, non empty value", function(){
655 each('string', function(){
658 expect(count).toEqual(1);
662 describe("every", function(){
663 var every = Ext.Array.every;
665 describe("scope/params", function(){
666 it("should execute in the specified scope", function(){
670 every([1, 2, 3], function(){
673 expect(actual).toBe(scope);
676 it("should pass the item, index and array", function(){
682 every(data, function(val, index, arr){
689 expect(values).toEqual([1, 2, 3]);
690 expect(indexes).toEqual([0, 1, 2]);
691 expect(arrs).toEqual([data, data, data]);
695 it("should return true on an empty array", function(){
696 expect(every([], function(){})).toBeTruthy();
699 it("should throw an exception if no fn is passed", function(){
702 }).toRaiseExtError();
705 it("should stop as soon as a false value is found", function(){
709 result = every([true, true, false, true], function(v){
713 expect(count).toEqual(3);
714 expect(result).toBeFalsy();
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){
724 describe("some", function(){
725 var some = Ext.Array.some;
727 describe("scope/params", function(){
728 it("should execute in the specified scope", function(){
732 some([1, 2, 3], function(){
735 expect(actual).toBe(scope);
738 it("should pass the item, index and array", function(){
744 some(data, function(val, index, arr){
751 expect(values).toEqual([1]);
752 expect(indexes).toEqual([0]);
753 expect(arrs).toEqual([data]);
757 it("should return false on an empty array", function(){
758 expect(some([], function(){})).toBeFalsy();
761 it("should throw an exception if no fn is passed", function(){
764 }).toRaiseExtError();
767 it("should stop as soon as a matching value is found", function(){
771 result = some([1, 2, 3, 4], function(val){
775 expect(count).toEqual(3);
776 expect(result).toBeTruthy();
779 it("should return false if nothing matches the matcher function", function(){
783 result = some([1, 2, 3, 4, 5, 6, 7, 8, 9], function(val){
787 expect(count).toEqual(9);
788 expect(result).toBeFalsy();
792 describe("merge", function(){
793 var merge = Ext.Array.merge;
795 it("should return an empty array if run on an empty array", function(){
796 expect(merge([])).toEqual([]);
799 it("should return a new reference", function(){
801 expect(merge(arr)).not.toBe(arr);
804 it("should return a copy if all items are unique", function(){
805 expect(merge([6, 7, 8])).toEqual([6, 7, 8]);
808 it("should only use strict typing to match", function(){
809 expect(merge([1, '1'])).toEqual([1, '1']);
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]);
817 describe("intersect", function(){
818 var intersect = Ext.Array.intersect;
820 it("should return an empty array if no arrays are passed", function(){
821 expect(intersect()).toEqual([]);
824 it("should return an empty array if one empty array is passed", function(){
825 expect(intersect([])).toEqual([]);
828 it("should return a new reference", function(){
830 expect(intersect(arr)).not.toBe(arr);
833 it("should return a copy if one array is passed", function(){
834 expect(intersect([6, 7, 8])).toEqual([6, 7, 8]);
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]);
841 it("should return an empty array if there is no intersecting values", function(){
842 expect(intersect([1, 2, 3], [4, 5, 6])).toEqual([]);
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]);
849 it("should only use strict typing to match", function(){
850 expect(intersect([1], ['1'])).toEqual([]);
854 describe("difference", function(){
855 var difference = Ext.Array.difference;
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]);
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]);
865 it("should return a new reference", function(){
867 expect(difference(arr, [3, 2])).not.toBe(arr);
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]);
874 it("should only use strict typing to match", function(){
875 expect(difference([1], ['1'])).toEqual([1]);
879 describe("sort", function() {
881 beforeEach(function() {
882 sarray = ['bbb', 'addda', 'erere', 'fff', 'de3'];
883 narray = [1,3,2,4,6,7];
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']);
894 it("should be able to use a sortFn that returns a Number", function() {
895 Ext.Array.sort(sarray, function(a,b){
899 return a > b ? 1: -1;
901 expect(sarray).toEqual(['addda', 'bbb', 'de3', 'erere', 'fff']);
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]);
912 it("should be able to use a sortFn that returns a Number", function() {
913 Ext.Array.sort(narray, function(a,b){
916 expect(narray).toEqual([1,2,3,4,6,7]);
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);
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);
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);
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);
945 describe("sum", function() {
946 it("should return 21", function() {
947 expect(Ext.Array.sum([1,2,3,4,5,6])).toEqual(21);
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);