Debian Codegraph @

tests/unit/widget/extend.js

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