X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fevent.js;h=cbeb9ff0011c9f59650c4b1c653b4a14bb9094db;hb=d12e8a34e6d96f98ac4f9408a0d61a9ed373f917;hp=642ba5e5d0b18e598f608a865717ad1ff7cb362a;hpb=77344f4c50386690782ab8ddc92898a918f54f5c;p=jquery.git diff --git a/test/unit/event.js b/test/unit/event.js index 642ba5e..cbeb9ff 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -166,6 +166,72 @@ test("bind(), multi-namespaced events", function() { jQuery("#firstp").trigger("custom"); }); +test("unbind(type)", function() { + expect( 0 ); + + var $elem = jQuery("#firstp"), + message; + + function error(){ + ok( false, message ); + } + + message = "unbind passing function"; + $elem.bind('error', error).unbind('error',error).triggerHandler('error'); + + message = "unbind all from event"; + $elem.bind('error', error).unbind('error').triggerHandler('error'); + + message = "unbind all"; + $elem.bind('error', error).unbind().triggerHandler('error'); + + message = "unbind many with function"; + $elem.bind('error error2',error) + .unbind('error error2', error ) + .trigger('error').triggerHandler('error2'); + + message = "unbind many"; // #3538 + $elem.bind('error error2',error) + .unbind('error error2') + .trigger('error').triggerHandler('error2'); +}); + +test("unbind(eventObject)", function() { + expect(4); + + var $elem = jQuery("#firstp"), + num; + + function assert( expected ){ + num = 0; + $elem.trigger('foo').triggerHandler('bar'); + equals( num, expected, "Check the right handlers are triggered" ); + } + + $elem + // This handler shouldn't be unbound + .bind('foo', function(){ + num += 1; + }) + .bind('foo', function(e){ + $elem.unbind( e ) + num += 2; + }) + // Neither this one + .bind('bar', function(){ + num += 4; + }); + + assert( 7 ); + assert( 5 ); + + $elem.unbind('bar'); + assert( 1 ); + + $elem.unbind(); + assert( 0 ); +}); + test("trigger() shortcuts", function() { expect(6); jQuery('
foo
').appendTo( $parent ); + + var event = jQuery.Event("noNew"); + ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" ); + equals( event.type, "noNew", "Verify its type" ); + + equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" ); + equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" ); + equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" ); + + event.preventDefault(); + equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" ); + event.stopPropagation(); + equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" ); + + event.isPropagationStopped = function(){ return false }; + event.stopImmediatePropagation(); + equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" ); + equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" ); + + $parent.bind('foo',function(e){ + // Tries bubbling + equals( e.type, 'foo', 'Verify event type when passed passing an event object' ); + equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' ); + equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' ); + equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' ); + }); + + // test with an event object + event = new jQuery.Event("foo"); + event.secret = 'boo!'; + $child.trigger(event); + + // test with a literal object + $child.trigger({type:'foo', secret:'boo!'}); + + $parent.unbind(); - // have the extra handler leave the return value alone - // Triggers 9 - equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" ); + function error(){ + ok( false, "This assertion shouldn't be reached"); + } + + $parent.bind('foo', error ); + + $child.bind('foo',function(e, a, b, c ){ + equals( arguments.length, 4, "Check arguments length"); + equals( a, 1, "Check first custom argument"); + equals( b, 2, "Check second custom argument"); + equals( c, 3, "Check third custom argument"); + + equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" ); + equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" ); + equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" ); + + // Skips both errors + e.stopImmediatePropagation(); + + return "result"; + }); + + // We should add this back in when we want to test the order + // in which event handlers are iterated. + //$child.bind('foo', error ); + + event = new jQuery.Event("foo"); + $child.trigger( event, [1,2,3] ).unbind(); + equals( event.result, "result", "Check event.result attribute"); + + // Will error if it bubbles + $child.triggerHandler('foo'); + + $child.unbind(); + $parent.unbind().remove(); }); test("toggle(Function, Function, ...)", function() { @@ -384,6 +472,111 @@ test("toggle(Function, Function, ...)", function() { var data = jQuery.data( $div[0], 'events' ); ok( !data, "Unbinding one function from toggle unbinds them all"); }); + +test(".live()/.die()", function() { + expect(34); + + var submit = 0, div = 0, livea = 0, liveb = 0; + + jQuery("div").live("submit", function(){ submit++; return false; }); + jQuery("div").live("click", function(){ div++; }); + jQuery("div#nothiddendiv").live("click", function(){ livea++; }); + jQuery("div#nothiddendivchild").live("click", function(){ liveb++; }); + + // Nothing should trigger on the body + jQuery("body").trigger("click"); + equals( submit, 0, "Click on body" ); + equals( div, 0, "Click on body" ); + equals( livea, 0, "Click on body" ); + equals( liveb, 0, "Click on body" ); + + // This should trigger two events + jQuery("div#nothiddendiv").trigger("click"); + equals( submit, 0, "Click on div" ); + equals( div, 1, "Click on div" ); + equals( livea, 1, "Click on div" ); + equals( liveb, 0, "Click on div" ); + + // This should trigger three events (w/ bubbling) + jQuery("div#nothiddendivchild").trigger("click"); + equals( submit, 0, "Click on inner div" ); + equals( div, 2, "Click on inner div" ); + equals( livea, 2, "Click on inner div" ); + equals( liveb, 1, "Click on inner div" ); + + // This should trigger one submit + jQuery("div#nothiddendivchild").trigger("submit"); + equals( submit, 1, "Submit on div" ); + equals( div, 2, "Submit on div" ); + equals( livea, 2, "Submit on div" ); + equals( liveb, 1, "Submit on div" ); + + // Make sure no other events were removed in the process + jQuery("div#nothiddendivchild").trigger("click"); + equals( submit, 1, "die Click on inner div" ); + equals( div, 3, "die Click on inner div" ); + equals( livea, 3, "die Click on inner div" ); + equals( liveb, 2, "die Click on inner div" ); + + // Now make sure that the removal works + jQuery("div#nothiddendivchild").die("click"); + jQuery("div#nothiddendivchild").trigger("click"); + equals( submit, 1, "die Click on inner div" ); + equals( div, 4, "die Click on inner div" ); + equals( livea, 4, "die Click on inner div" ); + equals( liveb, 2, "die Click on inner div" ); + + // Make sure that the click wasn't removed too early + jQuery("div#nothiddendiv").trigger("click"); + equals( submit, 1, "die Click on inner div" ); + equals( div, 5, "die Click on inner div" ); + equals( livea, 5, "die Click on inner div" ); + equals( liveb, 2, "die Click on inner div" ); + + jQuery("div#nothiddendiv").die("click"); + jQuery("div").die("click"); + jQuery("div").die("submit"); + + // Verify that return false prevents default action + jQuery("#anchor2").live("click", function(){ return false; }); + var hash = window.location.hash; + jQuery("#anchor2").trigger("click"); + equals( window.location.hash, hash, "return false worked" ); + jQuery("#anchor2").die("click"); + + // Verify that .preventDefault() prevents default action + jQuery("#anchor2").live("click", function(e){ e.preventDefault(); }); + var hash = window.location.hash; + jQuery("#anchor2").trigger("click"); + equals( window.location.hash, hash, "e.preventDefault() worked" ); + jQuery("#anchor2").die("click"); + + // Test binding the same handler to multiple points + var called = 0; + function callback(){ called++; return false; } + + jQuery("#nothiddendiv").live("click", callback); + jQuery("#anchor2").live("click", callback); + + jQuery("#nothiddendiv").trigger("click"); + equals( called, 1, "Verify that only one click occurred." ); + + jQuery("#anchor2").trigger("click"); + equals( called, 2, "Verify that only one click occurred." ); + + // Make sure that only one callback is removed + jQuery("#anchor2").die("click", callback); + + jQuery("#nothiddendiv").trigger("click"); + equals( called, 3, "Verify that only one click occurred." ); + + jQuery("#anchor2").trigger("click"); + equals( called, 3, "Verify that only one click occurred." ); + + // Cleanup + jQuery("#nothiddendiv").die("click", callback); +}); + /* test("jQuery(function($) {})", function() { stop(); @@ -400,4 +593,4 @@ test("event properties", function() { start(); }).click(); }); -*/ \ No newline at end of file +*/