2015-04-06 19:07:04 +00:00
|
|
|
define( [
|
2016-04-06 14:06:47 +00:00
|
|
|
"qunit",
|
2015-04-06 19:07:04 +00:00
|
|
|
"jquery",
|
|
|
|
"ui/widget"
|
2016-04-06 14:06:47 +00:00
|
|
|
], function( QUnit, $ ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
2015-01-25 19:03:21 +00:00
|
|
|
|
2016-04-06 14:06:47 +00:00
|
|
|
QUnit.test( "$.widget.extend()", function( assert ) {
|
2017-04-21 18:49:52 +00:00
|
|
|
assert.expect( 28 );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
var ret, empty, optionsWithLength, optionsWithDate, myKlass, customObject, optionsWithCustomObject, nullUndef,
|
|
|
|
target, recursive, obj, input, output,
|
|
|
|
settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
|
2011-05-11 17:37:40 +00:00
|
|
|
options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
|
|
|
|
optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
|
|
|
|
merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
|
|
|
|
deep1 = { foo: { bar: true } },
|
|
|
|
deep2 = { foo: { baz: true }, foo2: document },
|
|
|
|
deep2copy = { foo: { baz: true }, foo2: document },
|
|
|
|
deepmerged = { foo: { bar: true, baz: true }, foo2: document },
|
2015-08-24 13:32:13 +00:00
|
|
|
arr = [ 1, 2, 3 ],
|
2012-04-19 15:21:24 +00:00
|
|
|
nestedarray = { arr: arr },
|
|
|
|
defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
|
|
|
|
defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
|
|
|
|
options1 = { xnumber2: 1, xstring2: "x" },
|
|
|
|
options1Copy = { xnumber2: 1, xstring2: "x" },
|
|
|
|
options2 = { xstring2: "xx", xxx: "newstringx" },
|
|
|
|
options2Copy = { xstring2: "xx", xxx: "newstringx" },
|
|
|
|
merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
|
2011-05-11 17:37:40 +00:00
|
|
|
|
|
|
|
$.widget.extend( settings, options );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
|
|
|
|
assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
|
|
|
$.widget.extend( deep1, deep2 );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
|
|
|
|
assert.deepEqual( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
|
|
|
|
assert.equal( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.strictEqual( $.widget.extend( {}, nestedarray ).arr, arr, "Don't clone arrays" );
|
|
|
|
assert.ok( $.isPlainObject( $.widget.extend( { arr: arr }, { arr: {} } ).arr ), "Cloned object heve to be an plain object" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
empty = {};
|
|
|
|
optionsWithLength = { foo: { length: -1 } };
|
2011-05-11 17:37:40 +00:00
|
|
|
$.widget.extend( empty, optionsWithLength );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
|
|
|
empty = {};
|
2012-04-19 02:36:15 +00:00
|
|
|
optionsWithDate = { foo: { date: new Date() } };
|
2011-05-11 17:37:40 +00:00
|
|
|
$.widget.extend( empty, optionsWithDate );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
myKlass = function() {};
|
|
|
|
customObject = new myKlass();
|
|
|
|
optionsWithCustomObject = { foo: { date: customObject } };
|
2011-05-11 17:37:40 +00:00
|
|
|
empty = {};
|
|
|
|
$.widget.extend( empty, optionsWithCustomObject );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.strictEqual( empty.foo.date, customObject, "Custom objects copy correctly (no methods)" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
|
|
|
// Makes the class a little more realistic
|
2015-08-24 13:32:13 +00:00
|
|
|
myKlass.prototype = { someMethod: function() {} };
|
2011-05-11 17:37:40 +00:00
|
|
|
empty = {};
|
|
|
|
$.widget.extend( empty, optionsWithCustomObject );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.strictEqual( empty.foo.date, customObject, "Custom objects copy correctly" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2015-08-24 13:32:13 +00:00
|
|
|
ret = $.widget.extend( { foo: 4 }, { foo: Number( 5 ) } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.equal( ret.foo, 5, "Wrapped numbers copy correctly" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
|
|
|
nullUndef = $.widget.extend( {}, options, { xnumber2: null } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.strictEqual( nullUndef.xnumber2, null, "Check to make sure null values are copied" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
|
|
|
nullUndef = $.widget.extend( {}, options, { xnumber2: undefined } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.strictEqual( nullUndef.xnumber2, options.xnumber2, "Check to make sure undefined values are not copied" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
|
|
|
nullUndef = $.widget.extend( {}, options, { xnumber0: null } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
target = {};
|
2021-06-06 22:58:12 +00:00
|
|
|
recursive = { foo: target, bar: 5 };
|
2011-05-11 17:37:40 +00:00
|
|
|
$.widget.extend( target, recursive );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( target, { foo: {}, bar: 5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2015-08-24 13:32:13 +00:00
|
|
|
ret = $.widget.extend( { foo: [] }, { foo: [ 0 ] } ); // 1907
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-10-22 19:33:57 +00:00
|
|
|
ret = $.widget.extend( { foo: "1,2,3" }, { foo: [ 1, 2, 3 ] } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( ret.foo, [ 1, 2, 3 ], "Properly extend a string to array." );
|
2012-10-22 19:33:57 +00:00
|
|
|
|
|
|
|
ret = $.widget.extend( { foo: "1,2,3" }, { foo: { to: "object" } } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( ret.foo, { to: "object" }, "Properly extend a string to object." );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-10-22 18:57:53 +00:00
|
|
|
ret = $.widget.extend( { foo: "bar" }, { foo: null } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.strictEqual( ret.foo, null, "Make sure a null value doesn't crash with deep extend, for #1908" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-10-22 19:33:57 +00:00
|
|
|
obj = { foo: null };
|
2021-06-06 22:58:12 +00:00
|
|
|
$.widget.extend( obj, { foo: "notnull" } );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
|
2011-05-11 17:37:40 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
settings = $.widget.extend( {}, defaults, options1, options2 );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( settings, merged2, "Check if extended: settings must be extended" );
|
|
|
|
assert.deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
|
|
|
|
assert.deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
|
|
|
|
assert.deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );
|
2011-09-12 21:23:54 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
input = {
|
2011-05-11 17:37:40 +00:00
|
|
|
key: [ 1, 2, 3 ]
|
2011-09-12 21:23:54 +00:00
|
|
|
};
|
2012-04-19 02:36:15 +00:00
|
|
|
output = $.widget.extend( {}, input );
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( input, output, "don't clone arrays" );
|
2015-08-24 13:32:13 +00:00
|
|
|
input.key[ 0 ] = 10;
|
2016-04-06 14:06:47 +00:00
|
|
|
assert.deepEqual( input, output, "don't clone arrays" );
|
2017-04-21 18:49:52 +00:00
|
|
|
|
|
|
|
input = Object.create( null );
|
|
|
|
input.foo = "f";
|
|
|
|
output = $.widget.extend( {}, input );
|
|
|
|
assert.deepEqual( input, output, "Object with no prototype" );
|
2015-08-24 13:32:13 +00:00
|
|
|
} );
|
2015-01-25 19:03:21 +00:00
|
|
|
|
2015-04-06 19:07:04 +00:00
|
|
|
} );
|