2015-05-06 17:47:07 +00:00
|
|
|
module( "event" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
( function() {
|
2013-08-29 20:18:17 +00:00
|
|
|
var notYetReady, noEarlyExecution,
|
|
|
|
order = [],
|
|
|
|
args = {};
|
|
|
|
|
|
|
|
notYetReady = !jQuery.isReady;
|
|
|
|
|
2015-05-06 17:47:07 +00:00
|
|
|
test( "jQuery.isReady", function() {
|
|
|
|
expect( 2 );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-05-06 17:47:07 +00:00
|
|
|
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" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
// Create an event handler.
|
|
|
|
function makeHandler( testId ) {
|
2015-05-06 17:47:07 +00:00
|
|
|
|
2013-08-29 20:18:17 +00:00
|
|
|
// 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 ) {
|
2015-05-06 17:47:07 +00:00
|
|
|
order.push( testId );
|
2015-09-02 23:52:01 +00:00
|
|
|
args[ testId ] = arg;
|
2013-08-29 20:18:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind to the ready event in every possible way.
|
2015-05-06 17:47:07 +00:00
|
|
|
jQuery( makeHandler( "a" ) );
|
|
|
|
jQuery( document ).ready( makeHandler( "b" ) );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
// Do it twice, just to be sure.
|
2015-05-06 17:47:07 +00:00
|
|
|
jQuery( makeHandler( "c" ) );
|
|
|
|
jQuery( document ).ready( makeHandler( "d" ) );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
noEarlyExecution = order.length === 0;
|
|
|
|
|
|
|
|
// This assumes that QUnit tests are run on DOM ready!
|
2015-05-06 17:47:07 +00:00
|
|
|
test( "jQuery ready", function() {
|
|
|
|
expect( 8 );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-05-06 17:47:07 +00:00
|
|
|
ok( noEarlyExecution,
|
|
|
|
"Handlers bound to DOM ready should not execute before DOM ready" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
// Ensure execution order.
|
2015-05-06 17:47:07 +00:00
|
|
|
deepEqual( order, [ "a", "b", "c", "d" ],
|
|
|
|
"Bound DOM ready handlers should execute in on-order" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
// Ensure handler argument is correct.
|
2015-05-06 17:47:07 +00:00
|
|
|
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" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
order = [];
|
|
|
|
|
|
|
|
// Now that the ready event has fired, again bind to the ready event
|
|
|
|
// in every possible way. These event handlers should execute immediately.
|
2015-05-06 17:47:07 +00:00
|
|
|
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" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} )();
|