X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fevent.js;h=cbeb9ff0011c9f59650c4b1c653b4a14bb9094db;hb=d12e8a34e6d96f98ac4f9408a0d61a9ed373f917;hp=334f286c78d37d78d64214780d934dae2bb780b7;hpb=6728e3cf74d02794113deb0af70a5f9a7caa2241;p=jquery.git diff --git a/test/unit/event.js b/test/unit/event.js index 334f286..cbeb9ff 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -1,122 +1,305 @@ module("event"); -test("bind()", function() { - expect(15); - +test("bind(), with data", function() { + expect(3); var handler = function(event) { ok( event.data, "bind() with data, check passed data exists" ); - ok( event.data.foo == "bar", "bind() with data, Check value of passed data" ); + equals( event.data.foo, "bar", "bind() with data, Check value of passed data" ); }; - $("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler); - - ok( !$("#firstp").get(0).$events, "Event handler unbound when using data." ); - - reset(); + jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler); + + ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." ); +}); + +test("bind(), with data, trigger with data", function() { + expect(4); var handler = function(event, data) { ok( event.data, "check passed data exists" ); - ok( event.data.foo == "bar", "Check value of passed data" ); + equals( event.data.foo, "bar", "Check value of passed data" ); ok( data, "Check trigger data" ); - ok( data.bar == "foo", "Check value of trigger data" ); + equals( data.bar, "foo", "Check value of trigger data" ); }; - $("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind(handler); - - reset(); + jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler); +}); + +test("bind(), multiple events at once", function() { + expect(2); + var clickCounter = 0, + mouseoverCounter = 0; + var handler = function(event) { + if (event.type == "click") + clickCounter += 1; + else if (event.type == "mouseover") + mouseoverCounter += 1; + }; + jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover"); + equals( clickCounter, 1, "bind() with multiple events at once" ); + equals( mouseoverCounter, 1, "bind() with multiple events at once" ); +}); + +test("bind(), no data", function() { + expect(1); var handler = function(event) { ok ( !event.data, "Check that no data is added to the event object" ); }; - $("#firstp").bind("click", handler).trigger("click"); - - + jQuery("#firstp").bind("click", handler).trigger("click"); +}); + +test("bind(), iframes", function() { // events don't work with iframes, see #939 - this test fails in IE because of contentDocument // var doc = document.getElementById("iframe").contentDocument; // // doc.body.innerHTML = ""; - // + // // var input = doc.getElementsByTagName("input")[0]; - // - // $(input).bind("click",function() { + // + // jQuery(input).bind("click",function() { // ok( true, "Binding to element inside iframe" ); // }).click(); - +}); + +test("bind(), trigger change on select", function() { + expect(3); var counter = 0; function selectOnChange(event) { equals( event.data, counter++, "Event.data is not a global event object" ); }; - $("#form select").each(function(i){ - $(this).bind('change', i, selectOnChange); + jQuery("#form select").each(function(i){ + jQuery(this).bind('change', i, selectOnChange); }).trigger('change'); +}); - reset(); +test("bind(), namespaced events, cloned events", function() { + expect(6); - $("#firstp").bind("click",function(e){ + jQuery("#firstp").bind("custom.test",function(e){ + ok(true, "Custom event triggered"); + }); + + jQuery("#firstp").bind("click",function(e){ ok(true, "Normal click triggered"); - }); + }); - $("#firstp").bind("click.test",function(e){ + jQuery("#firstp").bind("click.test",function(e){ ok(true, "Namespaced click triggered"); - }); + }); // Trigger both bound fn (2) - $("#firstp").trigger("click"); + jQuery("#firstp").trigger("click"); // Trigger one bound fn (1) - $("#firstp").trigger("click.test"); + jQuery("#firstp").trigger("click.test"); // Remove only the one fn - $("#firstp").unbind("click.test"); + jQuery("#firstp").unbind("click.test"); // Trigger the remaining fn (1) - $("#firstp").trigger("click"); + jQuery("#firstp").trigger("click"); + + // Remove the remaining fn + jQuery("#firstp").unbind(".test"); + + // Trigger the remaining fn (0) + jQuery("#firstp").trigger("custom"); + + // using contents will get comments regular, text, and comment nodes + jQuery("#nonnodes").contents().bind("tester", function () { + equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" ); + }).trigger("tester"); + + // Make sure events stick with appendTo'd elements (which are cloned) #2027 + jQuery("test").click(function(){ return false; }).appendTo("p"); + ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" ); }); -test("click()", function() { +test("bind(), multi-namespaced events", function() { + expect(6); + + var order = [ + "click.test.abc", + "click.test.abc", + "click.test", + "click.test.abc", + "click.test", + "custom.test2" + ]; + + function check(name, msg){ + same(name, order.shift(), msg); + } + + jQuery("#firstp").bind("custom.test",function(e){ + check("custom.test", "Custom event triggered"); + }); + + jQuery("#firstp").bind("custom.test2",function(e){ + check("custom.test2", "Custom event triggered"); + }); + + jQuery("#firstp").bind("click.test",function(e){ + check("click.test", "Normal click triggered"); + }); + + jQuery("#firstp").bind("click.test.abc",function(e){ + check("click.test.abc", "Namespaced click triggered"); + }); + + // Trigger both bound fn (1) + jQuery("#firstp").trigger("click.test.abc"); + + // Trigger one bound fn (1) + jQuery("#firstp").trigger("click.abc"); + + // Trigger two bound fn (2) + jQuery("#firstp").trigger("click.test"); + + // Remove only the one fn + jQuery("#firstp").unbind("click.abc"); + + // Trigger the remaining fn (1) + jQuery("#firstp").trigger("click"); + + // Remove the remaining fn + jQuery("#firstp").unbind(".test"); + + // Trigger the remaining fn (1) + 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); - $('
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(); - // Trigger only the handlers (no native) and extra fn, with external event obj - // Triggers 9 - equals( $("#firstp").triggerHandler("foo", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" ); + 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() { - expect(4); +test("toggle(Function, Function, ...)", function() { + expect(11); + var count = 0, fn1 = function(e) { count++; }, fn2 = function(e) { count--; }, preventDefault = function(e) { e.preventDefault() }, - link = $('#mark'); + link = jQuery('#mark'); link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click(); - ok( count == 1, "Check for toggle(fn, fn)" ); - + equals( count, 1, "Check for toggle(fn, fn)" ); + + jQuery("#firstp").toggle(function () { + equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" ) + }, function() {}).trigger("click", [ 1, 2, 3 ]); + var first = 0; - $("#simon1").one("click", function() { + jQuery("#simon1").one("click", function() { ok( true, "Execute event only once" ); - $(this).toggle(function() { - ok( first++ == 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); + jQuery(this).toggle(function() { + equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); }, function() { - ok( first == 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); + equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); }); return false; }).click().click().click(); + + var turn = 0; + var fns = [ + function(){ + turn = 1; + }, + function(){ + turn = 2; + }, + function(){ + turn = 3; + } + ]; + + var $div = jQuery("