3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
15 describe("Ext.Object", function(){
17 describe("getKeys", function(){
18 var getKeys = Ext.Object.getKeys;
19 it("should return an empty array for a null value", function(){
20 expect(getKeys(null)).toEqual([]);
23 it("should return an empty array for an empty object", function(){
24 expect(getKeys({})).toEqual([]);
27 it("should return all the keys in the object", function(){
32 })).toEqual(['foo', 'bar', 'baz']);
36 describe("getValues", function(){
37 var getValues = Ext.Object.getValues;
38 it("should return an empty array for a null value", function(){
39 expect(getValues(null)).toEqual([]);
42 it("should return an empty array for an empty object", function(){
43 expect(getValues({})).toEqual([]);
46 it("should return all the values in the object", function(){
51 })).toEqual([1, 2, 3]);
55 describe("getKey", function(){
56 var getKey = Ext.Object.getKey;
58 it("should return null for a null object", function(){
59 expect(getKey(null, 'foo')).toBeNull();
62 it("should return null for an empty object", function(){
63 expect(getKey({}, 'foo')).toBeNull();
66 it("should return null if the value doesn't exist", function(){
73 it("should only do strict matching", function(){
79 it("should return the correct key if it matches", function(){
82 }, 1)).toEqual('foo');
85 it("should only return the first matched value", function(){
89 }, 1)).toEqual('bar');
93 describe("each", function(){
94 var each = Ext.Object.each;
96 describe("scope/params", function(){
97 it("should execute using the passed scope", function(){
108 expect(actual).toBe(scope);
111 it("should default the scope to the object", function(){
121 expect(actual).toBe(o);
124 it("should execute passing the key value and object", function(){
134 each(data, function(key, value, o){
140 expect(keys).toEqual(['foo', 'bar', 'baz']);
141 expect(values).toEqual([1, 'value', false]);
142 expect(obj).toBe(data);
146 describe("stopping", function(){
147 it("should not stop by default", function(){
157 expect(count).toEqual(4);
160 it("should only stop if the function returns false", function(){
171 expect(count).toEqual(4);
174 it("should stop immediately when false is returned", function(){
185 expect(count).toEqual(2);
190 describe("toQueryString", function(){
191 var toQueryString = Ext.Object.toQueryString;
193 describe("defaults", function(){
194 it("should return an empty string for a null object", function(){
195 expect(toQueryString(null)).toEqual('');
198 it("should return an empty string for an empty object", function(){
199 expect(toQueryString({})).toEqual('');
203 describe("simple values", function(){
204 it("should separate a property/value by an =", function(){
205 expect(toQueryString({
207 })).toEqual('foo=1');
210 it("should separate pairs by an &", function(){
211 expect(toQueryString({
214 })).toEqual('foo=1&bar=2');
217 it("should handle properties with empty values", function(){
218 expect(toQueryString({
223 it("should encode dates", function(){
224 var d = new Date(2011, 0, 1);
225 expect(toQueryString({
227 })).toEqual('foo=2011-01-01T00%3A00%3A00');
230 it("should url encode the key", function(){
231 expect(toQueryString({
233 })).toEqual('a%20prop=1');
236 it("should url encode the value", function(){
237 expect(toQueryString({
238 prop: '$300 & 5 cents'
239 })).toEqual('prop=%24300%20%26%205%20cents');
242 it("should encode both key and value at the same time", function(){
243 expect(toQueryString({
245 })).toEqual('a%20prop=%24300');
249 describe("arrays", function(){
250 it("should support an array value", function(){
251 expect(toQueryString({
253 })).toEqual('foo=1&foo=2&foo=3');
256 it("should be able to support multiple arrays", function(){
257 expect(toQueryString({
260 })).toEqual('foo=1&foo=2&bar=3&bar=4');
263 it("should be able to mix arrays and normal values", function(){
264 expect(toQueryString({
266 bar: ['val2', 'val3'],
268 })).toEqual('foo=val1&bar=val2&bar=val3&baz=val4');
272 describe("recursive", function() {
273 it("should support both nested arrays and objects", function() {
274 expect(decodeURIComponent(Ext.Object.toQueryString({
281 hobbies: ['coding', 'eating', 'sleeping', [1,2]]
282 }, true))).toEqual('username=Jacky&dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911&hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&hobbies[3][0]=1&hobbies[3][1]=2')
288 describe("merge", function(){
289 var merge = Ext.Object.merge;
291 describe("simple values", function(){
292 it("should copy over numeric values", function(){
293 expect(merge({}, 'prop1', 1)).toEqual({
298 it("should copy over string values", function(){
299 expect(merge({}, 'prop1', 'val')).toEqual({
304 it("should copy over boolean values", function(){
305 expect(merge({}, 'prop1', true)).toEqual({
310 it("should copy over null values", function(){
311 expect(merge({}, 'prop1', null)).toEqual({
317 describe("complex values", function(){
318 it("should copy a simple object but not have the same reference", function(){
324 }, result = merge({}, 'prop', o);
326 expect(result.prop).toEqual({
332 expect(result.prop).not.toBe(o);
335 it("should NOT merge an instance (the constructor of which is not Object)", function(){
336 var o = new Ext.Base(),
337 result = merge({}, 'prop1', o);
339 expect(result.prop1).toBe(o);
343 describe("overwriting properties", function(){
344 it("should merge objects if an object exists on the source and the passed value is an object literal", function(){
359 it("should copy an object reference if an object exists on the source and the passed value is some kind of class", function(){
360 var o = new Ext.Base(),
365 expect(result).toEqual({
368 expect(result.prop).toBe(o);
371 it("should replace the value of the target object if it is not an object", function(){
372 var o = new Ext.Base(),
377 expect(result.prop).toEqual(o);
378 expect(result.prop).toBe(o);
381 it("should overwrite simple values", function(){
384 }, 'prop', 2)).toEqual({
390 describe("merging objects", function(){
391 it("should merge objects", function(){
399 it("should merge left to right", function(){
412 it("should modify and return the source", function(){
414 result = merge(o, 'foo', 'bar');
416 expect(result.foo).toEqual('bar');
417 expect(result).toBe(o);
422 describe("toQueryObjects", function() {
430 hobbies: ['coding', 'eating', 'sleeping', [1,2,3]]
433 it("simple key value", function() {
434 expect(Ext.Object.toQueryObjects('username', 'Jacky')).toEqual([
442 it("non-recursive array", function() {
443 expect(Ext.Object.toQueryObjects('hobbies', ['eating', 'sleeping', 'coding'])).toEqual([
459 it("recursive object", function() {
460 expect(Ext.Object.toQueryObjects('dateOfBirth', {
474 name: 'dateOfBirth[day]',
478 name: 'dateOfBirth[month]',
482 name: 'dateOfBirth[year]',
486 name: 'dateOfBirth[somethingElse][nested][very]',
490 name: 'dateOfBirth[somethingElse][nested][deep][inHere]',
496 it("recursive array", function() {
497 expect(Ext.Object.toQueryObjects('hobbies', [
498 'eating', 'sleeping', 'coding', ['even', ['more']]
513 name: 'hobbies[3][0]',
517 name: 'hobbies[3][1][0]',
524 describe("fromQueryString", function() {
525 var fromQueryString = Ext.Object.fromQueryString;
527 describe("standard mode", function() {
528 it("empty string", function(){
529 expect(fromQueryString('')).toEqual({});
532 it("simple single key value pair", function(){
533 expect(fromQueryString('name=Jacky')).toEqual({name: 'Jacky'});
536 it("simple single key value pair with empty value", function(){
537 expect(fromQueryString('name=')).toEqual({name: ''});
540 it("multiple key value pairs", function(){
541 expect(fromQueryString('name=Jacky&loves=food')).toEqual({name: 'Jacky', loves: 'food'});
544 it("multiple key value pairs with URI encoded component", function(){
545 expect(fromQueryString('a%20property=%24300%20%26%205%20cents')).toEqual({'a property': '$300 & 5 cents'});
548 it("simple array", function(){
549 expect(fromQueryString('foo=1&foo=2&foo=3')).toEqual({foo: ['1', '2', '3']});
553 describe("recursive mode", function() {
554 it("empty string", function(){
555 expect(fromQueryString('', true)).toEqual({});
558 it("simple single key value pair", function(){
559 expect(fromQueryString('name=Jacky', true)).toEqual({name: 'Jacky'});
562 it("simple single key value pair with empty value", function(){
563 expect(fromQueryString('name=', true)).toEqual({name: ''});
566 it("multiple key value pairs", function(){
567 expect(fromQueryString('name=Jacky&loves=food', true)).toEqual({name: 'Jacky', loves: 'food'});
570 it("multiple key value pairs with URI encoded component", function(){
571 expect(fromQueryString('a%20property=%24300%20%26%205%20cents', true)).toEqual({'a property': '$300 & 5 cents'});
574 it("simple array (last value with the same name will overwrite previous value)", function(){
575 expect(fromQueryString('foo=1&foo=2&foo=3', true)).toEqual({foo: '3'});
578 it("simple array with empty brackets", function(){
579 expect(fromQueryString('foo[]=1&foo[]=2&foo[]=3', true)).toEqual({foo: ['1', '2', '3']});
582 it("simple array with non-empty brackets", function(){
583 expect(fromQueryString('foo[0]=1&foo[1]=2&foo[2]=3', true)).toEqual({foo: ['1', '2', '3']});
586 it("simple array with non-empty brackets and non sequential keys", function(){
587 expect(fromQueryString('foo[3]=1&foo[1]=2&foo[2]=3&foo[0]=0', true)).toEqual({foo: ['0', '2', '3', '1']});
590 it("simple array with non-empty brackets and non sequential keys and holes", function(){
591 expect(fromQueryString('foo[3]=1&foo[1]=2&foo[2]=3', true)).toEqual({foo: [undefined, '2', '3', '1']});
594 it("nested array", function(){
595 expect(fromQueryString('some[0][0]=stuff&some[0][1]=morestuff&some[0][]=otherstuff&some[1]=thingelse', true)).toEqual({
597 ['stuff', 'morestuff', 'otherstuff'],
603 it("nested object", function(){
604 expect(fromQueryString('dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911&dateOfBirth[extra][hour]=4&dateOfBirth[extra][minute]=30', true)).toEqual({
617 it("nested mixed types", function(){
618 expect(fromQueryString('username=Jacky&dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911&hobbies[0]=coding&hobbies[1]=eating&hobbies[2]=sleeping&hobbies[3][0]=nested&hobbies[3][1]=stuff', true)).toEqual({
625 hobbies: ['coding', 'eating', 'sleeping', ['nested', 'stuff']]