aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/widget
diff options
context:
space:
mode:
authorScott González <scott.gonzalez@gmail.com>2010-08-26 11:49:07 -0400
committerScott González <scott.gonzalez@gmail.com>2010-08-26 11:49:07 -0400
commit306764ca0f927476cdf99fa1bb77338bf341292f (patch)
tree277fb328e64835f02f72dbc7188e2f90a444af29 /tests/unit/widget
parentedfc8631d1449ee30c0c760aec6ca6ec23d40ecf (diff)
downloadjquery-ui-306764ca0f927476cdf99fa1bb77338bf341292f.tar.gz
jquery-ui-306764ca0f927476cdf99fa1bb77338bf341292f.zip
Widget: Added more unit tests.
Diffstat (limited to 'tests/unit/widget')
-rw-r--r--tests/unit/widget/widget.html4
-rw-r--r--tests/unit/widget/widget_core.js128
2 files changed, 132 insertions, 0 deletions
diff --git a/tests/unit/widget/widget.html b/tests/unit/widget/widget.html
index 9e02880c4..b2b9c0aa2 100644
--- a/tests/unit/widget/widget.html
+++ b/tests/unit/widget/widget.html
@@ -26,6 +26,10 @@
<div id="main" style="position: absolute; top: -10000px; left: -10000px;">
+<div id="widget-wrapper">
+ <div id="widget"></div>
+</div>
+
</div>
</body>
diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js
index cc0019825..2d5502838 100644
--- a/tests/unit/widget/widget_core.js
+++ b/tests/unit/widget/widget_core.js
@@ -281,4 +281,132 @@ test( ".widget() - overriden", function() {
same( wrapper[0], $( "<div></div>" ).testWidget().testWidget( "widget" )[0] );
});
+test( "._trigger() - no event, no ui", function() {
+ expect( 7 );
+ var handlers = [];
+
+ $.widget( "ui.testWidget", {
+ _create: function() {}
+ });
+
+ $( "#widget" ).testWidget({
+ foo: function( event, ui ) {
+ same( event.type, "testwidgetfoo", "correct event type in callback" );
+ same( ui, {}, "empty ui hash passed" );
+ handlers.push( "callback" );
+ }
+ });
+ $( document ).add( "#widget-wrapper" ).add( "#widget" )
+ .bind( "testwidgetfoo", function( event, ui ) {
+ same( ui, {}, "empty ui hash passed" );
+ handlers.push( this );
+ });
+ same( $( "#widget" ).data( "testWidget" )._trigger( "foo" ), true,
+ "_trigger returns true when event is not cancelled" );
+ same( handlers, [
+ $( "#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({
+ foo: function( event, ui ) {
+ ok( true, "callback invoked even if event is cancelled" );
+ }
+ })
+ .bind( "testwidgetfoo", function( event, ui ) {
+ ok( true, "event was triggered" );
+ return false;
+ });
+ same( $( "#widget" ).data( "testWidget" )._trigger( "foo" ), false,
+ "_trigger returns false when event is cancelled" );
+});
+
+test( "._trigger() - cancelled callback", function() {
+ $.widget( "ui.testWidget", {
+ _create: function() {}
+ });
+
+ $( "#widget" ).testWidget({
+ foo: function( event, ui ) {
+ return false;
+ }
+ });
+ same( $( "#widget" ).data( "testWidget" )._trigger( "foo" ), false,
+ "_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 );
+ same( ui, {
+ foo: "notbar",
+ baz: {
+ qux: 10,
+ quux: "jQuery"
+ }
+ }, "ui object modified" );
+ }
+ });
+ $( "#widget" ).bind( "testwidgetfoo", function( event, ui ) {
+ equal( event.originalEvent, originalEvent, "original event object passed" );
+ same( ui, {
+ 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" );
+ same( ui, {
+ 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" );
+ same( ui, {
+ foo: "notbar",
+ baz: {
+ qux: 10,
+ quux: 20
+ }
+ }, "modified ui hash passed" );
+ ui.baz.quux = "jQuery";
+ }
+ })
+ .testWidget( "testEvent" );
+});
+
})( jQuery );