X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fevent.js;h=b7b26046214df7645e6d71836fd489e2eed010fc;hb=6b08d88d04f4a41849753999e6e18126895086d0;hp=a6d8cb68bb0963b2c1dbd6a06cbc9433738f16e1;hpb=e2941d5a98e91c5f61b200b2763e5fa0eb339365;p=jquery.git diff --git a/test/unit/event.js b/test/unit/event.js index a6d8cb6..b7b2604 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -2,22 +2,16 @@ 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() { @@ -304,14 +298,14 @@ test("live/delegate immediate propagation", function() { $p.undelegate( "click" ); }); -test("bind/delegate bubbling, isDefaultPrevented (Bug #7793)", function() { +test("bind/delegate bubbling, isDefaultPrevented", function() { expect(2); var $anchor2 = jQuery( "#anchor2" ), $main = jQuery( "#main" ), fakeClick = function($jq) { // Use a native click so we don't get jQuery simulated bubbling if ( document.createEvent ) { - var e = document.createEvent( "MouseEvents" ); + var e = document.createEvent( 'MouseEvents' ); e.initEvent( "click", true, true ); $jq[0].dispatchEvent(e); } @@ -323,7 +317,15 @@ test("bind/delegate bubbling, isDefaultPrevented (Bug #7793)", function() { e.preventDefault(); }); $main.delegate("#foo", "click", function(e) { - equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" ); + var orig = e.originalEvent; + + if ( typeof(orig.defaultPrevented) === "boolean" || typeof(orig.returnValue) === "boolean" || orig.getPreventDefault ) { + equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" ); + + } else { + // Opera < 11 doesn't implement any interface we can use, so give it a pass + ok( true, "isDefaultPrevented not supported by this browser, test skipped" ); + } }); fakeClick( $anchor2 ); $anchor2.unbind( "click" ); @@ -359,38 +361,47 @@ test("bind(), trigger change on select", function() { }).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 () { @@ -398,7 +409,7 @@ test("bind(), namespaced events, cloned events", function() { }).trigger("tester"); // Make sure events stick with appendTo'd elements (which are cloned) #2027 - jQuery("test").click(function(){ return false; }).appendTo("p"); + jQuery("test").click(function(){ return false; }).appendTo("#main"); ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" ); }); @@ -463,7 +474,7 @@ test("bind(), multi-namespaced events", function() { test("bind(), with same function", function() { expect(2) - var count = 0 , func = function(){ + var count = 0, func = function(){ count++; }; @@ -540,7 +551,7 @@ test("bind(name, false), unbind(name, false)", function() { }); test("bind()/trigger()/unbind() on plain object", function() { - expect( 8 ); + expect( 7 ); var obj = {}; @@ -562,7 +573,6 @@ test("bind()/trigger()/unbind() on plain object", function() { 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." ); @@ -580,7 +590,7 @@ test("bind()/trigger()/unbind() on plain object", function() { jQuery(obj).unbind("test"); equals( obj && obj[ jQuery.expando ] && - obj[ jQuery.expando ][ jQuery.expando ] && + obj[ jQuery.expando ][ jQuery.expando ] && obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" ); }); @@ -605,18 +615,18 @@ 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() { @@ -1445,6 +1455,8 @@ test("live with change", function(){ }); test("live with submit", function() { + expect(5); + var count1 = 0, count2 = 0; jQuery("#testForm").live("submit", function(ev) { @@ -1461,7 +1473,16 @@ test("live with submit", function() { 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"); }); @@ -1945,26 +1966,6 @@ test("window resize", function() { 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(""), order = 0; - - input.prependTo("body"); - - 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[0].focus(); - input.remove(); - - jQuery("body").unbind("focusin.focusinBubblesTest"); -}); - /* test("jQuery(function($) {})", function() { stop();