corrade-http-templates – Blame information for rev 62

Subversion Repositories:
Rev:
Rev Author Line No. Line
62 office 1 define( [
2 "qunit",
3 "jquery",
4 "ui/widget"
5 ], function( QUnit, $ ) {
6  
7 QUnit.test( "$.widget.extend()", function( assert ) {
8 assert.expect( 27 );
9  
10 var ret, empty, optionsWithLength, optionsWithDate, myKlass, customObject, optionsWithCustomObject, nullUndef,
11 target, recursive, obj, input, output,
12 settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
13 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
14 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
15 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
16 deep1 = { foo: { bar: true } },
17 deep2 = { foo: { baz: true }, foo2: document },
18 deep2copy = { foo: { baz: true }, foo2: document },
19 deepmerged = { foo: { bar: true, baz: true }, foo2: document },
20 arr = [ 1, 2, 3 ],
21 nestedarray = { arr: arr },
22 defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
23 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
24 options1 = { xnumber2: 1, xstring2: "x" },
25 options1Copy = { xnumber2: 1, xstring2: "x" },
26 options2 = { xstring2: "xx", xxx: "newstringx" },
27 options2Copy = { xstring2: "xx", xxx: "newstringx" },
28 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
29  
30 $.widget.extend( settings, options );
31 assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
32 assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
33  
34 $.widget.extend( deep1, deep2 );
35 assert.deepEqual( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
36 assert.deepEqual( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
37 assert.equal( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
38  
39 assert.strictEqual( $.widget.extend( {}, nestedarray ).arr, arr, "Don't clone arrays" );
40 assert.ok( $.isPlainObject( $.widget.extend( { arr: arr }, { arr: {} } ).arr ), "Cloned object heve to be an plain object" );
41  
42 empty = {};
43 optionsWithLength = { foo: { length: -1 } };
44 $.widget.extend( empty, optionsWithLength );
45 assert.deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
46  
47 empty = {};
48 optionsWithDate = { foo: { date: new Date() } };
49 $.widget.extend( empty, optionsWithDate );
50 assert.deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
51  
52 myKlass = function() {};
53 customObject = new myKlass();
54 optionsWithCustomObject = { foo: { date: customObject } };
55 empty = {};
56 $.widget.extend( empty, optionsWithCustomObject );
57 assert.strictEqual( empty.foo.date, customObject, "Custom objects copy correctly (no methods)" );
58  
59 // Makes the class a little more realistic
60 myKlass.prototype = { someMethod: function() {} };
61 empty = {};
62 $.widget.extend( empty, optionsWithCustomObject );
63 assert.strictEqual( empty.foo.date, customObject, "Custom objects copy correctly" );
64  
65 ret = $.widget.extend( { foo: 4 }, { foo: Number( 5 ) } );
66 assert.equal( ret.foo, 5, "Wrapped numbers copy correctly" );
67  
68 nullUndef = $.widget.extend( {}, options, { xnumber2: null } );
69 assert.strictEqual( nullUndef.xnumber2, null, "Check to make sure null values are copied" );
70  
71 nullUndef = $.widget.extend( {}, options, { xnumber2: undefined } );
72 assert.strictEqual( nullUndef.xnumber2, options.xnumber2, "Check to make sure undefined values are not copied" );
73  
74 nullUndef = $.widget.extend( {}, options, { xnumber0: null } );
75 assert.strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted" );
76  
77 target = {};
78 recursive = { foo:target, bar:5 };
79 $.widget.extend( target, recursive );
80 assert.deepEqual( target, { foo: {}, bar: 5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
81  
82 ret = $.widget.extend( { foo: [] }, { foo: [ 0 ] } ); // 1907
83 assert.equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
84  
85 ret = $.widget.extend( { foo: "1,2,3" }, { foo: [ 1, 2, 3 ] } );
86 assert.deepEqual( ret.foo, [ 1, 2, 3 ], "Properly extend a string to array." );
87  
88 ret = $.widget.extend( { foo: "1,2,3" }, { foo: { to: "object" } } );
89 assert.deepEqual( ret.foo, { to: "object" }, "Properly extend a string to object." );
90  
91 ret = $.widget.extend( { foo: "bar" }, { foo: null } );
92 assert.strictEqual( ret.foo, null, "Make sure a null value doesn't crash with deep extend, for #1908" );
93  
94 obj = { foo: null };
95 $.widget.extend( obj, { foo:"notnull" } );
96 assert.equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
97  
98 settings = $.widget.extend( {}, defaults, options1, options2 );
99 assert.deepEqual( settings, merged2, "Check if extended: settings must be extended" );
100 assert.deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
101 assert.deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
102 assert.deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
103  
104 input = {
105 key: [ 1, 2, 3 ]
106 };
107 output = $.widget.extend( {}, input );
108 assert.deepEqual( input, output, "don't clone arrays" );
109 input.key[ 0 ] = 10;
110 assert.deepEqual( input, output, "don't clone arrays" );
111 } );
112  
113 } );