").appendTo("#qunit-fixture"),
cb = markup.find("input")[0];
jQuery(cb).on( "click", function(){
equal( this.checked, false, "just-clicked checkbox is not checked" );
});
markup.on( "click", function(){
equal( cb.checked, false, "checkbox is not checked in bubbled event" );
});
// Native click
cb.checked = true;
equal( cb.checked, true, "native - checkbox is initially checked" );
cb.click();
equal( cb.checked, false, "native - checkbox is no longer checked" );
// jQuery click
cb.checked = true;
equal( cb.checked, true, "jQuery - checkbox is initially checked" );
jQuery( cb ).trigger("click");
equal( cb.checked, false, "jQuery - checkbox is no longer checked" );
// Handlers only; checkbox state remains false
jQuery( cb ).triggerHandler( "click" );
});
test("focus-blur order (#12868)", function() {
expect( 5 );
var order,
$text = jQuery("#text1"),
$radio = jQuery("#radio1").trigger("focus");
// IE6-10 fire focus/blur events asynchronously; this is the resulting mess.
// IE's browser window must be topmost for this to work properly!!
stop();
$radio[0].focus();
setTimeout( function() {
$text
.on( "focus", function(){
equal( order++, 1, "text focus" );
})
.on( "blur", function(){
equal( order++, 0, "text blur" );
});
$radio
.on( "focus", function(){
equal( order++, 1, "radio focus" );
})
.on( "blur", function(){
equal( order++, 0, "radio blur" );
});
// Enabled input getting focus
order = 0;
equal( document.activeElement, $radio[0], "radio has focus" );
$text.trigger("focus");
setTimeout( function() {
equal( document.activeElement, $text[0], "text has focus" );
// Run handlers without native method on an input
order = 1;
$radio.triggerHandler( "focus" );
start();
}, 50 );
}, 50 );
});
test("hover event no longer special since 1.9", function() {
expect( 1 );
jQuery("
craft
")
.on( "hover", function( e ) {
equal( e.type, "hover", "I am hovering!" );
})
.trigger("hover")
.off("hover");
});
test("fixHooks extensions", function() {
expect( 2 );
// IE requires focusable elements to be visible, so append to body
var $fixture = jQuery( "
" ).appendTo( "body" ),
saved = jQuery.event.fixHooks.click;
// Ensure the property doesn't exist
$fixture.on( "click", function( event ) {
ok( !("blurrinessLevel" in event), "event.blurrinessLevel does not exist" );
});
fireNative( $fixture[0], "click" );
$fixture.off( "click" );
jQuery.event.fixHooks.click = {
filter: function( event ) {
event.blurrinessLevel = 42;
return event;
}
};
// Trigger a native click and ensure the property is set
$fixture.on( "click", function( event ) {
equal( event.blurrinessLevel, 42, "event.blurrinessLevel was set" );
});
fireNative( $fixture[0], "click" );
delete jQuery.event.fixHooks.click;
$fixture.off( "click" ).remove();
jQuery.event.fixHooks.click = saved;
});
testIframeWithCallback( "jQuery.ready promise", "event/promiseReady.html", function( isOk ) {
expect(1);
ok( isOk, "$.when( $.ready ) works" );
});
testIframeWithCallback( "Focusing iframe element", "event/focusElem.html", function( isOk ) {
expect(1);
ok( isOk, "Focused an element in an iframe" );
});
// need PHP here to make the incepted IFRAME hang
if ( hasPHP ) {
testIframeWithCallback( "jQuery.ready synchronous load with long loading subresources", "event/syncReady.html", function( isOk ) {
expect(1);
ok( isOk, "jQuery loaded synchronously fires ready when the DOM can truly be interacted with" );
});
}
(function(){
// This code must be run before DOM ready!
var notYetReady, noEarlyExecution,
order = [],
args = {};
notYetReady = !jQuery.isReady;
test("jQuery.isReady", function() {
expect(2);
equal(notYetReady, true, "jQuery.isReady should not be true before DOM ready");
equal(jQuery.isReady, true, "jQuery.isReady should be true once DOM is ready");
});
// Create an event handler.
function makeHandler( testId ) {
// When returned function is executed, push testId onto `order` array
// to ensure execution order. Also, store event handler arg to ensure
// the correct arg is being passed into the event handler.
return function( arg ) {
order.push(testId);
args[testId] = arg;
};
}
// Bind to the ready event in every possible way.
jQuery(makeHandler("a"));
jQuery(document).ready(makeHandler("b"));
jQuery(document).on("ready.readytest", makeHandler("c"));
// Do it twice, just to be sure.
jQuery(makeHandler("d"));
jQuery(document).ready(makeHandler("e"));
jQuery(document).on("ready.readytest", makeHandler("f"));
noEarlyExecution = order.length === 0;
// This assumes that QUnit tests are run on DOM ready!
test("jQuery ready", function() {
expect(10);
ok(noEarlyExecution, "Handlers bound to DOM ready should not execute before DOM ready");
// Ensure execution order.
deepEqual(order, ["a", "b", "d", "e", "c", "f"], "Bound DOM ready handlers should execute in on-order, but those bound with jQuery(document).on( 'ready', fn ) will always execute last");
// Ensure handler argument is correct.
equal(args["a"], jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery");
equal(args["b"], jQuery, "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery");
ok(args["c"] instanceof jQuery.Event, "Argument passed to fn in jQuery(document).on( 'ready', fn ) should be an event object");
order = [];
// Now that the ready event has fired, again bind to the ready event
// in every possible way. These event handlers should execute immediately.
jQuery(makeHandler("g"));
equal(order.pop(), "g", "Event handler should execute immediately");
equal(args["g"], jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery");
jQuery(document).ready(makeHandler("h"));
equal(order.pop(), "h", "Event handler should execute immediately");
equal(args["h"], jQuery, "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery");
jQuery(document).on("ready.readytest", makeHandler("never"));
equal(order.length, 0, "Event handler should never execute since DOM ready has already passed");
// Cleanup.
jQuery(document).off("ready.readytest");
});
})();
test("change handler should be detached from element", function() {
expect( 2 );
var $fixture = jQuery( "
" ).appendTo( "body" ),
originRemoveEvent = jQuery.removeEvent,
wrapperRemoveEvent = function(elem, type, handle){
equal("change", type, "Event handler for 'change' event should be removed");
equal("change-ie-leak", jQuery(elem).attr("id"), "Event handler for 'change' event should be removed from appropriate element");
originRemoveEvent(elem, type, handle);
};
jQuery.removeEvent = wrapperRemoveEvent ;
$fixture.on( "change", function() {});
$fixture.off( "change" );
$fixture.remove();
jQuery.removeEvent = originRemoveEvent;
});
asyncTest("trigger click on checkbox, fires change event", function() {
expect(1);
var check = jQuery("#check2");
check.on( "change", function() {
// get it?
check.off("change");
ok( true, "Change event fired as a result of triggered click" );
start();
}).trigger("click");
});
test( "Namespace preserved when passed an Event (#12739)", function() {
expect( 4 );
var markup = jQuery(
"
"
),
triggered = 0,
fooEvent;
markup.find("div")
.addBack()
.on( "foo.bar", function( e ) {
if ( !e.handled ) {
triggered++;
e.handled = true;
equal( e.namespace, "bar", "namespace is bar" );
jQuery( e.target ).find("div").each(function() {
jQuery( this ).triggerHandler( e );
});
}
})
.on( "foo.bar2", function() {
ok( false, "foo.bar2 called on trigger " + triggered + " id " + this.id );
});
markup.trigger("foo.bar");
markup.trigger( jQuery.Event("foo.bar") );
fooEvent = jQuery.Event("foo");
fooEvent.namespace = "bar";
markup.trigger( fooEvent );
markup.remove();
equal( triggered, 3, "foo.bar triggered" );
});
test( "make sure events cloned correctly", 18, function() {
var clone,
fixture = jQuery("#qunit-fixture"),
checkbox = jQuery("#check1"),
p = jQuery("#firstp");
fixture.on( "click change", function( event, result ) {
ok( result, event.type + " on original element is fired" );
}).on( "click", "#firstp", function( event, result ) {
ok( result, "Click on original child element though delegation is fired" );
}).on( "change", "#check1", function( event, result ) {
ok( result, "Change on original child element though delegation is fired" );
});
p.on("click", function() {
ok( true, "Click on original child element is fired" );
});
checkbox.on("change", function() {
ok( true, "Change on original child element is fired" );
});
fixture.clone().trigger("click").trigger("change"); // 0 events should be fired
clone = fixture.clone( true );
clone.find("p").eq(0).trigger( "click", true ); // 3 events should fire
clone.find("#check1").trigger( "change", true ); // 3 events should fire
clone.remove();
clone = fixture.clone( true, true );
clone.find("p").eq(0).trigger( "click", true ); // 3 events should fire
clone.find("#check1").trigger( "change", true ); // 3 events should fire
fixture.off();
p.off();
checkbox.off();
p.trigger("click"); // 0 should be fired
checkbox.trigger("change"); // 0 should be fired
clone.find("p").eq(0).trigger( "click", true ); // 3 events should fire
clone.find("#check1").trigger( "change", true ); // 3 events should fire
clone.remove();
clone.find("p").eq(0).trigger("click"); // 0 should be fired
clone.find("#check1").trigger("change"); // 0 events should fire
});
test( "Check order of focusin/focusout events", 2, function() {
var focus, blur,
input = jQuery("#name");
input.on("focus", function() {
focus = true;
}).on("focusin", function() {
ok( !focus, "Focusin event should fire before focus does" );
}).on("blur", function() {
blur = true;
}).on("focusout", function() {
ok( !blur, "Focusout event should fire before blur does" );
});
// gain focus
input.trigger("focus");
// then lose it
jQuery("#search").trigger("focus");
// cleanup
input.off();
});
test( "String.prototype.namespace does not cause trigger() to throw (#13360)", function() {
expect( 1 );
var errored = false;
String.prototype.namespace = function() {};
try {
jQuery("
").trigger("foo.bar");
} catch( e ) {
errored = true;
}
equal( errored, false, "trigger() did not throw exception" );
delete String.prototype.namespace;
});