Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Widget: Clean up extend tests
  • Loading branch information
jzaefferer committed May 7, 2011
commit fc5fab227554412186310c5b5919bae4140f1162
64 changes: 31 additions & 33 deletions tests/unit/widget/widget_extend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test("$.widget.extend(Object, Object)", function() {
expect(28);
expect(26);

var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
Expand All @@ -11,83 +11,81 @@ test("$.widget.extend(Object, Object)", function() {
deep2copy = { foo: { baz: true }, foo2: document },
deepmerged = { foo: { bar: true, baz: true }, foo2: document },
arr = [1, 2, 3],
nestedarray = { arr: arr };
nestedarray = { arr: arr },
ret;

$.widget.extend(settings, options);
same( settings, merged, "Check if extended: settings must be extended" );
same( options, optionsCopy, "Check if not modified: options must not be modified" );
$.widget.extend(settings, null, options);
same( settings, merged, "Check if extended: settings must be extended" );
same( options, optionsCopy, "Check if not modified: options must not be modified" );
deepEqual( settings, merged, "Check if extended: settings must be extended" );
deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );

$.widget.extend(deep1, deep2);
same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
deepEqual( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
deepEqual( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
equal( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );

ok( $.widget.extend({}, nestedarray).arr === arr, "Don't clone arrays" );
strictEqual( $.widget.extend({}, nestedarray).arr, arr, "Don't clone arrays" );
ok( jQuery.isPlainObject( $.widget.extend({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use $ instead of jQuery


var empty = {};
var optionsWithLength = { foo: { length: -1 } };
$.widget.extend(empty, optionsWithLength);
same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );

empty = {};
var optionsWithDate = { foo: { date: new Date } };
$.widget.extend(empty, optionsWithDate);
same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" );

var myKlass = function() {};
var customObject = new myKlass();
var optionsWithCustomObject = { foo: { date: customObject } };
empty = {};
$.widget.extend(empty, optionsWithCustomObject);
ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
strictEqual( empty.foo.date, customObject, "Custom objects copy correctly (no methods)" );

// Makes the class a little more realistic
myKlass.prototype = { someMethod: function(){} };
empty = {};
$.widget.extend(empty, optionsWithCustomObject);
ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
strictEqual( empty.foo.date, customObject, "Custom objects copy correctly" );

var ret = $.widget.extend({ foo: 4 }, { foo: new Number(5) } );
ok( ret.foo == 5, "Wrapped numbers copy correctly" );
ret = $.widget.extend({ foo: 4 }, { foo: new Number(5) } );
equal( ret.foo, 5, "Wrapped numbers copy correctly" );

var nullUndef;
nullUndef = $.widget.extend({}, options, { xnumber2: null });
ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
strictEqual( nullUndef.xnumber2, null, "Check to make sure null values are copied");

nullUndef = $.widget.extend({}, options, { xnumber2: undefined });
ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
strictEqual( nullUndef.xnumber2, options.xnumber2, "Check to make sure undefined values are not copied");

nullUndef = $.widget.extend({}, options, { xnumber0: null });
ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted");

// TODO weird test
/*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test ensures that we can't enter an infinite loop for recursive properties. It should be kept.

var target = {};
var recursive = { foo:target, bar:5 };
$.widget.extend(target, recursive);
same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
deepEqual( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
*/

var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );

var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
notStrictEqual( typeof ret.foo, "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );

var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
ok( typeof ret.foo !== "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" );
ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
notStrictEqual( typeof ret.foo, "undefined", "Make sure a null value doesn't crash with deep extend, for #1908" );

var obj = { foo:null };
jQuery.extend(true, obj, { foo:"notnull" } );
equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );

function func() {}
jQuery.extend(func, { key: "value" } );
equals( func.key, "value", "Verify a function can be extended" );
equal( func.key, "value", "Verify a function can be extended" );

var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
Expand All @@ -98,10 +96,10 @@ test("$.widget.extend(Object, Object)", function() {
merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };

var settings = jQuery.extend({}, defaults, options1, options2);
same( settings, merged2, "Check if extended: settings must be extended" );
same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
deepEqual( settings, merged2, "Check if extended: settings must be extended" );
deepEqual( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
deepEqual( options1, options1Copy, "Check if not modified: options1 must not be modified" );
deepEqual( options2, options2Copy, "Check if not modified: options2 must not be modified" );

var input = {
key: [ 1, 2, 3 ]
Expand Down