module("event"); test("bind(), with data", function() { expect(3); var handler = function(event) { ok( event.data, "bind() with data, check passed data exists" ); equals( event.data.foo, "bar", "bind() with data, Check value of passed data" ); }; 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" ); equals( event.data.foo, "bar", "Check value of passed data" ); ok( data, "Check trigger data" ); equals( data.bar, "foo", "Check value of trigger data" ); }; 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" ); }; 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]; // // 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" ); }; jQuery("#form select").each(function(i){ jQuery(this).bind('change', i, selectOnChange); }).trigger('change'); }); test("bind(), namespaced events, cloned events", function() { expect(6); jQuery("#firstp").bind("custom.test",function(e){ ok(true, "Custom event triggered"); }); jQuery("#firstp").bind("click",function(e){ ok(true, "Normal click triggered"); }); jQuery("#firstp").bind("click.test",function(e){ ok(true, "Namespaced click triggered"); }); // Trigger both bound fn (2) jQuery("#firstp").trigger("click"); // Trigger one bound fn (1) jQuery("#firstp").trigger("click.test"); // Remove only the one fn jQuery("#firstp").unbind("click.test"); // Trigger the remaining fn (1) 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("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("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(); 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(11); var count = 0, fn1 = function(e) { count++; }, fn2 = function(e) { count--; }, preventDefault = function(e) { e.preventDefault() }, link = jQuery('#mark'); link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click(); 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; jQuery("#simon1").one("click", function() { ok( true, "Execute event only once" ); jQuery(this).toggle(function() { equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); }, function() { 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("