Tests/Specs: Replace Chai with expect.js for assertions, because of browser support.
[mootools.git] / Specs / Types / Object.js
blob3ff61ca92fce8ee416e25287286bbeb1a3b25300
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'])).to.eql({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).to.eql({b: 'string', d: null});
28                 // To equal doesn't check for undefined properties
29                 expect('a' in subset).to.equal(false);
30                 expect('c' in subset).to.equal(false);
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')).to.equal('a');
37                 expect(Object.keyOf(object, 'not found')).to.equal(null);
38         });
40         // Object.contains
42         it('should return true if the object contains value otherwise false', function(){
43                 expect(Object.contains(object, 'string')).to.equal(true);
44                 expect(Object.contains(object, 'not found')).to.equal(false);
45         });
47         // Object.map
49         it('should map a new object according to the comparator', function(){
50                 expect(Object.map(object, Type.isNumber)).to.eql({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)).to.eql({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)).to.equal(true);
63                 expect(Object.every(object, Type.isNumber)).to.equal(false);
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)).to.equal(true);
70                 expect(Object.some(object, Type.isArray)).to.equal(false);
71         });
73         // Object.values
75         it('values should return an empty array', function(){
76                 expect(Object.values({})).to.eql([]);
77         });
79         it('should return an array with the values of the object', function(){
80                 expect(Object.values(object)).to.eql(['string', 233, {}]);
81         });
83         // Object.toQueryString
85         it('should return a query string', function(){
86                 var myObject = {apple: "red", lemon: "yellow"};
87                 expect(Object.toQueryString(myObject)).to.equal('apple=red&lemon=yellow');
89                 var myObject2 = {apple: ['red', 'yellow'], lemon: ['green', 'yellow']};
90                 expect(Object.toQueryString(myObject2)).to.equal('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
92                 var myObject3 = {fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}};
93                 expect(Object.toQueryString(myObject3)).to.equal('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');
94         });
96 });
98 describe('Object.getLength', function(){
100         it('should get the amount of items in an object', function(){
101                 var object = {
102                         a: [0, 1, 2],
103                         s: "It's-me-Valerio!",
104                         n: 1,
105                         f: 3.14,
106                         b: false
107                 };
109                 expect(Object.getLength(object)).to.equal(5);
111                 object.n = null;
113                 expect(Object.getLength(object)).to.equal(5);
114         });
119 describe('Object hasOwnProperty', function(){
121         var dit = (typeof window === 'undefined' ? xit : it);
122         dit('should not fail on window', function(){
123                 expect(function(){
124                         var fn = function(){};
125                         Object.each(window, fn);
126                         Object.keys(window);
127                         Object.values(window);
128                         Object.map(window, fn);
129                         Object.every(window, fn);
130                         Object.some(window, fn);
131                         Object.keyOf(window, document);
132                 }).to.not.throwError();
133         });
137 })();