Concat sinon files, add them under Tests/Utilities
[mootools.git] / Specs / Types / Object.js
blobe6d0b9d891911d6240f5dbb979b85ac5bc5364c2
1 /*
2 ---
3 name: Object
4 requires: ~
5 provides: ~
6 ...
7 */
9 (function(){
11 var object = { a: 'string', b: 233, c: {} };
13 describe("Object Methods", function(){
15         // Object subset
17         it('should return an object with only the specified keys', function(){
18                 expect(Object.subset(object, ['a', 'b'])).toEqual({a:'string',b:233});
19         });
21         it('should ignore undefined keys', function(){
22                 var obj = {
23                         b: 'string',
24                         d: null
25                 };
26                 var subset = Object.subset(obj, ['a', 'b', 'c', 'd']);
27                 expect(subset).toEqual({b: 'string', d: null});
28                 // To equal doesn't check for undefined properties
29                 expect('a' in subset).toBeFalsy();
30                 expect('c' in subset).toBeFalsy();
31         });
33         // Object keyOf
35         it('should return the key of the value or null if not found', function(){
36                 expect(Object.keyOf(object, 'string')).toEqual('a');
37                 expect(Object.keyOf(object, 'not found')).toBeNull();
38         });
40         // Object.contains
42         it('should return true if the object contains value otherwise false', function(){
43                 expect(Object.contains(object, 'string')).toBeTruthy();
44                 expect(Object.contains(object, 'not found')).toBeFalsy();
45         });
47         // Object.map
49         it('should map a new object according to the comparator', function(){
50                 expect(Object.map(object, Type.isNumber)).toEqual({a:false,b:true,c:false});
51         });
53         // Object.filter
55         it('should filter the object according to the comparator', function(){
56                 expect(Object.filter(object, Type.isNumber)).toEqual({b:233});
57         });
59         // Object.every
61         it('should return true if every value matches the comparator, otherwise false', function(){
62                 expect(Object.every(object, typeOf)).toBeTruthy();
63                 expect(Object.every(object, Type.isNumber)).toBeFalsy();
64         });
66         // Object.some
68         it('should return true if some of the values match the comparator, otherwise false', function(){
69                 expect(Object.some(object, Type.isNumber)).toBeTruthy();
70                 expect(Object.some(object, Type.isArray)).toBeFalsy();
71         });
73         // Object.keys
75         it('keys should return an empty array', function(){
76                 expect(Object.keys({})).toEqual([]);
77         });
79         it('should return an array containing the keys of the object', function(){
80                 expect(Object.keys(object)).toEqual(['a', 'b', 'c']);
81         });
83         // Object.values
85         it('values should return an empty array', function(){
86                 expect(Object.values({})).toEqual([]);
87         });
89         it('should return an array with the values of the object', function(){
90                 expect(Object.values(object)).toEqual(['string', 233, {}]);
91         });
93         // Object.toQueryString
95         it('should return a query string', function(){
96                 var myObject = {apple: "red", lemon: "yellow"};
97                 expect(Object.toQueryString(myObject)).toEqual('apple=red&lemon=yellow');
99                 var myObject2 = {apple: ['red', 'yellow'], lemon: ['green', 'yellow']};
100                 expect(Object.toQueryString(myObject2)).toEqual('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
102                 var myObject3 = {fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}};
103                 expect(Object.toQueryString(myObject3)).toEqual('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');
104         });
108 describe('Object.getLength', function(){
110         it('should get the amount of items in an object', function(){
111                 var object = {
112                         a: [0, 1, 2],
113                         s: "It's-me-Valerio!",
114                         n: 1,
115                         f: 3.14,
116                         b: false
117                 };
119                 expect(Object.getLength(object)).toEqual(5);
121                 object.n = null;
123                 expect(Object.getLength(object)).toEqual(5);
124         });
129 describe('Object hasOwnProperty', function(){
131         it('should not fail on window', function(){
132                 expect(function(){
133                         var fn = function(){};
134                         Object.each(window, fn);
135                         Object.keys(window);
136                         Object.values(window);
137                         Object.map(window, fn);
138                         Object.every(window, fn);
139                         Object.some(window, fn);
140                         Object.keyOf(window, document);
141                 }).not.toThrow();
142         });
146 })();