-module("event");
+module("event", { teardown: moduleTeardown });
test("null or undefined handler", function() {
expect(2);
- // Supports Fixes bug #7229
- try {
-
- jQuery("#firstp").click(null);
-
- ok(true, "Passing a null handler will not throw an exception");
-
- } catch (e) {}
-
- try {
-
- jQuery("#firstp").click(undefined);
-
- ok(true, "Passing an undefined handler will not throw an exception");
+ // Supports Fixes bug #7229
+ try {
+ jQuery("#firstp").click(null);
+ ok(true, "Passing a null handler will not throw an exception");
+ } catch (e) {}
- } catch (e) {}
+ try {
+ jQuery("#firstp").click(undefined);
+ ok(true, "Passing an undefined handler will not throw an exception");
+ } catch (e) {}
});
test("bind(), with data", function() {
};
jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
- ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
+ ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
});
test("click(), with data", function() {
};
jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
- ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
+ ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
});
test("bind(), with data, trigger with data", function() {
cur = "focusin";
div.trigger("focusin.a");
+ // manually clean up detached elements
+ div.remove();
+
div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
equals( e.type, cur, "Verify right multi event was fired." );
equals( e.data, obj, "Make sure the data came in correctly." );
cur = "mouseover";
div.trigger("mouseover");
+ // manually clean up detached elements
+ div.remove();
+
div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
equals( e.type, cur, "Verify right multi event was fired." );
});
cur = "focusout";
div.trigger("focusout.b");
+
+ // manually clean up detached elements
+ div.remove();
});
test("bind(), namespace with special add", function() {
// Use a native click so we don't get jQuery simulated bubbling
if ( document.createEvent ) {
var e = document.createEvent( 'MouseEvents' );
- e.initEvent( "click", true, true );
+ e.initEvent( "click", true, true );
$jq[0].dispatchEvent(e);
}
else if ( $jq[0].click ) {
}).trigger('change');
});
-test("bind(), namespaced events, cloned events", function() {
- expect(6);
+test("bind(), namespaced events, cloned events", 18, function() {
+ var firstp = jQuery( "#firstp" );
- jQuery("#firstp").bind("custom.test",function(e){
- ok(true, "Custom event triggered");
+ firstp.bind("custom.test",function(e){
+ ok(false, "Custom event triggered");
});
- jQuery("#firstp").bind("click",function(e){
+ firstp.bind("click",function(e){
ok(true, "Normal click triggered");
+ equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" );
});
- jQuery("#firstp").bind("click.test",function(e){
- ok(true, "Namespaced click triggered");
+ firstp.bind("click.test",function(e){
+ var check = "click";
+ ok( true, "Namespaced click triggered" );
+ if ( e.namespace ) {
+ check += "test";
+ }
+ equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" );
});
- // Trigger both bound fn (2)
- jQuery("#firstp").trigger("click");
+ //clone(true) element to verify events are cloned correctly
+ firstp = firstp.add( firstp.clone( true ).attr( "id", "firstp2" ).insertBefore( firstp ) );
- // Trigger one bound fn (1)
- jQuery("#firstp").trigger("click.test");
+ // Trigger both bound fn (8)
+ firstp.trigger("click");
+
+ // Trigger one bound fn (4)
+ firstp.trigger("click.test");
// Remove only the one fn
- jQuery("#firstp").unbind("click.test");
+ firstp.unbind("click.test");
- // Trigger the remaining fn (1)
- jQuery("#firstp").trigger("click");
+ // Trigger the remaining fn (4)
+ firstp.trigger("click");
- // Remove the remaining fn
- jQuery("#firstp").unbind(".test");
+ // Remove the remaining namespaced fn
+ firstp.unbind(".test");
- // Trigger the remaining fn (0)
- jQuery("#firstp").trigger("custom");
+ // Try triggering the custom event (0)
+ firstp.trigger("custom");
// using contents will get comments regular, text, and comment nodes
jQuery("#nonnodes").contents().bind("tester", function () {
}).trigger("tester");
// Make sure events stick with appendTo'd elements (which are cloned) #2027
- jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
+ jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("#main");
ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
});
test("bind(), with same function", function() {
expect(2)
- var count = 0 , func = function(){
+ var count = 0, func = function(){
count++;
};
.bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
.bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
- ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
+ ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
});
test("bind(name, false), unbind(name, false)", function() {
jQuery("#ap").unbind("click", false);
jQuery("#ap").trigger("click");
equals( main, 1, "Verify that the trigger happened correctly." );
+
+ // manually clean up events from elements outside the fixture
+ jQuery("#main").unbind("click");
});
test("bind()/trigger()/unbind() on plain object", function() {
- expect( 8 );
+ expect( 7 );
var obj = {};
}
});
- var events = jQuery(obj).data("__events__");
+ var events = jQuery._data(obj, "events");
ok( events, "Object has events bound." );
equals( obj.events, undefined, "Events object on plain objects is not events" );
- equals( typeof events, "function", "'events' expando is a function on plain objects." );
equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
// Make sure it doesn't complain when no events are found
jQuery(obj).unbind("test");
- equals( obj.__events__, undefined, "Make sure events object is removed" );
+ equals( obj && obj[ jQuery.expando ] &&
+ obj[ jQuery.expando ][ jQuery.expando ] &&
+ obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
});
test("unbind(type)", function() {
message = "unbind many with function";
$elem.bind('error1 error2',error)
- .unbind('error1 error2', error )
- .trigger('error1').triggerHandler('error2');
+ .unbind('error1 error2', error )
+ .trigger('error1').triggerHandler('error2');
message = "unbind many"; // #3538
$elem.bind('error1 error2',error)
- .unbind('error1 error2')
- .trigger('error1').triggerHandler('error2');
+ .unbind('error1 error2')
+ .trigger('error1').triggerHandler('error2');
message = "unbind without a type or handler";
$elem.bind("error1 error2.test",error)
- .unbind()
- .trigger("error1").triggerHandler("error2");
+ .unbind()
+ .trigger("error1").triggerHandler("error2");
});
test("unbind(eventObject)", function() {
equals( times, 4, "hover handlers fired" );
});
+test("mouseover triggers mouseenter", function() {
+ expect(1);
+
+ var count = 0,
+ elem = jQuery("<a />");
+ elem.mouseenter(function () {
+ count++;
+ });
+ elem.trigger('mouseover');
+ equals(count, 1, "make sure mouseover triggers a mouseenter" );
+
+ elem.remove();
+});
+
test("trigger() shortcuts", function() {
expect(6);
- jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
+
+ var elem = jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL');
+ elem.find('a').bind('click', function() {
var close = jQuery('spanx', this); // same with jQuery(this).find('span');
equals( close.length, 0, "Context element does not exist, length must be zero" );
ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
return false;
}).click();
+ // manually clean up detached elements
+ elem.remove();
+
jQuery("#check1").click(function() {
ok( true, "click event handler for checkbox gets fired twice, see #815" );
}).click();
jQuery('#simon1').click();
equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
- jQuery('<img />').load(function(){
+ elem = jQuery('<img />').load(function(){
ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
}).load();
+
+ // manually clean up detached elements
+ elem.remove();
});
test("trigger() bubbling", function() {
equals( body, 2, "ap bubble" );
equals( main, 1, "ap bubble" );
equals( ap, 1, "ap bubble" );
+
+ // manually clean up events from elements outside the fixture
+ jQuery(document).unbind("click");
+ jQuery("html, body, #main").unbind("click");
});
test("trigger(type, [data], [fn])", function() {
pass = true;
try {
- jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
+ jQuery('#main table:first').bind('test:test', function(){}).trigger('test:test');
} catch (e) {
pass = false;
}
equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
$div.unbind('click',fns[0]);
- var data = jQuery.data( $div[0], 'events' );
+ var data = jQuery._data( $div[0], 'events' );
ok( !data, "Unbinding one function from toggle unbinds them all");
+ // manually clean up detached elements
+ $div.remove();
+
// Test Multi-Toggles
var a = [], b = [];
$div = jQuery("<div/>");
$div.click();
same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
same( b, [1,2], "Check that a click worked with a second toggle, second click." );
+
+ // manually clean up detached elements
+ $div.remove();
});
test(".live()/.die()", function() {
equals( clicked, 2, "live with a context" );
// Make sure the event is actually stored on the context
- ok( jQuery.data(container, "events").live, "live with a context" );
+ ok( jQuery._data(container, "events").live, "live with a context" );
// Test unbinding with a different context
jQuery("#foo", container).die("click");
div.trigger("submit");
equals( count, 2, "Make sure both the click and submit were triggered." );
+
+ // manually clean up events from elements outside the fixture
+ div.die();
});
test("live with namespaces", function(){
});
test("live with submit", function() {
+ expect(5);
+
var count1 = 0, count2 = 0;
jQuery("#testForm").live("submit", function(ev) {
equals( count1, 1, "Verify form submit." );
equals( count2, 1, "Verify body submit." );
+ jQuery("#testForm input[name=sub1]").live("click", function(ev) {
+ ok( true, "cancelling submit still calls click handler" );
+ });
+
+ jQuery("#testForm input[name=sub1]")[0].click();
+ equals( count1, 2, "Verify form submit." );
+ equals( count2, 2, "Verify body submit." );
+
jQuery("#testForm").die("submit");
+ jQuery("#testForm input[name=sub1]").die("click");
jQuery("body").die("submit");
});
equals( clicked, 2, "delegate with a context" );
// Make sure the event is actually stored on the context
- ok( jQuery.data(container, "events").live, "delegate with a context" );
+ ok( jQuery._data(container, "events").live, "delegate with a context" );
// Test unbinding with a different context
jQuery("#main").undelegate("#foo", "click");
ok( true, "Resize event fired." );
}).resize().unbind("resize");
- ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
+ ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
});
test("focusin bubbles", function() {
- //create an input and focusin on it
- var input = jQuery("<input/>"), order = 0;
-
- input.prependTo("body");
+ expect(4);
+
+ var input = jQuery( '<input type="text" />' ).prependTo( "body" ),
+ order = 0;
- jQuery("body").bind("focusin.focusinBubblesTest",function(){
- equals(1,order++,"focusin on the body second")
+ jQuery( "body" ).bind( "focusin.focusinBubblesTest", function(){
+ equals( 1, order++, "focusin on the body second" );
});
- input.bind("focusin.focusinBubblesTest",function(){
- equals(0,order++,"focusin on the element first")
+ input.bind( "focusin.focusinBubblesTest", function(){
+ equals( 0, order++, "focusin on the element first" );
});
+ // DOM focus method
input[0].focus();
- input.remove();
+ // jQuery trigger, which calls DOM focus
+ order = 0;
+ input[0].blur();
+ input.trigger( "focus" );
- jQuery("body").unbind("focusin.focusinBubblesTest");
+ input.remove();
+ jQuery( "body" ).unbind( "focusin.focusinBubblesTest" );
});
/*