Widget: Shift to use no globals

This commit is contained in:
Amanpreet Singh 2016-04-06 19:36:47 +05:30
parent 92f122d5b1
commit 10741ef7f8
4 changed files with 457 additions and 445 deletions

View File

@ -1,14 +1,15 @@
define( [
"qunit",
"jquery",
"ui/widget"
], function( $ ) {
], function( QUnit, $ ) {
module( "widget animation", ( function() {
QUnit.module( "widget animation", ( function() {
var show = $.fn.show,
fadeIn = $.fn.fadeIn,
slideDown = $.fn.slideDown;
return {
setup: function() {
beforeEach: function() {
$.widget( "ui.testWidget", {
_create: function() {
this.element.hide();
@ -19,7 +20,7 @@ module( "widget animation", ( function() {
} );
$.effects = { effect: { testEffect: $.noop } };
},
teardown: function() {
afterEach: function() {
delete $.ui.testWidget;
delete $.effects.effect.testEffect;
$.fn.show = show;
@ -29,34 +30,36 @@ module( "widget animation", ( function() {
};
}() ) );
asyncTest( "show: null", function() {
expect( 4 );
QUnit.test( "show: null", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget(),
hasRun = false;
$.fn.show = function() {
ok( true, "show called" );
equal( arguments.length, 0, "no args passed to show" );
assert.ok( true, "show called" );
assert.equal( arguments.length, 0, "no args passed to show" );
};
element
.delay( 50 )
.queue( function( next ) {
ok( !hasRun, "queue before show" );
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
ok( hasRun, "queue after show" );
start();
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
asyncTest( "show: true", function() {
expect( 4 );
QUnit.test( "show: true", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: true
@ -64,8 +67,8 @@ asyncTest( "show: true", function() {
hasRun = false;
$.fn.fadeIn = function( duration, easing, complete ) {
return this.queue( function( next ) {
strictEqual( duration, undefined, "duration" );
strictEqual( easing, undefined, "easing" );
assert.strictEqual( duration, undefined, "duration" );
assert.strictEqual( easing, undefined, "easing" );
complete();
next();
} );
@ -74,21 +77,22 @@ asyncTest( "show: true", function() {
element
.delay( 50 )
.queue( function( next ) {
ok( !hasRun, "queue before show" );
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
ok( hasRun, "queue after show" );
start();
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
asyncTest( "show: number", function() {
expect( 4 );
QUnit.test( "show: number", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: 123
@ -96,8 +100,8 @@ asyncTest( "show: number", function() {
hasRun = false;
$.fn.fadeIn = function( duration, easing, complete ) {
return this.queue( function( next ) {
strictEqual( duration, 123, "duration" );
strictEqual( easing, undefined, "easing" );
assert.strictEqual( duration, 123, "duration" );
assert.strictEqual( easing, undefined, "easing" );
complete();
next();
} );
@ -106,21 +110,22 @@ asyncTest( "show: number", function() {
element
.delay( 50 )
.queue( function( next ) {
ok( !hasRun, "queue before show" );
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
ok( hasRun, "queue after show" );
start();
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
asyncTest( "show: core animation", function() {
expect( 4 );
QUnit.test( "show: core animation", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: "slideDown"
@ -128,8 +133,8 @@ asyncTest( "show: core animation", function() {
hasRun = false;
$.fn.slideDown = function( duration, easing, complete ) {
return this.queue( function( next ) {
strictEqual( duration, undefined, "duration" );
strictEqual( easing, undefined, "easing" );
assert.strictEqual( duration, undefined, "duration" );
assert.strictEqual( easing, undefined, "easing" );
complete();
next();
} );
@ -138,21 +143,22 @@ asyncTest( "show: core animation", function() {
element
.delay( 50 )
.queue( function( next ) {
ok( !hasRun, "queue before show" );
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
ok( hasRun, "queue after show" );
start();
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
asyncTest( "show: effect", function() {
expect( 5 );
QUnit.test( "show: effect", function( assert ) {
var ready = assert.async();
assert.expect( 5 );
var element = $( "#widget" ).testWidget( {
show: "testEffect"
@ -160,9 +166,9 @@ asyncTest( "show: effect", function() {
hasRun = false;
$.fn.show = function( options ) {
return this.queue( function( next ) {
equal( options.effect, "testEffect", "effect" );
ok( !( "duration" in options ), "duration" );
ok( !( "easing" in options ), "easing" );
assert.equal( options.effect, "testEffect", "effect" );
assert.ok( !( "duration" in options ), "duration" );
assert.ok( !( "easing" in options ), "easing" );
options.complete();
next();
} );
@ -171,21 +177,22 @@ asyncTest( "show: effect", function() {
element
.delay( 50 )
.queue( function( next ) {
ok( !hasRun, "queue before show" );
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
ok( hasRun, "queue after show" );
start();
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
asyncTest( "show: object(core animation)", function() {
expect( 4 );
QUnit.test( "show: object(core animation)", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: {
@ -197,8 +204,8 @@ asyncTest( "show: object(core animation)", function() {
hasRun = false;
$.fn.slideDown = function( duration, easing, complete ) {
return this.queue( function( next ) {
equal( duration, 123, "duration" );
equal( easing, "testEasing", "easing" );
assert.equal( duration, 123, "duration" );
assert.equal( easing, "testEasing", "easing" );
complete();
next();
} );
@ -207,21 +214,22 @@ asyncTest( "show: object(core animation)", function() {
element
.delay( 50 )
.queue( function( next ) {
ok( !hasRun, "queue before show" );
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
ok( hasRun, "queue after show" );
start();
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
asyncTest( "show: object(effect)", function() {
expect( 3 );
QUnit.test( "show: object(effect)", function( assert ) {
var ready = assert.async();
assert.expect( 3 );
var element = $( "#widget" ).testWidget( {
show: {
@ -233,7 +241,7 @@ asyncTest( "show: object(effect)", function() {
hasRun = false;
$.fn.show = function( options ) {
return this.queue( function( next ) {
deepEqual( options, {
assert.deepEqual( options, {
effect: "testEffect",
duration: 123,
easing: "testEasing",
@ -247,15 +255,15 @@ asyncTest( "show: object(effect)", function() {
element
.delay( 50 )
.queue( function( next ) {
ok( !hasRun, "queue before show" );
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
ok( hasRun, "queue after show" );
start();
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );

View File

@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"ui/widget"
], function( $ ) {
], function( QUnit, $ ) {
module( "widget factory classes", {
setup: function() {
QUnit.module( "widget factory classes", {
beforeEach: function() {
$.widget( "ui.classesWidget", {
options: {
classes: {
@ -50,7 +51,7 @@ module( "widget factory classes", {
}
} );
},
teardown: function() {
afterEach: function() {
delete $.ui.classesWidget;
delete $.fn.classesWidget;
}
@ -89,8 +90,8 @@ function elementLacksClasses( widget, method, assert ) {
"_" + method + "Class works with ( element, keys, null" + toggle + " )" );
}
test( ".option() - classes setter", function( assert ) {
expect( 11 );
QUnit.test( ".option() - classes setter", function( assert ) {
assert.expect( 11 );
var testWidget = $.ui.classesWidget();
@ -118,16 +119,16 @@ test( ".option() - classes setter", function( assert ) {
"Appending a class to the current value works as expected" );
} );
test( ".destroy() - class removal", function( assert ) {
expect( 1 );
QUnit.test( ".destroy() - class removal", function( assert ) {
assert.expect( 1 );
assert.domEqual( "#widget", function() {
$( "#widget" ).classesWidget().classesWidget( "destroy" );
} );
} );
test( "._add/_remove/_toggleClass()", function( assert ) {
expect( 24 );
QUnit.test( "._add/_remove/_toggleClass()", function( assert ) {
assert.expect( 24 );
var widget = $( "#widget" ).classesWidget();

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"ui/widget"
], function( $ ) {
], function( QUnit, $ ) {
test( "$.widget.extend()", function() {
expect( 27 );
QUnit.test( "$.widget.extend()", function( assert ) {
assert.expect( 27 );
var ret, empty, optionsWithLength, optionsWithDate, myKlass, customObject, optionsWithCustomObject, nullUndef,
target, recursive, obj, input, output,
@ -27,86 +28,86 @@ test( "$.widget.extend()", function() {
merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
$.widget.extend( settings, options );
deepEqual( settings, merged, "Check if extended: settings must be extended" );
deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
assert.deepEqual( settings, merged, "Check if extended: settings must be extended" );
assert.deepEqual( options, optionsCopy, "Check if not modified: options must not be modified" );
$.widget.extend( deep1, deep2 );
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" );
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" );
strictEqual( $.widget.extend( {}, nestedarray ).arr, arr, "Don't clone arrays" );
ok( $.isPlainObject( $.widget.extend( { arr: arr }, { arr: {} } ).arr ), "Cloned object heve to be an plain object" );
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" );
empty = {};
optionsWithLength = { foo: { length: -1 } };
$.widget.extend( empty, optionsWithLength );
deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
assert.deepEqual( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
empty = {};
optionsWithDate = { foo: { date: new Date() } };
$.widget.extend( empty, optionsWithDate );
deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
assert.deepEqual( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
myKlass = function() {};
customObject = new myKlass();
optionsWithCustomObject = { foo: { date: customObject } };
empty = {};
$.widget.extend( empty, optionsWithCustomObject );
strictEqual( empty.foo.date, customObject, "Custom objects copy correctly (no methods)" );
assert.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 );
strictEqual( empty.foo.date, customObject, "Custom objects copy correctly" );
assert.strictEqual( empty.foo.date, customObject, "Custom objects copy correctly" );
ret = $.widget.extend( { foo: 4 }, { foo: Number( 5 ) } );
equal( ret.foo, 5, "Wrapped numbers copy correctly" );
assert.equal( ret.foo, 5, "Wrapped numbers copy correctly" );
nullUndef = $.widget.extend( {}, options, { xnumber2: null } );
strictEqual( nullUndef.xnumber2, null, "Check to make sure null values are copied" );
assert.strictEqual( nullUndef.xnumber2, null, "Check to make sure null values are copied" );
nullUndef = $.widget.extend( {}, options, { xnumber2: undefined } );
strictEqual( nullUndef.xnumber2, options.xnumber2, "Check to make sure undefined values are not copied" );
assert.strictEqual( nullUndef.xnumber2, options.xnumber2, "Check to make sure undefined values are not copied" );
nullUndef = $.widget.extend( {}, options, { xnumber0: null } );
strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted" );
assert.strictEqual( nullUndef.xnumber0, null, "Check to make sure null values are inserted" );
target = {};
recursive = { foo:target, bar:5 };
$.widget.extend( target, recursive );
deepEqual( target, { foo: {}, bar: 5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
assert.deepEqual( target, { foo: {}, bar: 5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
ret = $.widget.extend( { 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" );
assert.equal( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
ret = $.widget.extend( { foo: "1,2,3" }, { foo: [ 1, 2, 3 ] } );
deepEqual( ret.foo, [ 1, 2, 3 ], "Properly extend a string to array." );
assert.deepEqual( ret.foo, [ 1, 2, 3 ], "Properly extend a string to array." );
ret = $.widget.extend( { foo: "1,2,3" }, { foo: { to: "object" } } );
deepEqual( ret.foo, { to: "object" }, "Properly extend a string to object." );
assert.deepEqual( ret.foo, { to: "object" }, "Properly extend a string to object." );
ret = $.widget.extend( { foo: "bar" }, { foo: null } );
strictEqual( ret.foo, null, "Make sure a null value doesn't crash with deep extend, for #1908" );
assert.strictEqual( ret.foo, null, "Make sure a null value doesn't crash with deep extend, for #1908" );
obj = { foo: null };
$.widget.extend( obj, { foo:"notnull" } );
equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
assert.equal( obj.foo, "notnull", "Make sure a null value can be overwritten" );
settings = $.widget.extend( {}, defaults, options1, options2 );
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" );
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" );
input = {
key: [ 1, 2, 3 ]
};
output = $.widget.extend( {}, input );
deepEqual( input, output, "don't clone arrays" );
assert.deepEqual( input, output, "don't clone arrays" );
input.key[ 0 ] = 10;
deepEqual( input, output, "don't clone arrays" );
assert.deepEqual( input, output, "don't clone arrays" );
} );
} );