aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/widget/widget_core.js
diff options
context:
space:
mode:
authorScott González <scott.gonzalez@gmail.com>2011-01-31 22:04:09 -0500
committerScott González <scott.gonzalez@gmail.com>2011-01-31 22:04:09 -0500
commitcb8f5b7f2679ab5473229cac432f28c72521048c (patch)
tree60b63ec0b7f190832dcadef9e04a195985e61621 /tests/unit/widget/widget_core.js
parentbb857ddd8d6d4ac7620b49bf765352cd064494c0 (diff)
parenteffbb2c0ec2e60bea6ec5e5822ee52f8ea9e18bc (diff)
downloadjquery-ui-cb8f5b7f2679ab5473229cac432f28c72521048c.tar.gz
jquery-ui-cb8f5b7f2679ab5473229cac432f28c72521048c.zip
Merge branch 'master' into widget-events
Conflicts: ui/jquery.ui.widget.js
Diffstat (limited to 'tests/unit/widget/widget_core.js')
-rw-r--r--tests/unit/widget/widget_core.js103
1 files changed, 83 insertions, 20 deletions
diff --git a/tests/unit/widget/widget_core.js b/tests/unit/widget/widget_core.js
index d993194e2..6f4930621 100644
--- a/tests/unit/widget/widget_core.js
+++ b/tests/unit/widget/widget_core.js
@@ -1,6 +1,3 @@
-/*
- * widget unit tests
- */
(function( $ ) {
module( "widget factory", {
@@ -26,6 +23,54 @@ test( "widget creation", function() {
"option method copied over from base widget" );
});
+test( "element normalization", function() {
+ expect( 11 );
+ var elem;
+ $.widget( "ui.testWidget", {} );
+
+ $.ui.testWidget.prototype._create = function() {
+ ok( this.element.is( "div" ), "generated div" );
+ same( this.element.data( "testWidget" ), this, "intance stored in .data()" );
+ };
+ $.ui.testWidget();
+
+ $.ui.testWidget.prototype.defaultElement = "<span data-test='pass'>";
+ $.ui.testWidget.prototype._create = function() {
+ ok( this.element.is( "span[data-test=pass]" ), "generated span with properties" );
+ same( this.element.data( "testWidget" ), this, "instace stored in .data()" );
+ };
+ $.ui.testWidget();
+
+ elem = $( "<input>" );
+ $.ui.testWidget.prototype._create = function() {
+ same( this.element[ 0 ], elem[ 0 ], "from element" );
+ same( elem.data( "testWidget" ), this, "instace stored in .data()" );
+ };
+ $.ui.testWidget( {}, elem[ 0 ] );
+
+ elem = $( "<div>" );
+ $.ui.testWidget.prototype._create = function() {
+ same( this.element[ 0 ], elem[ 0 ], "from jQuery object" );
+ same( elem.data( "testWidget" ), this, "instace stored in .data()" );
+ };
+ $.ui.testWidget( {}, elem );
+
+ elem = $( "<div id='element-normalization-selector'></div>" )
+ .appendTo( "#main" );
+ $.ui.testWidget.prototype._create = function() {
+ same( this.element[ 0 ], elem[ 0 ], "from selector" );
+ same( elem.data( "testWidget" ), this, "instace stored in .data()" );
+ };
+ $.ui.testWidget( {}, "#element-normalization-selector" );
+
+ $.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();
+});
+
test( "jQuery usage", function() {
expect( 11 );
@@ -55,7 +100,7 @@ test( "jQuery usage", function() {
});
shouldCreate = true;
- var elem = $( "<div></div>" )
+ var elem = $( "<div>" )
.bind( "testwidgetcreate", function() {
ok( shouldCreate, "create event triggered on instantiation" );
})
@@ -101,7 +146,7 @@ test( "direct usage", function() {
}
});
- var elem = $( "<div></div>" )[ 0 ];
+ var elem = $( "<div>" )[ 0 ];
shouldCreate = true;
var instance = new $.ui.testWidget( {}, elem );
@@ -153,7 +198,7 @@ test( "merge multiple option arguments", function() {
});
}
});
- $( "<div></div>" ).testWidget({
+ $( "<div>" ).testWidget({
option1: "valuex",
option2: "valuex",
option3: "value3",
@@ -200,7 +245,7 @@ test( "._getCreateOptions()", function() {
});
test( "re-init", function() {
- var div = $( "<div></div>" ),
+ var div = $( "<div>" ),
actions = [];
$.widget( "ui.testWidget", {
@@ -286,7 +331,7 @@ test( ".option() - getter", function() {
_create: function() {}
});
- var div = $( "<div></div>" ).testWidget({
+ var div = $( "<div>" ).testWidget({
foo: "bar",
baz: 5,
qux: [ "quux", "quuux" ]
@@ -317,7 +362,7 @@ test( ".option() - delegate to ._setOptions()", function() {
calls.push( options );
}
});
- var div = $( "<div></div>" ).testWidget();
+ var div = $( "<div>" ).testWidget();
calls = [];
div.testWidget( "option", "foo", "bar" );
@@ -343,7 +388,7 @@ test( ".option() - delegate to ._setOption()", function() {
});
}
});
- var div = $( "<div></div>" ).testWidget();
+ var div = $( "<div>" ).testWidget();
calls = [];
div.testWidget( "option", "foo", "bar" );
@@ -370,7 +415,7 @@ test( ".enable()", function() {
same( val, false, "disabled set to false" );
}
});
- $( "<div></div>" ).testWidget().testWidget( "enable" );
+ $( "<div>" ).testWidget().testWidget( "enable" );
});
test( ".disable()", function() {
@@ -382,26 +427,26 @@ test( ".disable()", function() {
same( val, true, "disabled set to true" );
}
});
- $( "<div></div>" ).testWidget().testWidget( "disable" );
+ $( "<div>" ).testWidget().testWidget( "disable" );
});
test( ".widget() - base", function() {
$.widget( "ui.testWidget", {
_create: function() {}
});
- var div = $( "<div></div>" ).testWidget();
+ var div = $( "<div>" ).testWidget();
same( div[0], div.testWidget( "widget" )[0]);
});
test( ".widget() - overriden", function() {
- var wrapper = $( "<div></div>" );
+ var wrapper = $( "<div>" );
$.widget( "ui.testWidget", {
_create: function() {},
widget: function() {
return wrapper;
}
});
- same( wrapper[0], $( "<div></div>" ).testWidget().testWidget( "widget" )[0] );
+ same( wrapper[0], $( "<div>" ).testWidget().testWidget( "widget" )[0] );
});
test( "._bind() to element (default)", function() {
@@ -691,6 +736,28 @@ test( "._trigger() - provide event and ui", function() {
.testWidget( "testEvent" );
});
+test( "._triger() - instance as element", function() {
+ expect( 4 );
+ $.widget( "ui.testWidget", {
+ defaultElement: null,
+ testEvent: function() {
+ var ui = { foo: "bar" };
+ this._trigger( "foo", null, ui );
+ }
+ });
+ var instance = $.ui.testWidget({
+ foo: function( event, ui ) {
+ equal( event.type, "testwidgetfoo", "event object passed to callback" );
+ same( ui, { foo: "bar" }, "ui object passed to callback" );
+ }
+ });
+ $( instance ).bind( "testwidgetfoo", function( event, ui ) {
+ equal( event.type, "testwidgetfoo", "event object passed to event handler" );
+ same( ui, { foo: "bar" }, "ui object passed to event handler" );
+ });
+ instance.testEvent();
+});
+
test( "auto-destroy - .remove()", function() {
expect( 1 );
$.widget( "ui.testWidget", {
@@ -721,8 +788,6 @@ test( "auto-destroy - .remove() on child", function() {
}
});
$( "#widget" ).testWidget().children().remove();
- // http://github.com/jquery/qunit/pull/34
- $.ui.testWidget.prototype.destroy = $.noop;
});
test( "auto-destroy - .empty()", function() {
@@ -733,8 +798,6 @@ test( "auto-destroy - .empty()", function() {
}
});
$( "#widget" ).testWidget().empty();
- // http://github.com/jquery/qunit/pull/34
- $.ui.testWidget.prototype.destroy = $.noop;
});
test( "auto-destroy - .empty() on parent", function() {
@@ -758,4 +821,4 @@ test( "auto-destroy - .detach()", function() {
$( "#widget" ).testWidget().detach();
});
-})( jQuery );
+}( jQuery ) );