2010-08-26 14:59:51 +00:00
|
|
|
(function( $ ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
module( "widget factory", {
|
2010-01-07 03:19:50 +00:00
|
|
|
teardown: function() {
|
2012-04-30 00:22:31 +00:00
|
|
|
if ( $.ui ) {
|
|
|
|
delete $.ui.testWidget;
|
|
|
|
}
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-06-15 17:42:00 +00:00
|
|
|
TestHelpers.testJshint( "widget" );
|
2012-04-30 00:22:31 +00:00
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
test( "widget creation", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 5 );
|
2010-01-07 03:19:50 +00:00
|
|
|
var myPrototype = {
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {},
|
2010-01-07 03:19:50 +00:00
|
|
|
creationTest: function() {}
|
|
|
|
};
|
2010-08-26 14:59:51 +00:00
|
|
|
|
|
|
|
$.widget( "ui.testWidget", myPrototype );
|
|
|
|
ok( $.isFunction( $.ui.testWidget ), "constructor was created" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( "object", typeof $.ui.testWidget.prototype, "prototype was created" );
|
|
|
|
equal( $.ui.testWidget.prototype._create, myPrototype._create,
|
2010-08-26 14:59:51 +00:00
|
|
|
"create function is copied over" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( $.ui.testWidget.prototype.creationTest, myPrototype.creationTest,
|
2010-08-26 14:59:51 +00:00
|
|
|
"random function is copied over" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( $.ui.testWidget.prototype.option, $.Widget.prototype.option,
|
2010-08-26 14:59:51 +00:00
|
|
|
"option method copied over from base widget" );
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
|
|
|
|
2011-01-24 00:58:31 +00:00
|
|
|
test( "element normalization", function() {
|
2011-01-31 15:37:50 +00:00
|
|
|
expect( 11 );
|
2011-01-24 00:58:31 +00:00
|
|
|
var elem;
|
|
|
|
$.widget( "ui.testWidget", {} );
|
|
|
|
|
|
|
|
$.ui.testWidget.prototype._create = function() {
|
2011-02-26 12:31:45 +00:00
|
|
|
// workaround for core ticket #8381
|
|
|
|
this.element.appendTo( "#qunit-fixture" );
|
2011-01-24 00:58:31 +00:00
|
|
|
ok( this.element.is( "div" ), "generated div" );
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( this.element.data( "ui-testWidget" ), this, "instance stored in .data()" );
|
2011-01-24 00:58:31 +00:00
|
|
|
};
|
|
|
|
$.ui.testWidget();
|
|
|
|
|
2011-02-11 02:25:50 +00:00
|
|
|
$.ui.testWidget.prototype.defaultElement = "<span data-test='pass'></span>";
|
2011-01-24 00:58:31 +00:00
|
|
|
$.ui.testWidget.prototype._create = function() {
|
|
|
|
ok( this.element.is( "span[data-test=pass]" ), "generated span with properties" );
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( this.element.data( "ui-testWidget" ), this, "instace stored in .data()" );
|
2011-01-24 00:58:31 +00:00
|
|
|
};
|
|
|
|
$.ui.testWidget();
|
|
|
|
|
|
|
|
elem = $( "<input>" );
|
|
|
|
$.ui.testWidget.prototype._create = function() {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this.element[ 0 ], elem[ 0 ], "from element" );
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" );
|
2011-01-24 00:58:31 +00:00
|
|
|
};
|
|
|
|
$.ui.testWidget( {}, elem[ 0 ] );
|
|
|
|
|
|
|
|
elem = $( "<div>" );
|
|
|
|
$.ui.testWidget.prototype._create = function() {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this.element[ 0 ], elem[ 0 ], "from jQuery object" );
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" );
|
2011-01-24 00:58:31 +00:00
|
|
|
};
|
|
|
|
$.ui.testWidget( {}, elem );
|
|
|
|
|
|
|
|
elem = $( "<div id='element-normalization-selector'></div>" )
|
2011-02-23 11:46:13 +00:00
|
|
|
.appendTo( "#qunit-fixture" );
|
2011-01-24 00:58:31 +00:00
|
|
|
$.ui.testWidget.prototype._create = function() {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this.element[ 0 ], elem[ 0 ], "from selector" );
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( elem.data( "ui-testWidget" ), this, "instace stored in .data()" );
|
2011-01-24 00:58:31 +00:00
|
|
|
};
|
|
|
|
$.ui.testWidget( {}, "#element-normalization-selector" );
|
2011-01-24 18:33:24 +00:00
|
|
|
|
|
|
|
$.ui.testWidget.prototype.defaultElement = null;
|
|
|
|
$.ui.testWidget.prototype._create = function() {
|
|
|
|
// using strictEqual throws an error (Maximum call stack size exceeded)
|
|
|
|
ok( this.element[ 0 ] === this, "instance as element" );
|
|
|
|
};
|
|
|
|
$.ui.testWidget();
|
2011-01-24 00:58:31 +00:00
|
|
|
});
|
|
|
|
|
2012-03-01 13:30:02 +00:00
|
|
|
test( "custom selector expression", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 1 );
|
2012-03-01 13:30:02 +00:00
|
|
|
var elem = $( "<div>" ).appendTo( "#qunit-fixture" );
|
|
|
|
$.widget( "ui.testWidget", {} );
|
|
|
|
elem.testWidget();
|
2012-07-09 20:02:43 +00:00
|
|
|
deepEqual( $( ":ui-testwidget" )[0], elem[0] );
|
2012-03-01 13:30:02 +00:00
|
|
|
elem.testWidget( "destroy" );
|
|
|
|
});
|
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
test( "jQuery usage", function() {
|
2012-03-01 14:03:30 +00:00
|
|
|
expect( 16 );
|
2010-08-26 14:59:51 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
var elem, instance, ret, bcInstance,
|
|
|
|
shouldCreate = false;
|
2010-08-26 14:59:51 +00:00
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {
|
2010-01-07 03:19:50 +00:00
|
|
|
getterSetterVal: 5,
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-09-30 00:36:45 +00:00
|
|
|
ok( shouldCreate, "create called on instantiation" );
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
2010-08-26 14:59:51 +00:00
|
|
|
methodWithParams: function( param1, param2 ) {
|
|
|
|
ok( true, "method called via .pluginName(methodName)" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( param1, "value1",
|
2010-08-26 14:59:51 +00:00
|
|
|
"parameter passed via .pluginName(methodName, param)" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( param2, "value2",
|
2010-08-26 14:59:51 +00:00
|
|
|
"multiple parameters passed via .pluginName(methodName, param, param)" );
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
return this;
|
|
|
|
},
|
2010-08-26 14:59:51 +00:00
|
|
|
getterSetterMethod: function( val ) {
|
|
|
|
if ( val ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
this.getterSetterVal = val;
|
|
|
|
} else {
|
|
|
|
return this.getterSetterVal;
|
|
|
|
}
|
2011-03-22 18:35:18 +00:00
|
|
|
},
|
|
|
|
jQueryObject: function() {
|
|
|
|
return $( "body" );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
});
|
2010-08-26 14:59:51 +00:00
|
|
|
|
2010-09-30 00:36:45 +00:00
|
|
|
shouldCreate = true;
|
2012-04-19 02:36:15 +00:00
|
|
|
elem = $( "<div>" )
|
2010-09-30 00:36:45 +00:00
|
|
|
.bind( "testwidgetcreate", function() {
|
|
|
|
ok( shouldCreate, "create event triggered on instantiation" );
|
|
|
|
})
|
|
|
|
.testWidget();
|
|
|
|
shouldCreate = false;
|
2010-08-26 14:59:51 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
instance = elem.data( "ui-testWidget" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( typeof instance, "object", "instance stored in .data(pluginName)" );
|
|
|
|
equal( instance.element[0], elem[0], "element stored on widget" );
|
2012-04-19 02:36:15 +00:00
|
|
|
ret = elem.testWidget( "methodWithParams", "value1", "value2" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( ret, elem, "jQuery object returned from method call" );
|
2010-08-26 14:59:51 +00:00
|
|
|
|
2012-03-01 13:30:02 +00:00
|
|
|
// 1.9 BC for #7810
|
|
|
|
// TODO remove
|
2012-04-19 02:36:15 +00:00
|
|
|
bcInstance = elem.data("testWidget");
|
2012-03-01 13:30:02 +00:00
|
|
|
equal( typeof bcInstance, "object", "instance stored in .data(pluginName)" );
|
|
|
|
equal( bcInstance.element[0], elem[0], "element stored on widget" );
|
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
ret = elem.testWidget( "getterSetterMethod" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( ret, 5, "getter/setter can act as getter" );
|
2010-08-26 14:59:51 +00:00
|
|
|
ret = elem.testWidget( "getterSetterMethod", 30 );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( ret, elem, "getter/setter method can be chainable" );
|
|
|
|
equal( instance.getterSetterVal, 30, "getter/setter can act as setter" );
|
2011-03-22 18:35:18 +00:00
|
|
|
ret = elem.testWidget( "jQueryObject" );
|
|
|
|
equal( ret[ 0 ], document.body, "returned jQuery object" );
|
|
|
|
equal( ret.end(), elem, "stack preserved" );
|
2012-03-01 14:03:30 +00:00
|
|
|
|
|
|
|
elem.testWidget( "destroy" );
|
|
|
|
equal( elem.data( "ui-testWidget" ), null );
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
test( "direct usage", function() {
|
|
|
|
expect( 9 );
|
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
var elem, instance, ret,
|
|
|
|
shouldCreate = false;
|
2010-08-26 14:59:51 +00:00
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {
|
2010-01-07 03:19:50 +00:00
|
|
|
getterSetterVal: 5,
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-09-30 00:36:45 +00:00
|
|
|
ok( shouldCreate, "create called on instantiation" );
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
2010-08-26 14:59:51 +00:00
|
|
|
methodWithParams: function( param1, param2 ) {
|
|
|
|
ok( true, "method called dirctly" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( param1, "value1", "parameter passed via direct call" );
|
|
|
|
equal( param2, "value2", "multiple parameters passed via direct call" );
|
2010-08-26 14:59:51 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
return this;
|
|
|
|
},
|
2010-08-26 14:59:51 +00:00
|
|
|
getterSetterMethod: function( val ) {
|
|
|
|
if ( val ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
this.getterSetterVal = val;
|
|
|
|
} else {
|
|
|
|
return this.getterSetterVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
elem = $( "<div>" )[ 0 ];
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2010-09-30 00:36:45 +00:00
|
|
|
shouldCreate = true;
|
2012-04-19 02:36:15 +00:00
|
|
|
instance = new $.ui.testWidget( {}, elem );
|
2010-09-30 00:36:45 +00:00
|
|
|
shouldCreate = false;
|
2010-08-26 14:59:51 +00:00
|
|
|
|
2012-03-01 13:30:02 +00:00
|
|
|
equal( $( elem ).data( "ui-testWidget" ), instance,
|
2010-08-26 14:59:51 +00:00
|
|
|
"instance stored in .data(pluginName)" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( instance.element[ 0 ], elem, "element stored on widget" );
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
ret = instance.methodWithParams( "value1", "value2" );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( ret, instance, "plugin returned from method call" );
|
2010-08-26 14:59:51 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
ret = instance.getterSetterMethod();
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( ret, 5, "getter/setter can act as getter" );
|
2010-08-26 14:59:51 +00:00
|
|
|
instance.getterSetterMethod( 30 );
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( instance.getterSetterVal, 30, "getter/setter can act as setter" );
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
|
|
|
|
2010-12-10 19:11:20 +00:00
|
|
|
test( "error handling", function() {
|
2011-02-16 08:28:32 +00:00
|
|
|
expect( 3 );
|
2010-12-10 19:11:20 +00:00
|
|
|
var error = $.error;
|
2011-02-16 08:28:32 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_privateMethod: function () {}
|
|
|
|
});
|
2010-12-10 19:11:20 +00:00
|
|
|
$.error = function( msg ) {
|
|
|
|
equal( msg, "cannot call methods on testWidget prior to initialization; " +
|
|
|
|
"attempted to call method 'missing'", "method call before init" );
|
|
|
|
};
|
|
|
|
$( "<div>" ).testWidget( "missing" );
|
|
|
|
$.error = function( msg ) {
|
|
|
|
equal( msg, "no such method 'missing' for testWidget widget instance",
|
|
|
|
"invalid method call on widget instance" );
|
|
|
|
};
|
|
|
|
$( "<div>" ).testWidget().testWidget( "missing" );
|
2011-02-16 08:28:32 +00:00
|
|
|
$.error = function ( msg ) {
|
|
|
|
equal( msg, "no such method '_privateMethod' for testWidget widget instance",
|
|
|
|
"invalid method call on widget instance" );
|
|
|
|
};
|
2011-06-19 12:42:15 +00:00
|
|
|
$( "<div>" ).testWidget().testWidget( "_privateMethod" );
|
2010-12-10 19:11:20 +00:00
|
|
|
$.error = error;
|
|
|
|
});
|
|
|
|
|
2011-02-01 02:55:22 +00:00
|
|
|
test( "merge multiple option arguments", function() {
|
2010-08-26 14:59:51 +00:00
|
|
|
expect( 1 );
|
|
|
|
$.widget( "ui.testWidget", {
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this.options, {
|
2011-04-25 18:04:30 +00:00
|
|
|
create: null,
|
2010-01-07 03:19:50 +00:00
|
|
|
disabled: false,
|
|
|
|
option1: "value1",
|
|
|
|
option2: "value2",
|
|
|
|
option3: "value3",
|
|
|
|
option4: {
|
|
|
|
option4a: "valuea",
|
|
|
|
option4b: "valueb"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2011-01-25 02:22:40 +00:00
|
|
|
$( "<div>" ).testWidget({
|
2010-01-07 03:19:50 +00:00
|
|
|
option1: "valuex",
|
|
|
|
option2: "valuex",
|
|
|
|
option3: "value3",
|
|
|
|
option4: {
|
|
|
|
option4a: "valuex"
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
option1: "value1",
|
|
|
|
option2: "value2",
|
|
|
|
option4: {
|
|
|
|
option4b: "valueb"
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
option4: {
|
|
|
|
option4a: "valuea"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-02-01 02:55:22 +00:00
|
|
|
test( "._getCreateOptions()", function() {
|
2010-10-06 19:11:49 +00:00
|
|
|
expect( 1 );
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
options: {
|
|
|
|
option1: "valuex",
|
|
|
|
option2: "valuex",
|
2011-02-11 02:25:50 +00:00
|
|
|
option3: "value3"
|
2010-10-06 19:11:49 +00:00
|
|
|
},
|
|
|
|
_getCreateOptions: function() {
|
|
|
|
return {
|
|
|
|
option1: "override1",
|
2011-02-11 02:25:50 +00:00
|
|
|
option2: "overideX"
|
2010-10-06 19:11:49 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
_create: function() {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this.options, {
|
2011-04-25 18:04:30 +00:00
|
|
|
create: null,
|
2010-10-06 19:11:49 +00:00
|
|
|
disabled: false,
|
|
|
|
option1: "override1",
|
|
|
|
option2: "value2",
|
|
|
|
option3: "value3"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$( "<div>" ).testWidget({ option2: "value2" });
|
|
|
|
});
|
|
|
|
|
2012-01-21 13:45:41 +00:00
|
|
|
test( "._getCreateEventData()", function() {
|
|
|
|
expect( 1 );
|
|
|
|
var data = { foo: "bar" };
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_getCreateEventData: function() {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$( "<div>" ).testWidget({
|
|
|
|
create: function( event, ui ) {
|
|
|
|
strictEqual( ui, data, "event data" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
test( "re-init", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 3 );
|
2011-01-25 02:22:40 +00:00
|
|
|
var div = $( "<div>" ),
|
2010-08-23 19:35:28 +00:00
|
|
|
actions = [];
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {
|
|
|
|
actions.push( "create" );
|
|
|
|
},
|
|
|
|
_init: function() {
|
|
|
|
actions.push( "init" );
|
|
|
|
},
|
2012-10-23 02:37:42 +00:00
|
|
|
_setOption: function( key ) {
|
2010-08-23 19:35:28 +00:00
|
|
|
actions.push( "option" + key );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
actions = [];
|
|
|
|
div.testWidget({ foo: "bar" });
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( actions, [ "create", "init" ], "correct methods called on init" );
|
2010-08-23 19:35:28 +00:00
|
|
|
|
|
|
|
actions = [];
|
|
|
|
div.testWidget();
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( actions, [ "init" ], "correct methods call on re-init" );
|
2010-08-23 19:35:28 +00:00
|
|
|
|
|
|
|
actions = [];
|
|
|
|
div.testWidget({ foo: "bar" });
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( actions, [ "optionfoo", "init" ], "correct methods called on re-init with options" );
|
2010-08-23 19:35:28 +00:00
|
|
|
});
|
|
|
|
|
2011-03-28 16:37:08 +00:00
|
|
|
test( "inheritance - options", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 4 );
|
2011-03-28 16:37:08 +00:00
|
|
|
// #5830 - Widget: Using inheritance overwrites the base classes options
|
|
|
|
$.widget( "ui.testWidgetBase", {
|
|
|
|
options: {
|
|
|
|
obj: {
|
|
|
|
key1: "foo",
|
|
|
|
key2: "bar"
|
|
|
|
},
|
|
|
|
arr: [ "testing" ]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.widget( "ui.testWidgetExtension", $.ui.testWidgetBase, {
|
|
|
|
options: {
|
|
|
|
obj: {
|
|
|
|
key1: "baz"
|
|
|
|
},
|
|
|
|
arr: [ "alpha", "beta" ]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( $.ui.testWidgetBase.prototype.options.obj, {
|
2011-03-28 16:37:08 +00:00
|
|
|
key1: "foo",
|
|
|
|
key2: "bar"
|
|
|
|
}, "base class option object not overridden");
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( $.ui.testWidgetBase.prototype.options.arr, [ "testing" ],
|
2011-03-28 16:37:08 +00:00
|
|
|
"base class option array not overridden");
|
|
|
|
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( $.ui.testWidgetExtension.prototype.options.obj, {
|
2011-03-28 16:37:08 +00:00
|
|
|
key1: "baz",
|
|
|
|
key2: "bar"
|
|
|
|
}, "extension class option object extends base");
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( $.ui.testWidgetExtension.prototype.options.arr, [ "alpha", "beta" ],
|
2011-03-28 16:37:08 +00:00
|
|
|
"extension class option array overwrites base");
|
|
|
|
|
|
|
|
delete $.ui.testWidgetBase;
|
|
|
|
delete $.ui.testWidgetExtension;
|
|
|
|
});
|
|
|
|
|
2011-01-14 20:52:03 +00:00
|
|
|
test( "._super()", function() {
|
2011-02-11 02:25:50 +00:00
|
|
|
expect( 9 );
|
2011-01-14 20:52:03 +00:00
|
|
|
var instance;
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
method: function( a, b ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this, instance, "this is correct in testWidget" );
|
|
|
|
deepEqual( a, 5, "parameter passed to testWidget" );
|
|
|
|
deepEqual( b, 20, "second parameter passed to testWidget" );
|
2011-01-14 20:52:03 +00:00
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget2", $.ui.testWidget, {
|
2011-02-11 02:25:50 +00:00
|
|
|
method: function( a, b ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this, instance, "this is correct in testWidget2" );
|
|
|
|
deepEqual( a, 5, "parameter passed to testWidget2" );
|
|
|
|
deepEqual( b, 10, "parameter passed to testWidget2" );
|
2011-11-18 11:19:32 +00:00
|
|
|
return this._super( a, b*2 );
|
2011-02-11 02:25:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget3", $.ui.testWidget2, {
|
2011-01-14 20:52:03 +00:00
|
|
|
method: function( a ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this, instance, "this is correct in testWidget3" );
|
|
|
|
deepEqual( a, 5, "parameter passed to testWidget3" );
|
2011-11-18 11:19:32 +00:00
|
|
|
var ret = this._super( a, a*2 );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ret, 25, "super returned value" );
|
2011-01-14 20:52:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-03-01 13:30:02 +00:00
|
|
|
instance = $( "<div>" ).testWidget3().data( "ui-testWidget3" );
|
2011-01-14 20:52:03 +00:00
|
|
|
instance.method( 5 );
|
2011-02-11 02:25:50 +00:00
|
|
|
delete $.ui.testWidget3;
|
2011-01-14 20:52:03 +00:00
|
|
|
delete $.ui.testWidget2;
|
|
|
|
});
|
|
|
|
|
|
|
|
test( "._superApply()", function() {
|
2011-02-11 02:25:50 +00:00
|
|
|
expect( 10 );
|
2011-01-14 20:52:03 +00:00
|
|
|
var instance;
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
method: function( a, b ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this, instance, "this is correct in testWidget" );
|
|
|
|
deepEqual( a, 5, "parameter passed to testWidget" );
|
|
|
|
deepEqual( b, 10, "second parameter passed to testWidget" );
|
2011-01-14 20:52:03 +00:00
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget2", $.ui.testWidget, {
|
|
|
|
method: function( a, b ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this, instance, "this is correct in testWidget2" );
|
|
|
|
deepEqual( a, 5, "parameter passed to testWidget2" );
|
|
|
|
deepEqual( b, 10, "second parameter passed to testWidget2" );
|
2011-11-18 11:19:32 +00:00
|
|
|
return this._superApply( arguments );
|
2011-02-11 02:25:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget3", $.ui.testWidget2, {
|
|
|
|
method: function( a, b ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( this, instance, "this is correct in testWidget3" );
|
|
|
|
deepEqual( a, 5, "parameter passed to testWidget3" );
|
|
|
|
deepEqual( b, 10, "second parameter passed to testWidget3" );
|
2011-11-18 11:19:32 +00:00
|
|
|
var ret = this._superApply( arguments );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ret, 15, "super returned value" );
|
2011-01-14 20:52:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-03-01 13:30:02 +00:00
|
|
|
instance = $( "<div>" ).testWidget3().data( "ui-testWidget3" );
|
2011-01-14 20:52:03 +00:00
|
|
|
instance.method( 5, 10 );
|
2011-02-11 02:25:50 +00:00
|
|
|
delete $.ui.testWidget3;
|
2011-01-14 20:52:03 +00:00
|
|
|
delete $.ui.testWidget2;
|
|
|
|
});
|
|
|
|
|
2010-08-26 14:50:26 +00:00
|
|
|
test( ".option() - getter", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 6 );
|
2010-08-26 14:50:26 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {}
|
|
|
|
});
|
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
var options,
|
|
|
|
div = $( "<div>" ).testWidget({
|
|
|
|
foo: "bar",
|
|
|
|
baz: 5,
|
|
|
|
qux: [ "quux", "quuux" ]
|
|
|
|
});
|
2010-08-26 14:50:26 +00:00
|
|
|
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( div.testWidget( "option", "x" ), null, "non-existent option" );
|
|
|
|
deepEqual( div.testWidget( "option", "foo"), "bar", "single option - string" );
|
|
|
|
deepEqual( div.testWidget( "option", "baz"), 5, "single option - number" );
|
|
|
|
deepEqual( div.testWidget( "option", "qux"), [ "quux", "quuux" ],
|
2010-08-26 14:50:26 +00:00
|
|
|
"single option - array" );
|
|
|
|
|
2012-04-19 02:36:15 +00:00
|
|
|
options = div.testWidget( "option" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( options, {
|
2011-04-25 18:04:30 +00:00
|
|
|
create: null,
|
2010-08-26 14:50:26 +00:00
|
|
|
disabled: false,
|
|
|
|
foo: "bar",
|
|
|
|
baz: 5,
|
|
|
|
qux: [ "quux", "quuux" ]
|
|
|
|
}, "full options hash returned" );
|
|
|
|
options.foo = "notbar";
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( div.testWidget( "option", "foo"), "bar",
|
2010-08-26 14:50:26 +00:00
|
|
|
"modifying returned options hash does not modify plugin instance" );
|
|
|
|
});
|
|
|
|
|
2011-06-09 13:47:18 +00:00
|
|
|
test( ".option() - deep option getter", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 5 );
|
2011-06-09 13:47:18 +00:00
|
|
|
$.widget( "ui.testWidget", {} );
|
|
|
|
var div = $( "<div>" ).testWidget({
|
|
|
|
foo: {
|
|
|
|
bar: "baz",
|
|
|
|
qux: {
|
|
|
|
quux: "xyzzy"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
equal( div.testWidget( "option", "foo.bar" ), "baz", "one level deep - string" );
|
|
|
|
deepEqual( div.testWidget( "option", "foo.qux" ), { quux: "xyzzy" },
|
|
|
|
"one level deep - object" );
|
|
|
|
equal( div.testWidget( "option", "foo.qux.quux" ), "xyzzy", "two levels deep - string" );
|
|
|
|
equal( div.testWidget( "option", "x.y" ), null, "top level non-existent" );
|
|
|
|
equal( div.testWidget( "option", "foo.x.y" ), null, "one level deep - non-existent" );
|
|
|
|
});
|
|
|
|
|
2010-09-27 15:21:09 +00:00
|
|
|
test( ".option() - delegate to ._setOptions()", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 2 );
|
2012-04-19 02:36:15 +00:00
|
|
|
var div,
|
|
|
|
calls = [];
|
2010-09-27 15:21:09 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {},
|
|
|
|
_setOptions: function( options ) {
|
|
|
|
calls.push( options );
|
|
|
|
}
|
|
|
|
});
|
2012-04-19 02:36:15 +00:00
|
|
|
div = $( "<div>" ).testWidget();
|
2010-09-27 15:21:09 +00:00
|
|
|
|
|
|
|
calls = [];
|
|
|
|
div.testWidget( "option", "foo", "bar" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( calls, [{ foo: "bar" }], "_setOptions called for single option" );
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2010-09-27 15:21:09 +00:00
|
|
|
calls = [];
|
|
|
|
div.testWidget( "option", {
|
|
|
|
bar: "qux",
|
|
|
|
quux: "quuux"
|
|
|
|
});
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( calls, [{ bar: "qux", quux: "quuux" }],
|
2010-09-27 15:21:09 +00:00
|
|
|
"_setOptions called with multiple options" );
|
|
|
|
});
|
|
|
|
|
|
|
|
test( ".option() - delegate to ._setOption()", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 2 );
|
2012-04-19 02:36:15 +00:00
|
|
|
var div,
|
|
|
|
calls = [];
|
2010-08-26 14:50:26 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {},
|
|
|
|
_setOption: function( key, val ) {
|
|
|
|
calls.push({
|
|
|
|
key: key,
|
|
|
|
val: val
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-04-19 02:36:15 +00:00
|
|
|
div = $( "<div>" ).testWidget();
|
2010-08-26 14:50:26 +00:00
|
|
|
|
|
|
|
calls = [];
|
|
|
|
div.testWidget( "option", "foo", "bar" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( calls, [{ key: "foo", val: "bar" }],
|
2010-08-26 14:50:26 +00:00
|
|
|
"_setOption called for single option" );
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2010-08-26 14:50:26 +00:00
|
|
|
calls = [];
|
|
|
|
div.testWidget( "option", {
|
|
|
|
bar: "qux",
|
|
|
|
quux: "quuux"
|
|
|
|
});
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( calls, [
|
2010-08-26 14:50:26 +00:00
|
|
|
{ key: "bar", val: "qux" },
|
|
|
|
{ key: "quux", val: "quuux" }
|
|
|
|
], "_setOption called with multiple options" );
|
|
|
|
});
|
|
|
|
|
2011-03-24 13:21:53 +00:00
|
|
|
test( ".option() - deep option setter", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 6 );
|
2011-03-24 13:21:53 +00:00
|
|
|
$.widget( "ui.testWidget", {} );
|
|
|
|
var div = $( "<div>" ).testWidget();
|
|
|
|
function deepOption( from, to, msg ) {
|
2012-03-01 13:30:02 +00:00
|
|
|
div.data( "ui-testWidget" ).options.foo = from;
|
2011-03-24 13:21:53 +00:00
|
|
|
$.ui.testWidget.prototype._setOption = function( key, value ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( key, "foo", msg + ": key" );
|
|
|
|
deepEqual( value, to, msg + ": value" );
|
2011-03-24 13:21:53 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
deepOption( { bar: "baz" }, { bar: "qux" }, "one deep" );
|
|
|
|
div.testWidget( "option", "foo.bar", "qux" );
|
|
|
|
|
|
|
|
deepOption( null, { bar: "baz" }, "null" );
|
|
|
|
div.testWidget( "option", "foo.bar", "baz" );
|
|
|
|
|
|
|
|
deepOption(
|
|
|
|
{ bar: "baz", qux: { quux: "quuux" } },
|
|
|
|
{ bar: "baz", qux: { quux: "quuux", newOpt: "newVal" } },
|
|
|
|
"add property" );
|
|
|
|
div.testWidget( "option", "foo.qux.newOpt", "newVal" );
|
|
|
|
});
|
|
|
|
|
2010-08-26 14:50:26 +00:00
|
|
|
test( ".enable()", function() {
|
|
|
|
expect( 2 );
|
2010-08-26 14:59:51 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
2010-08-26 14:50:26 +00:00
|
|
|
_create: function() {},
|
|
|
|
_setOption: function( key, val ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( key, "disabled", "_setOption called with disabled option" );
|
|
|
|
deepEqual( val, false, "disabled set to false" );
|
2010-08-26 14:50:26 +00:00
|
|
|
}
|
|
|
|
});
|
2011-01-25 02:22:40 +00:00
|
|
|
$( "<div>" ).testWidget().testWidget( "enable" );
|
2010-08-26 14:50:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test( ".disable()", function() {
|
|
|
|
expect( 2 );
|
2010-08-26 14:59:51 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
2010-08-26 14:50:26 +00:00
|
|
|
_create: function() {},
|
|
|
|
_setOption: function( key, val ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( key, "disabled", "_setOption called with disabled option" );
|
|
|
|
deepEqual( val, true, "disabled set to true" );
|
2010-08-26 14:50:26 +00:00
|
|
|
}
|
|
|
|
});
|
2011-01-25 02:22:40 +00:00
|
|
|
$( "<div>" ).testWidget().testWidget( "disable" );
|
2010-08-26 14:50:26 +00:00
|
|
|
});
|
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
test( ".widget() - base", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 1 );
|
2010-08-26 14:59:51 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {}
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
2011-01-25 02:22:40 +00:00
|
|
|
var div = $( "<div>" ).testWidget();
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( div[0], div.testWidget( "widget" )[0]);
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
|
|
|
|
2010-08-26 14:59:51 +00:00
|
|
|
test( ".widget() - overriden", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 1 );
|
2011-01-25 02:22:40 +00:00
|
|
|
var wrapper = $( "<div>" );
|
2010-08-26 14:59:51 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {},
|
2010-01-07 03:19:50 +00:00
|
|
|
widget: function() {
|
2010-07-30 16:38:18 +00:00
|
|
|
return wrapper;
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
});
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( wrapper[0], $( "<div>" ).testWidget().testWidget( "widget" )[0] );
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
|
|
|
|
2012-06-13 12:00:45 +00:00
|
|
|
test( "._on() to element (default)", function() {
|
2011-01-18 06:46:26 +00:00
|
|
|
expect( 12 );
|
2012-04-19 02:36:15 +00:00
|
|
|
var that, widget;
|
2011-01-18 06:46:26 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {
|
2011-09-12 21:23:54 +00:00
|
|
|
that = this;
|
2012-06-13 12:00:45 +00:00
|
|
|
this._on({
|
2011-01-18 06:46:26 +00:00
|
|
|
keyup: this.keyup,
|
2011-01-18 06:53:20 +00:00
|
|
|
keydown: "keydown"
|
2011-01-18 06:46:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
keyup: function( event ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( that, this );
|
|
|
|
equal( that.element[0], event.currentTarget );
|
|
|
|
equal( "keyup", event.type );
|
2011-01-18 06:46:26 +00:00
|
|
|
},
|
|
|
|
keydown: function( event ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( that, this );
|
|
|
|
equal( that.element[0], event.currentTarget );
|
|
|
|
equal( "keydown", event.type );
|
2011-01-18 06:46:26 +00:00
|
|
|
}
|
|
|
|
});
|
2012-04-19 02:36:15 +00:00
|
|
|
widget = $( "<div></div>" )
|
2011-01-18 06:46:26 +00:00
|
|
|
.testWidget()
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
|
|
|
widget
|
|
|
|
.testWidget( "disable" )
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
|
|
|
widget
|
|
|
|
.testWidget( "enable" )
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
|
|
|
widget
|
|
|
|
.testWidget( "destroy" )
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
|
|
|
});
|
|
|
|
|
2012-06-13 12:00:45 +00:00
|
|
|
test( "._on() to descendent", function() {
|
2011-01-18 06:46:26 +00:00
|
|
|
expect( 12 );
|
2012-04-19 02:36:15 +00:00
|
|
|
var that, widget, descendant;
|
2011-01-18 06:46:26 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {
|
2011-09-12 21:23:54 +00:00
|
|
|
that = this;
|
2012-06-13 12:00:45 +00:00
|
|
|
this._on( this.element.find( "strong" ), {
|
2011-01-18 06:46:26 +00:00
|
|
|
keyup: this.keyup,
|
2011-01-18 06:53:20 +00:00
|
|
|
keydown: "keydown"
|
2011-01-18 06:46:26 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
keyup: function( event ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( that, this );
|
|
|
|
equal( that.element.find( "strong" )[0], event.currentTarget );
|
|
|
|
equal( "keyup", event.type );
|
2011-01-18 06:46:26 +00:00
|
|
|
},
|
|
|
|
keydown: function(event) {
|
2012-02-28 14:56:32 +00:00
|
|
|
equal( that, this );
|
|
|
|
equal( that.element.find( "strong" )[0], event.currentTarget );
|
|
|
|
equal( "keydown", event.type );
|
2011-01-18 06:46:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
// trigger events on both widget and descendent to ensure that only descendent receives them
|
2012-04-19 02:36:15 +00:00
|
|
|
widget = $( "<div><p><strong>hello</strong> world</p></div>" )
|
2011-01-18 06:46:26 +00:00
|
|
|
.testWidget()
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
2012-04-19 02:36:15 +00:00
|
|
|
descendant = widget.find( "strong" )
|
2011-01-18 06:46:26 +00:00
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
|
|
|
widget
|
|
|
|
.testWidget( "disable" )
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
2012-04-19 02:36:15 +00:00
|
|
|
descendant
|
2011-01-18 06:46:26 +00:00
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
|
|
|
widget
|
|
|
|
.testWidget( "enable" )
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
2012-04-19 02:36:15 +00:00
|
|
|
descendant
|
2011-01-18 06:46:26 +00:00
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
2012-04-19 02:36:15 +00:00
|
|
|
descendant
|
2011-02-01 02:55:22 +00:00
|
|
|
.addClass( "ui-state-disabled" )
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
2011-01-18 06:46:26 +00:00
|
|
|
widget
|
|
|
|
.testWidget( "destroy" )
|
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
2012-04-19 02:36:15 +00:00
|
|
|
descendant
|
2011-01-18 06:46:26 +00:00
|
|
|
.trigger( "keyup" )
|
|
|
|
.trigger( "keydown" );
|
|
|
|
});
|
|
|
|
|
2012-06-13 12:00:45 +00:00
|
|
|
test( "_on() with delegate", function() {
|
2011-06-19 12:59:18 +00:00
|
|
|
expect( 8 );
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {
|
2012-06-10 18:55:04 +00:00
|
|
|
var uuid = this.uuid;
|
2011-06-19 12:59:18 +00:00
|
|
|
this.element = {
|
|
|
|
bind: function( event, handler ) {
|
2012-06-10 18:55:04 +00:00
|
|
|
equal( event, "click.testWidget" + uuid );
|
2011-08-13 02:10:31 +00:00
|
|
|
ok( $.isFunction(handler) );
|
2011-06-19 12:59:18 +00:00
|
|
|
},
|
|
|
|
trigger: $.noop
|
2011-07-28 13:47:32 +00:00
|
|
|
};
|
2011-08-13 02:10:31 +00:00
|
|
|
this.widget = function() {
|
|
|
|
return {
|
|
|
|
delegate: function( selector, event, handler ) {
|
|
|
|
equal( selector, "a" );
|
2012-06-10 18:55:04 +00:00
|
|
|
equal( event, "click.testWidget" + uuid );
|
2011-08-13 02:10:31 +00:00
|
|
|
ok( $.isFunction(handler) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2012-06-13 12:00:45 +00:00
|
|
|
this._on({
|
2011-06-19 12:59:18 +00:00
|
|
|
"click": "handler",
|
2011-08-13 02:10:31 +00:00
|
|
|
"click a": "handler"
|
2011-06-19 12:59:18 +00:00
|
|
|
});
|
2011-08-13 02:10:31 +00:00
|
|
|
this.widget = function() {
|
|
|
|
return {
|
|
|
|
delegate: function( selector, event, handler ) {
|
|
|
|
equal( selector, "form fieldset > input" );
|
2012-06-10 18:55:04 +00:00
|
|
|
equal( event, "change.testWidget" + uuid );
|
2011-08-13 02:10:31 +00:00
|
|
|
ok( $.isFunction(handler) );
|
|
|
|
}
|
|
|
|
};
|
2011-06-19 12:59:18 +00:00
|
|
|
};
|
2012-06-13 12:00:45 +00:00
|
|
|
this._on({
|
2011-06-19 12:59:18 +00:00
|
|
|
"change form fieldset > input": "handler"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$.ui.testWidget();
|
2011-07-28 13:47:32 +00:00
|
|
|
});
|
2011-06-19 12:59:18 +00:00
|
|
|
|
2012-06-14 14:51:29 +00:00
|
|
|
test( "_on() to common element", function() {
|
2012-06-10 18:55:04 +00:00
|
|
|
expect( 1 );
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {
|
2012-06-14 14:51:29 +00:00
|
|
|
this._on( this.document, {
|
2012-06-10 18:55:04 +00:00
|
|
|
"customevent": "_handler"
|
|
|
|
});
|
|
|
|
},
|
|
|
|
_handler: function() {
|
|
|
|
ok( true, "handler triggered" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var widget = $( "#widget" ).testWidget().data( "testWidget" );
|
|
|
|
$( "#widget-wrapper" ).testWidget();
|
|
|
|
widget.destroy();
|
|
|
|
$( document ).trigger( "customevent" );
|
|
|
|
});
|
|
|
|
|
2012-06-14 16:33:16 +00:00
|
|
|
test( "_off() - single event", function() {
|
|
|
|
expect( 3 );
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {} );
|
|
|
|
var shouldTriggerWidget, shouldTriggerOther,
|
|
|
|
element = $( "#widget" ),
|
|
|
|
widget = element.testWidget().data( "testWidget" );
|
|
|
|
widget._on( element, { foo: function() {
|
|
|
|
ok( shouldTriggerWidget, "foo called from _on" );
|
|
|
|
}});
|
|
|
|
element.bind( "foo", function() {
|
|
|
|
ok( shouldTriggerOther, "foo called from bind" );
|
|
|
|
});
|
|
|
|
shouldTriggerWidget = true;
|
|
|
|
shouldTriggerOther = true;
|
|
|
|
element.trigger( "foo" );
|
|
|
|
shouldTriggerWidget = false;
|
|
|
|
widget._off( element, "foo" );
|
|
|
|
element.trigger( "foo" );
|
|
|
|
});
|
|
|
|
|
|
|
|
test( "_off() - multiple events", function() {
|
|
|
|
expect( 6 );
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {} );
|
|
|
|
var shouldTriggerWidget, shouldTriggerOther,
|
|
|
|
element = $( "#widget" ),
|
|
|
|
widget = element.testWidget().data( "testWidget" );
|
|
|
|
widget._on( element, {
|
|
|
|
foo: function() {
|
|
|
|
ok( shouldTriggerWidget, "foo called from _on" );
|
|
|
|
},
|
|
|
|
bar: function() {
|
|
|
|
ok( shouldTriggerWidget, "bar called from _on" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
element.bind( "foo bar", function( event ) {
|
|
|
|
ok( shouldTriggerOther, event.type + " called from bind" );
|
|
|
|
});
|
|
|
|
shouldTriggerWidget = true;
|
|
|
|
shouldTriggerOther = true;
|
|
|
|
element.trigger( "foo" );
|
|
|
|
element.trigger( "bar" );
|
|
|
|
shouldTriggerWidget = false;
|
|
|
|
widget._off( element, "foo bar" );
|
|
|
|
element.trigger( "foo" );
|
|
|
|
element.trigger( "bar" );
|
|
|
|
});
|
|
|
|
|
|
|
|
test( "_off() - all events", function() {
|
|
|
|
expect( 6 );
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {} );
|
|
|
|
var shouldTriggerWidget, shouldTriggerOther,
|
|
|
|
element = $( "#widget" ),
|
|
|
|
widget = element.testWidget().data( "testWidget" );
|
|
|
|
widget._on( element, {
|
|
|
|
foo: function() {
|
|
|
|
ok( shouldTriggerWidget, "foo called from _on" );
|
|
|
|
},
|
|
|
|
bar: function() {
|
|
|
|
ok( shouldTriggerWidget, "bar called from _on" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
element.bind( "foo bar", function( event ) {
|
|
|
|
ok( shouldTriggerOther, event.type + " called from bind" );
|
|
|
|
});
|
|
|
|
shouldTriggerWidget = true;
|
|
|
|
shouldTriggerOther = true;
|
|
|
|
element.trigger( "foo" );
|
|
|
|
element.trigger( "bar" );
|
|
|
|
shouldTriggerWidget = false;
|
|
|
|
widget._off( element );
|
|
|
|
element.trigger( "foo" );
|
|
|
|
element.trigger( "bar" );
|
|
|
|
});
|
|
|
|
|
2011-01-25 02:54:50 +00:00
|
|
|
test( "._hoverable()", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 10 );
|
2011-01-25 02:54:50 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {
|
|
|
|
this._hoverable( this.element.children() );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var div = $( "#widget" ).testWidget().children();
|
|
|
|
ok( !div.hasClass( "ui-state-hover" ), "not hovered on init" );
|
|
|
|
div.trigger( "mouseenter" );
|
|
|
|
ok( div.hasClass( "ui-state-hover" ), "hovered after mouseenter" );
|
|
|
|
div.trigger( "mouseleave" );
|
|
|
|
ok( !div.hasClass( "ui-state-hover" ), "not hovered after mouseleave" );
|
|
|
|
|
|
|
|
div.trigger( "mouseenter" );
|
|
|
|
ok( div.hasClass( "ui-state-hover" ), "hovered after mouseenter" );
|
|
|
|
$( "#widget" ).testWidget( "disable" );
|
|
|
|
ok( !div.hasClass( "ui-state-hover" ), "not hovered while disabled" );
|
|
|
|
div.trigger( "mouseenter" );
|
|
|
|
ok( !div.hasClass( "ui-state-hover" ), "can't hover while disabled" );
|
|
|
|
$( "#widget" ).testWidget( "enable" );
|
|
|
|
ok( !div.hasClass( "ui-state-hover" ), "enabling doesn't reset hover" );
|
|
|
|
|
|
|
|
div.trigger( "mouseenter" );
|
|
|
|
ok( div.hasClass( "ui-state-hover" ), "hovered after mouseenter" );
|
|
|
|
$( "#widget" ).testWidget( "destroy" );
|
|
|
|
ok( !div.hasClass( "ui-state-hover" ), "not hovered after destroy" );
|
|
|
|
div.trigger( "mouseenter" );
|
|
|
|
ok( !div.hasClass( "ui-state-hover" ), "event handler removed on destroy" );
|
|
|
|
});
|
|
|
|
|
|
|
|
test( "._focusable()", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 10 );
|
2011-01-25 02:54:50 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
this._focusable( this.element.children() );
|
|
|
|
}
|
2011-01-25 02:54:50 +00:00
|
|
|
});
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2011-01-25 02:54:50 +00:00
|
|
|
var div = $( "#widget" ).testWidget().children();
|
|
|
|
ok( !div.hasClass( "ui-state-focus" ), "not focused on init" );
|
2011-02-01 02:55:22 +00:00
|
|
|
div.trigger( "focusin" );
|
2011-01-25 02:54:50 +00:00
|
|
|
ok( div.hasClass( "ui-state-focus" ), "focused after explicit focus" );
|
2011-02-01 02:55:22 +00:00
|
|
|
div.trigger( "focusout" );
|
2011-01-25 02:54:50 +00:00
|
|
|
ok( !div.hasClass( "ui-state-focus" ), "not focused after blur" );
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2011-02-01 02:55:22 +00:00
|
|
|
div.trigger( "focusin" );
|
2011-01-25 02:54:50 +00:00
|
|
|
ok( div.hasClass( "ui-state-focus" ), "focused after explicit focus" );
|
|
|
|
$( "#widget" ).testWidget( "disable" );
|
|
|
|
ok( !div.hasClass( "ui-state-focus" ), "not focused while disabled" );
|
2011-02-01 02:55:22 +00:00
|
|
|
div.trigger( "focusin" );
|
2011-01-25 02:54:50 +00:00
|
|
|
ok( !div.hasClass( "ui-state-focus" ), "can't focus while disabled" );
|
|
|
|
$( "#widget" ).testWidget( "enable" );
|
|
|
|
ok( !div.hasClass( "ui-state-focus" ), "enabling doesn't reset focus" );
|
2011-06-19 12:42:15 +00:00
|
|
|
|
2011-02-01 02:55:22 +00:00
|
|
|
div.trigger( "focusin" );
|
2011-01-25 02:54:50 +00:00
|
|
|
ok( div.hasClass( "ui-state-focus" ), "focused after explicit focus" );
|
|
|
|
$( "#widget" ).testWidget( "destroy" );
|
|
|
|
ok( !div.hasClass( "ui-state-focus" ), "not focused after destroy" );
|
2011-02-01 02:55:22 +00:00
|
|
|
div.trigger( "focusin" );
|
2011-01-25 02:54:50 +00:00
|
|
|
ok( !div.hasClass( "ui-state-focus" ), "event handler removed on destroy" );
|
|
|
|
});
|
|
|
|
|
2010-08-26 15:49:07 +00:00
|
|
|
test( "._trigger() - no event, no ui", function() {
|
|
|
|
expect( 7 );
|
|
|
|
var handlers = [];
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {}
|
|
|
|
});
|
|
|
|
|
|
|
|
$( "#widget" ).testWidget({
|
|
|
|
foo: function( event, ui ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( event.type, "testwidgetfoo", "correct event type in callback" );
|
|
|
|
deepEqual( ui, {}, "empty ui hash passed" );
|
2010-08-26 15:49:07 +00:00
|
|
|
handlers.push( "callback" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$( document ).add( "#widget-wrapper" ).add( "#widget" )
|
|
|
|
.bind( "testwidgetfoo", function( event, ui ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, {}, "empty ui hash passed" );
|
2010-08-26 15:49:07 +00:00
|
|
|
handlers.push( this );
|
|
|
|
});
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), true,
|
2010-08-26 15:49:07 +00:00
|
|
|
"_trigger returns true when event is not cancelled" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( handlers, [
|
2010-08-26 15:49:07 +00:00
|
|
|
$( "#widget" )[ 0 ],
|
|
|
|
$( "#widget-wrapper" )[ 0 ],
|
|
|
|
document,
|
|
|
|
"callback"
|
|
|
|
], "event bubbles and then invokes callback" );
|
|
|
|
|
|
|
|
$( document ).unbind( "testwidgetfoo" );
|
|
|
|
});
|
|
|
|
|
|
|
|
test( "._trigger() - cancelled event", function() {
|
|
|
|
expect( 3 );
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {}
|
|
|
|
});
|
|
|
|
|
|
|
|
$( "#widget" ).testWidget({
|
2012-10-23 02:37:42 +00:00
|
|
|
foo: function() {
|
2010-08-26 15:49:07 +00:00
|
|
|
ok( true, "callback invoked even if event is cancelled" );
|
|
|
|
}
|
|
|
|
})
|
2012-10-23 02:37:42 +00:00
|
|
|
.bind( "testwidgetfoo", function() {
|
2010-08-26 15:49:07 +00:00
|
|
|
ok( true, "event was triggered" );
|
|
|
|
return false;
|
|
|
|
});
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), false,
|
2010-08-26 15:49:07 +00:00
|
|
|
"_trigger returns false when event is cancelled" );
|
|
|
|
});
|
|
|
|
|
|
|
|
test( "._trigger() - cancelled callback", function() {
|
2012-06-27 15:32:48 +00:00
|
|
|
expect( 1 );
|
2010-08-26 15:49:07 +00:00
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {}
|
|
|
|
});
|
|
|
|
|
|
|
|
$( "#widget" ).testWidget({
|
2012-10-23 02:37:42 +00:00
|
|
|
foo: function() {
|
2010-08-26 15:49:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2012-03-01 13:30:02 +00:00
|
|
|
deepEqual( $( "#widget" ).data( "ui-testWidget" )._trigger( "foo" ), false,
|
2010-08-26 15:49:07 +00:00
|
|
|
"_trigger returns false when callback returns false" );
|
|
|
|
});
|
|
|
|
|
|
|
|
test( "._trigger() - provide event and ui", function() {
|
|
|
|
expect( 7 );
|
|
|
|
|
|
|
|
var originalEvent = $.Event( "originalTest" );
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {},
|
|
|
|
testEvent: function() {
|
|
|
|
var ui = {
|
|
|
|
foo: "bar",
|
|
|
|
baz: {
|
|
|
|
qux: 5,
|
|
|
|
quux: 20
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._trigger( "foo", originalEvent, ui );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, {
|
2010-08-26 15:49:07 +00:00
|
|
|
foo: "notbar",
|
|
|
|
baz: {
|
|
|
|
qux: 10,
|
|
|
|
quux: "jQuery"
|
|
|
|
}
|
|
|
|
}, "ui object modified" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$( "#widget" ).bind( "testwidgetfoo", function( event, ui ) {
|
|
|
|
equal( event.originalEvent, originalEvent, "original event object passed" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, {
|
2010-08-26 15:49:07 +00:00
|
|
|
foo: "bar",
|
|
|
|
baz: {
|
|
|
|
qux: 5,
|
|
|
|
quux: 20
|
|
|
|
}
|
|
|
|
}, "ui hash passed" );
|
|
|
|
ui.foo = "notbar";
|
|
|
|
});
|
|
|
|
$( "#widget-wrapper" ).bind( "testwidgetfoo", function( event, ui ) {
|
|
|
|
equal( event.originalEvent, originalEvent, "original event object passed" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, {
|
2010-08-26 15:49:07 +00:00
|
|
|
foo: "notbar",
|
|
|
|
baz: {
|
|
|
|
qux: 5,
|
|
|
|
quux: 20
|
|
|
|
}
|
|
|
|
}, "modified ui hash passed" );
|
|
|
|
ui.baz.qux = 10;
|
|
|
|
});
|
|
|
|
$( "#widget" ).testWidget({
|
|
|
|
foo: function( event, ui ) {
|
|
|
|
equal( event.originalEvent, originalEvent, "original event object passed" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, {
|
2010-08-26 15:49:07 +00:00
|
|
|
foo: "notbar",
|
|
|
|
baz: {
|
|
|
|
qux: 10,
|
|
|
|
quux: 20
|
|
|
|
}
|
|
|
|
}, "modified ui hash passed" );
|
|
|
|
ui.baz.quux = "jQuery";
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.testWidget( "testEvent" );
|
|
|
|
});
|
|
|
|
|
2011-03-28 16:37:08 +00:00
|
|
|
test( "._trigger() - array as ui", function() {
|
|
|
|
// #6795 - Widget: handle array arguments to _trigger consistently
|
|
|
|
expect( 4 );
|
|
|
|
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {},
|
|
|
|
testEvent: function() {
|
|
|
|
var ui = {
|
|
|
|
foo: "bar",
|
|
|
|
baz: {
|
|
|
|
qux: 5,
|
|
|
|
quux: 20
|
|
|
|
}
|
2012-04-19 02:36:15 +00:00
|
|
|
},
|
|
|
|
extra = {
|
|
|
|
bar: 5
|
2011-03-28 16:37:08 +00:00
|
|
|
};
|
|
|
|
this._trigger( "foo", null, [ ui, extra ] );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$( "#widget" ).bind( "testwidgetfoo", function( event, ui, extra ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, {
|
2011-03-28 16:37:08 +00:00
|
|
|
foo: "bar",
|
|
|
|
baz: {
|
|
|
|
qux: 5,
|
|
|
|
quux: 20
|
|
|
|
}
|
|
|
|
}, "event: ui hash passed" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( extra, {
|
2011-03-28 16:37:08 +00:00
|
|
|
bar: 5
|
|
|
|
}, "event: extra argument passed" );
|
|
|
|
});
|
|
|
|
$( "#widget" ).testWidget({
|
|
|
|
foo: function( event, ui, extra ) {
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, {
|
2011-03-28 16:37:08 +00:00
|
|
|
foo: "bar",
|
|
|
|
baz: {
|
|
|
|
qux: 5,
|
|
|
|
quux: 20
|
|
|
|
}
|
|
|
|
}, "callback: ui hash passed" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( extra, {
|
2011-03-28 16:37:08 +00:00
|
|
|
bar: 5
|
|
|
|
}, "callback: extra argument passed" );
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.testWidget( "testEvent" );
|
|
|
|
});
|
|
|
|
|
2011-03-22 18:35:18 +00:00
|
|
|
test( "._trigger() - instance as element", function() {
|
2011-01-24 18:33:24 +00:00
|
|
|
expect( 4 );
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
defaultElement: null,
|
|
|
|
testEvent: function() {
|
2012-04-19 02:36:15 +00:00
|
|
|
this._trigger( "foo", null, { foo: "bar" } );
|
2011-01-24 18:33:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
var instance = $.ui.testWidget({
|
|
|
|
foo: function( event, ui ) {
|
|
|
|
equal( event.type, "testwidgetfoo", "event object passed to callback" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, { foo: "bar" }, "ui object passed to callback" );
|
2011-01-24 18:33:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$( instance ).bind( "testwidgetfoo", function( event, ui ) {
|
|
|
|
equal( event.type, "testwidgetfoo", "event object passed to event handler" );
|
2012-02-28 14:56:32 +00:00
|
|
|
deepEqual( ui, { foo: "bar" }, "ui object passed to event handler" );
|
2011-01-24 18:33:24 +00:00
|
|
|
});
|
|
|
|
instance.testEvent();
|
|
|
|
});
|
|
|
|
|
2011-07-28 12:26:59 +00:00
|
|
|
(function() {
|
|
|
|
function shouldDestroy( expected, callback ) {
|
|
|
|
expect( 1 );
|
|
|
|
var destroyed = false;
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
_create: function() {},
|
|
|
|
destroy: function() {
|
|
|
|
destroyed = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
callback();
|
|
|
|
equal( destroyed, expected );
|
|
|
|
}
|
2010-09-03 13:49:29 +00:00
|
|
|
|
2011-07-28 12:26:59 +00:00
|
|
|
test( "auto-destroy - .remove()", function() {
|
|
|
|
shouldDestroy( true, function() {
|
|
|
|
$( "#widget" ).testWidget().remove();
|
|
|
|
});
|
2010-09-03 13:49:29 +00:00
|
|
|
});
|
2011-09-12 21:23:54 +00:00
|
|
|
|
2011-07-28 12:26:59 +00:00
|
|
|
test( "auto-destroy - .remove() on parent", function() {
|
|
|
|
shouldDestroy( true, function() {
|
|
|
|
$( "#widget" ).testWidget().parent().remove();
|
|
|
|
});
|
2010-09-03 13:49:29 +00:00
|
|
|
});
|
2011-09-12 21:23:54 +00:00
|
|
|
|
2011-07-28 12:26:59 +00:00
|
|
|
test( "auto-destroy - .remove() on child", function() {
|
|
|
|
shouldDestroy( false, function() {
|
|
|
|
$( "#widget" ).testWidget().children().remove();
|
|
|
|
});
|
2010-09-03 13:49:29 +00:00
|
|
|
});
|
2011-09-12 21:23:54 +00:00
|
|
|
|
2011-07-28 12:26:59 +00:00
|
|
|
test( "auto-destroy - .empty()", function() {
|
|
|
|
shouldDestroy( false, function() {
|
|
|
|
$( "#widget" ).testWidget().empty();
|
|
|
|
});
|
2010-09-03 13:49:29 +00:00
|
|
|
});
|
2011-09-12 21:23:54 +00:00
|
|
|
|
2011-07-28 12:26:59 +00:00
|
|
|
test( "auto-destroy - .empty() on parent", function() {
|
|
|
|
shouldDestroy( true, function() {
|
|
|
|
$( "#widget" ).testWidget().parent().empty();
|
|
|
|
});
|
2010-09-03 13:49:29 +00:00
|
|
|
});
|
2011-09-12 21:23:54 +00:00
|
|
|
|
2011-07-28 12:26:59 +00:00
|
|
|
test( "auto-destroy - .detach()", function() {
|
|
|
|
shouldDestroy( false, function() {
|
|
|
|
$( "#widget" ).testWidget().detach();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}());
|
2010-09-03 13:49:29 +00:00
|
|
|
|
2011-02-03 21:37:17 +00:00
|
|
|
test( "redefine", function() {
|
|
|
|
expect( 4 );
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
method: function( str ) {
|
|
|
|
strictEqual( this, instance, "original invoked with correct this" );
|
|
|
|
equal( str, "bar", "original invoked with correct parameter" );
|
|
|
|
}
|
|
|
|
});
|
2011-02-11 02:25:50 +00:00
|
|
|
$.ui.testWidget.foo = "bar";
|
2011-02-03 21:37:17 +00:00
|
|
|
$.widget( "ui.testWidget", $.ui.testWidget, {
|
|
|
|
method: function( str ) {
|
|
|
|
equal( str, "foo", "new invoked with correct parameter" );
|
2011-11-21 13:43:08 +00:00
|
|
|
this._super( "bar" );
|
2011-02-03 21:37:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-02-01 21:59:26 +00:00
|
|
|
var instance = new $.ui.testWidget({});
|
2011-02-03 21:37:17 +00:00
|
|
|
instance.method( "foo" );
|
2011-02-11 02:25:50 +00:00
|
|
|
equal( $.ui.testWidget.foo, "bar", "static properties remain" );
|
2011-02-03 21:37:17 +00:00
|
|
|
});
|
|
|
|
|
2012-02-01 21:59:26 +00:00
|
|
|
test( "redefine deep prototype chain", function() {
|
|
|
|
expect( 8 );
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
method: function( str ) {
|
|
|
|
strictEqual( this, instance, "original invoked with correct this" );
|
|
|
|
equal( str, "level 4", "original invoked with correct parameter" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$.widget( "ui.testWidget2", $.ui.testWidget, {
|
|
|
|
method: function( str ) {
|
|
|
|
strictEqual( this, instance, "testWidget2 invoked with correct this" );
|
|
|
|
equal( str, "level 2", "testWidget2 invoked with correct parameter" );
|
|
|
|
this._super( "level 3" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$.widget( "ui.testWidget3", $.ui.testWidget2, {
|
|
|
|
method: function( str ) {
|
|
|
|
strictEqual( this, instance, "testWidget3 invoked with correct this" );
|
|
|
|
equal( str, "level 1", "testWidget3 invoked with correct parameter" );
|
|
|
|
this._super( "level 2" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// redefine testWidget after other widgets have inherited from it
|
|
|
|
// this tests whether the inheriting widgets get updated prototype chains
|
|
|
|
$.widget( "ui.testWidget", $.ui.testWidget, {
|
|
|
|
method: function( str ) {
|
|
|
|
strictEqual( this, instance, "new invoked with correct this" );
|
|
|
|
equal( str, "level 3", "new invoked with correct parameter" );
|
|
|
|
this._super( "level 4" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// redefine testWidget3 after it has been automatically redefined
|
|
|
|
// this tests whether we properly handle _super() when the topmost prototype
|
|
|
|
// doesn't have the method defined
|
|
|
|
$.widget( "ui.testWidget3", $.ui.testWidget3, {} );
|
|
|
|
|
|
|
|
var instance = new $.ui.testWidget3({});
|
|
|
|
instance.method( "level 1" );
|
|
|
|
|
|
|
|
delete $.ui.testWidget3;
|
|
|
|
delete $.ui.testWidget2;
|
|
|
|
});
|
|
|
|
|
2011-09-12 21:37:14 +00:00
|
|
|
asyncTest( "_delay", function() {
|
2011-09-12 21:47:09 +00:00
|
|
|
expect( 6 );
|
2011-09-12 21:37:14 +00:00
|
|
|
var order = 0,
|
|
|
|
that;
|
|
|
|
$.widget( "ui.testWidget", {
|
|
|
|
defaultElement: null,
|
|
|
|
_create: function() {
|
|
|
|
that = this;
|
2011-09-12 21:47:09 +00:00
|
|
|
var timer = this._delay(function() {
|
2011-09-12 21:37:14 +00:00
|
|
|
strictEqual( this, that );
|
|
|
|
equal( order, 1 );
|
|
|
|
start();
|
|
|
|
}, 500);
|
2011-09-12 21:47:09 +00:00
|
|
|
ok( timer !== undefined );
|
|
|
|
timer = this._delay("callback");
|
|
|
|
ok( timer !== undefined );
|
2011-09-12 21:37:14 +00:00
|
|
|
},
|
|
|
|
callback: function() {
|
|
|
|
strictEqual( this, that );
|
|
|
|
equal( order, 0 );
|
|
|
|
order += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$( "#widget" ).testWidget();
|
|
|
|
});
|
|
|
|
|
2011-01-25 02:22:40 +00:00
|
|
|
}( jQuery ) );
|