1 describe("Ext.Object", function(){
3 describe("getKeys", function(){
4 var getKeys = Ext.Object.getKeys;
5 it("should return an empty array for a null value", function(){
6 expect(getKeys(null)).toEqual([]);
9 it("should return an empty array for an empty object", function(){
10 expect(getKeys({})).toEqual([]);
13 it("should return all the keys in the object", function(){
18 })).toEqual(['foo', 'bar', 'baz']);
22 describe("getValues", function(){
23 var getValues = Ext.Object.getValues;
24 it("should return an empty array for a null value", function(){
25 expect(getValues(null)).toEqual([]);
28 it("should return an empty array for an empty object", function(){
29 expect(getValues({})).toEqual([]);
32 it("should return all the values in the object", function(){
37 })).toEqual([1, 2, 3]);
41 describe("getKey", function(){
42 var getKey = Ext.Object.getKey;
44 it("should return null for a null object", function(){
45 expect(getKey(null, 'foo')).toBeNull();
48 it("should return null for an empty object", function(){
49 expect(getKey({}, 'foo')).toBeNull();
52 it("should return null if the value doesn't exist", function(){
59 it("should only do strict matching", function(){
65 it("should return the correct key if it matches", function(){
68 }, 1)).toEqual('foo');
71 it("should only return the first matched value", function(){
75 }, 1)).toEqual('bar');
79 describe("each", function(){
80 var each = Ext.Object.each;
82 describe("scope/params", function(){
83 it("should execute using the passed scope", function(){
94 expect(actual).toBe(scope);
97 it("should default the scope to the object", function(){
107 expect(actual).toBe(o);
110 it("should execute passing the key value and object", function(){
120 each(data, function(key, value, o){
126 expect(keys).toEqual(['foo', 'bar', 'baz']);
127 expect(values).toEqual([1, 'value', false]);
128 expect(obj).toBe(data);
132 describe("stopping", function(){
133 it("should not stop by default", function(){
143 expect(count).toEqual(4);
146 it("should only stop if the function returns false", function(){
157 expect(count).toEqual(4);
160 it("should stop immediately when false is returned", function(){
171 expect(count).toEqual(2);
176 describe("toQueryString", function(){
177 var toQueryString = Ext.Object.toQueryString;
179 describe("defaults", function(){
180 it("should return an empty string for a null object", function(){
181 expect(toQueryString(null)).toEqual('');
184 it("should return an empty string for an empty object", function(){
185 expect(toQueryString({})).toEqual('');
189 describe("simple values", function(){
190 it("should separate a property/value by an =", function(){
191 expect(toQueryString({
193 })).toEqual('foo=1');
196 it("should separate pairs by an &", function(){
197 expect(toQueryString({
200 })).toEqual('foo=1&bar=2');
203 it("should handle properties with empty values", function(){
204 expect(toQueryString({
209 it("should encode dates", function(){
210 var d = new Date(2011, 0, 1);
211 expect(toQueryString({
213 })).toEqual('foo=2011-01-01T00%3A00%3A00');
216 it("should url encode the key", function(){
217 expect(toQueryString({
219 })).toEqual('a%20prop=1');
222 it("should url encode the value", function(){
223 expect(toQueryString({
224 prop: '$300 & 5 cents'
225 })).toEqual('prop=%24300%20%26%205%20cents');
228 it("should encode both key and value at the same time", function(){
229 expect(toQueryString({
231 })).toEqual('a%20prop=%24300');
235 describe("arrays", function(){
236 it("should support an array value", function(){
237 expect(toQueryString({
239 })).toEqual('foo=1&foo=2&foo=3');
242 it("should be able to support multiple arrays", function(){
243 expect(toQueryString({
246 })).toEqual('foo=1&foo=2&bar=3&bar=4');
249 it("should be able to mix arrays and normal values", function(){
250 expect(toQueryString({
252 bar: ['val2', 'val3'],
254 })).toEqual('foo=val1&bar=val2&bar=val3&baz=val4');
258 describe("recursive", function() {
259 it("should support both nested arrays and objects", function() {
260 expect(decodeURIComponent(Ext.Object.toQueryString({
267 hobbies: ['coding', 'eating', 'sleeping', [1,2]]
268 }, 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')
274 describe("merge", function(){
275 var merge = Ext.Object.merge;
277 describe("simple values", function(){
278 it("should copy over numeric values", function(){
279 expect(merge({}, 'prop1', 1)).toEqual({
284 it("should copy over string values", function(){
285 expect(merge({}, 'prop1', 'val')).toEqual({
290 it("should copy over boolean values", function(){
291 expect(merge({}, 'prop1', true)).toEqual({
296 it("should copy over null values", function(){
297 expect(merge({}, 'prop1', null)).toEqual({
303 describe("complex values", function(){
304 it("should copy a simple object but not have the same reference", function(){
310 }, result = merge({}, 'prop', o);
312 expect(result.prop).toEqual({
318 expect(result.prop).not.toBe(o);
321 it("should NOT merge an instance (the constructor of which is not Object)", function(){
322 var o = new Ext.Base(),
323 result = merge({}, 'prop1', o);
325 expect(result.prop1).toBe(o);
329 describe("overwriting properties", function(){
330 it("should merge objects if an object exists on the source and the passed value is an object literal", function(){
345 it("should copy an object reference if an object exists on the source and the passed value is some kind of class", function(){
346 var o = new Ext.Base(),
351 expect(result).toEqual({
354 expect(result.prop).toBe(o);
357 it("should replace the value of the target object if it is not an object", function(){
358 var o = new Ext.Base(),
363 expect(result.prop).toEqual(o);
364 expect(result.prop).toBe(o);
367 it("should overwrite simple values", function(){
370 }, 'prop', 2)).toEqual({
376 describe("merging objects", function(){
377 it("should merge objects", function(){
385 it("should merge left to right", function(){
398 it("should modify and return the source", function(){
400 result = merge(o, 'foo', 'bar');
402 expect(result.foo).toEqual('bar');
403 expect(result).toBe(o);
408 describe("toQueryObjects", function() {
416 hobbies: ['coding', 'eating', 'sleeping', [1,2,3]]
419 it("simple key value", function() {
420 expect(Ext.Object.toQueryObjects('username', 'Jacky')).toEqual([
428 it("non-recursive array", function() {
429 expect(Ext.Object.toQueryObjects('hobbies', ['eating', 'sleeping', 'coding'])).toEqual([
445 it("recursive object", function() {
446 expect(Ext.Object.toQueryObjects('dateOfBirth', {
460 name: 'dateOfBirth[day]',
464 name: 'dateOfBirth[month]',
468 name: 'dateOfBirth[year]',
472 name: 'dateOfBirth[somethingElse][nested][very]',
476 name: 'dateOfBirth[somethingElse][nested][deep][inHere]',
482 it("recursive array", function() {
483 expect(Ext.Object.toQueryObjects('hobbies', [
484 'eating', 'sleeping', 'coding', ['even', ['more']]
499 name: 'hobbies[3][0]',
503 name: 'hobbies[3][1][0]',
510 describe("fromQueryString", function() {
511 var fromQueryString = Ext.Object.fromQueryString;
513 describe("standard mode", function() {
514 it("empty string", function(){
515 expect(fromQueryString('')).toEqual({});
518 it("simple single key value pair", function(){
519 expect(fromQueryString('name=Jacky')).toEqual({name: 'Jacky'});
522 it("simple single key value pair with empty value", function(){
523 expect(fromQueryString('name=')).toEqual({name: ''});
526 it("multiple key value pairs", function(){
527 expect(fromQueryString('name=Jacky&loves=food')).toEqual({name: 'Jacky', loves: 'food'});
530 it("multiple key value pairs with URI encoded component", function(){
531 expect(fromQueryString('a%20property=%24300%20%26%205%20cents')).toEqual({'a property': '$300 & 5 cents'});
534 it("simple array", function(){
535 expect(fromQueryString('foo=1&foo=2&foo=3')).toEqual({foo: ['1', '2', '3']});
539 describe("recursive mode", function() {
540 it("empty string", function(){
541 expect(fromQueryString('', true)).toEqual({});
544 it("simple single key value pair", function(){
545 expect(fromQueryString('name=Jacky', true)).toEqual({name: 'Jacky'});
548 it("simple single key value pair with empty value", function(){
549 expect(fromQueryString('name=', true)).toEqual({name: ''});
552 it("multiple key value pairs", function(){
553 expect(fromQueryString('name=Jacky&loves=food', true)).toEqual({name: 'Jacky', loves: 'food'});
556 it("multiple key value pairs with URI encoded component", function(){
557 expect(fromQueryString('a%20property=%24300%20%26%205%20cents', true)).toEqual({'a property': '$300 & 5 cents'});
560 it("simple array (last value with the same name will overwrite previous value)", function(){
561 expect(fromQueryString('foo=1&foo=2&foo=3', true)).toEqual({foo: '3'});
564 it("simple array with empty brackets", function(){
565 expect(fromQueryString('foo[]=1&foo[]=2&foo[]=3', true)).toEqual({foo: ['1', '2', '3']});
568 it("simple array with non-empty brackets", function(){
569 expect(fromQueryString('foo[0]=1&foo[1]=2&foo[2]=3', true)).toEqual({foo: ['1', '2', '3']});
572 it("simple array with non-empty brackets and non sequential keys", function(){
573 expect(fromQueryString('foo[3]=1&foo[1]=2&foo[2]=3&foo[0]=0', true)).toEqual({foo: ['0', '2', '3', '1']});
576 it("simple array with non-empty brackets and non sequential keys and holes", function(){
577 expect(fromQueryString('foo[3]=1&foo[1]=2&foo[2]=3', true)).toEqual({foo: [undefined, '2', '3', '1']});
580 it("nested array", function(){
581 expect(fromQueryString('some[0][0]=stuff&some[0][1]=morestuff&some[0][]=otherstuff&some[1]=thingelse', true)).toEqual({
583 ['stuff', 'morestuff', 'otherstuff'],
589 it("nested object", function(){
590 expect(fromQueryString('dateOfBirth[day]=1&dateOfBirth[month]=2&dateOfBirth[year]=1911&dateOfBirth[extra][hour]=4&dateOfBirth[extra][minute]=30', true)).toEqual({
603 it("nested mixed types", function(){
604 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({
611 hobbies: ['coding', 'eating', 'sleeping', ['nested', 'stuff']]