Tests/Specs: Replace Chai with expect.js for assertions, because of browser support.
[mootools.git] / Specs / Types / Array.js
blobaab5d2b5c9396124af3a3788a91ee63946d84d1f
1 /*
2 ---
3 name: Array
4 requires: ~
5 provides: ~
6 ...
7 */
9 (function(){
11 var getTestArray = function(){
12         var a = [0, 1, 2, 3];
13         delete a[1];
14         delete a[2];
15         return a;
19 describe("Array", function(){
21         // Array.flatten
23         it('should flatten a multidimensional array', function(){
24                 var arr = [1,2,3,[4,5,[6,7,[8]]], [[[[[9]]]]]];
25                 expect(arr.flatten()).to.eql([1,2,3,4,5,6,7,8,9]);
26         });
28         it('should flatten arguments', function(){
29                 var test = function(){
30                         return Array.flatten(arguments);
31                 };
32                 expect(test(1,2,3)).to.eql([1,2,3]);
33                 expect(test([1,2,3])).to.eql([1,2,3]);
34                 expect(test(1,2,[3])).to.eql([1,2,3]);
35         });
37         // Array.filter
39         it('should filter an array', function(){
40                 var array = [1,2,3,0,0,0];
41                 var arr = array.concat([false, null, 4]).filter(Type.isNumber);
42                 expect(arr).to.eql(array.concat(4));
43         });
45         it('filter should skip deleted elements', function(){
46                 var i = 0;
47                 getTestArray().filter(function(){
48                         i++;
49                         return true;
50                 });
52                 expect(i).to.equal(2);
53         });
55         // Array.clean
57         it('should clean an array from undefined and null values', function(){
58                 var array = [null, 1, 0, true, false, "foo", undefined];
59                 var arr = array.clean();
60                 expect(arr).to.eql([1, 0, true, false, "foo"]);
61         });
63         // Array.map
65         it('should return a mapping of an array', function(){
66                 var arr = [1,2,3,0,0,0].map(function(item){
67                         return (item + 1);
68                 });
70                 expect(arr).to.eql([2,3,4,1,1,1]);
71         });
73         it('map should skip deleted elements', function(){
74                 var i = 0;
75                 getTestArray().map(function(){
76                         return i++;
77                 });
79                 expect(i).to.equal(2);
80         });
82         // Array.every
84         it('should return true if every item matches the comparator, otherwise false', function(){
85                 expect([1,2,3,0,0,0].every(Type.isNumber)).to.equal(true);
87                 expect(['1',2,3,0].every(Type.isNumber)).to.equal(false);
88         });
90         it('every should skip deleted elements', function(){
91                 var i = 0;
92                 getTestArray().every(function(){
93                         i++;
94                         return true;
95                 });
97                 expect(i).to.equal(2);
98         });
100         // Array.some
102         it('should return true if some of the items in the array match the comparator, otherwise false', function(){
103                 expect(['1',2,3,0].some(Type.isNumber)).to.equal(true);
105                 expect([1,2,3,0,0,0].map(String).some(Type.isNumber)).to.equal(false);
106         });
108         it('some should skip deleted elements', function(){
109                 var i = 0;
110                 var a = getTestArray();
111                 delete a[0];
113                 // skips the first three elements
114                 a.some(function(value, index){
115                         i = index;
116                         return true;
117                 });
119                 expect(i).to.equal(3);
120         });
122         // Array.indexOf
124         it('should return the index of the item', function(){
125                 expect([1,2,3,0,0,0].indexOf(0)).to.equal(3);
126         });
128         it('should return -1 if the item is not found in the array', function(){
129                 expect([1,2,3,0,0,0].indexOf('not found')).to.equal(-1);
130         });
132         // Array.erase
134         it('should remove all items in the array that match the specified item', function(){
135                 var arr = [1,2,3,0,0,0].erase(0);
136                 expect(arr).to.eql([1,2,3]);
137         });
139         // Array.contains
141         it('should return true if the array contains the specified item', function(){
142                 expect([1,2,3,0,0,0].contains(0)).to.equal(true);
143         });
145         it('should return false if the array does not contain the specified item', function(){
146                 expect([0,1,2].contains('not found')).to.equal(false);
147         });
149         // Array.associate
151         it('should associate an array with a specified array', function(){
152                 var obj = [1,2,3,0,0,0].associate(['a', 'b', 'c', 'd']);
153                 expect(obj).to.eql({a:1, b:2, c:3, d:0});
154         });
156         // Array.append
158         it('should append to an array', function(){
159                 var a = [1,2,4];
160                 var b = [2,3,4,5];
161                 a.append(b);
162                 expect(a).to.eql([1,2,4,2,3,4,5]);
163                 expect(b).to.eql([2,3,4,5]);
164         });
166         var isType = function(type){
167                 return function(obj){
168                         return typeOf(obj) == type;
169                 };
170         };
172         // Array.link
173         it('should link an array items to a new object according to the specified matchers', function(){
174                 if (typeof document === 'undefined'){
175                         var el = {$family: function(){ return 'element'; }};
176                 } else {
177                         var el = document.createElement('div');
178                 }
180                 var assoc2 = [100, 'Hello', {foo: 'bar'}, el, false].link({
181                         myNumber: isType('number'),
182                         myElement: isType('element'),
183                         myObject: isType('object'),
184                         myString: isType('string'),
185                         myBoolean: isType('boolean')
186                 });
188                 expect(assoc2).to.eql({
189                         myNumber: 100,
190                         myElement: el,
191                         myObject: {foo: 'bar'},
192                         myString: 'Hello',
193                         myBoolean: false
194                 });
195         });
197         //<1.2compat>
198         // Array.extend
199         it('should extend an array', function(){
200                 var a = [1,2,4];
201                 var b = [2,3,4,5];
202                 a.extend(b);
203                 expect(a).to.eql([1,2,4,2,3,4,5]);
204                 expect(b).to.eql([2,3,4,5]);
205         });
206         //</1.2compat>
208         it('should append an array', function(){
209                 var a = [1,2,4];
210                 var b = [2,3,4,5];
211                 a.append(b);
212                 expect(a).to.eql([1,2,4,2,3,4,5]);
213                 expect(b).to.eql([2,3,4,5]);
214         });
216         // Array.combine
218         it('should combine an array', function(){
219                 var arr = [1,2,3,4].combine([3,1,4,5,6,7]);
220                 expect(arr).to.eql([1,2,3,4,5,6,7]);
221         });
223         // Array.include
225         it('should include only new items', function(){
226                 var arr = [1,2,3,4].include(1).include(5);
227                 expect(arr).to.eql([1,2,3,4,5]);
228         });
230         // Array.getLast
232         it('should return the last item in the array', function(){
233                 expect([1,2,3,0,0,0].getLast()).to.equal(0);
234                 expect([3].getLast()).to.equal(3);
235         });
237         it('should return null if there are no items', function(){
238                 expect([].getLast()).to.equal(null);
239         });
241         // Array.empty
243         it('should empty the array', function(){
244                 var arr = [1,2,3,4].empty();
245                 expect(arr).to.eql([]);
246         });
250 describe("Array Color Methods", function(){
252         // Array.hexToRgb
254         it('should return null if the length of the array is not 3', function(){
255                 expect([].hexToRgb()).to.equal(null);
256         });
258         it('should return a CSS rgb string', function(){
259                 expect(['0','0','0'].hexToRgb()).to.equal('rgb(0,0,0)');
260         });
262         it('should support shorthand hex', function(){
263                 expect(['c','c','c'].hexToRgb()).to.equal('rgb(204,204,204)');
264         });
266         it('should return an array with 16-based numbers when passed true', function(){
267                 expect(['ff','ff','ff'].hexToRgb(true)).to.eql([255,255,255]);
268         });
270         // Array.rgbToHex
272         it('should return null if the array does not have at least 3 times', function(){
273                 expect([0,1].rgbToHex()).to.equal(null);
274         });
276         it('should return a css hexadecimal string', function(){
277                 expect(['255', '0', '0'].rgbToHex()).to.equal('#ff0000');
278                 expect([0,0,255].rgbToHex()).to.equal('#0000ff');
279         });
281         it('should return an array with hexadecimal string items', function(){
282                 expect([0,255,0].rgbToHex(true)).to.eql(['00', 'ff', '00']);
283         });
285         it('should return `transparent` if the fourth item is 0 and first param is not true', function(){
286                 expect([0,0,0,0].rgbToHex()).to.equal('transparent');
287         });
291 describe('Array.getRandom', function(){
293         it('should get a random element from an array', function(){
294                 var a = [1];
296                 expect(a.getRandom()).to.equal(1);
298                 a.push(2);
300                 // Let's try a few times
301                 expect(a).to.contain(a.getRandom());
302                 expect(a).to.contain(a.getRandom());
303                 expect(a).to.contain(a.getRandom());
304                 expect(a).to.contain(a.getRandom());
305         });
309 describe('Array.pick', function(){
311         it('should pick a value that is not null from the array', function(){
312                 expect([null, undefined, true, 1].pick()).to.equal(true);
313         });
315         it('should return null', function(){
316                 expect([].pick()).to.equal(null);
317         });
321 describe('Array', function(){
323         describe('Array.map', function(){
325                 it('should return an array with the same length', function(){
326                         expect([1, 2, 3, undefined].map(function(v){
327                                 return v;
328                         }).length).to.equal(4);
329                 });
331                 it('shoud return an empty array when the thisArg does not has a length property', function(){
332                         expect([].map.call({}, function(){
333                                 return 1;
334                         })).to.eql([]);
335                 });
337         });
339         it('should accept thisArgs without length property', function(){
340                 var object = {}, fn = function(){};
341                 expect([].every.call(object, fn)).to.equal(true);
342                 expect([].filter.call(object, fn)).to.eql([]);
343                 expect([].indexOf.call(object)).to.equal(-1);
344                 expect([].map.call(object, fn)).to.eql([]);
345                 expect([].some.call(object, fn)).to.equal(false);
346         });
348         describe('Array.filter', function(){
350                 it('should return the original item, and not any mutations.', function(){
352                         var result = [0, 1, 2].filter(function(num, i, array){
353                                 if (num == 1){
354                                         array[i] = 'mutation';
355                                         return true;
356                                 }
357                         });
359                         expect(result[0]).to.equal(1);
360                 });
362         });
366 })();