2015-09-03 01:03:44 +00:00
QUnit . module ( "ready" ) ;
2013-08-29 20:18:17 +00:00
2015-12-22 12:26:56 +00:00
( function ( ) {
2013-08-29 20:18:17 +00:00
var notYetReady , noEarlyExecution ,
order = [ ] ,
args = { } ;
notYetReady = ! jQuery . isReady ;
2015-09-03 01:46:31 +00:00
QUnit . test ( "jQuery.isReady" , function ( assert ) {
assert . expect ( 2 ) ;
2013-08-29 20:18:17 +00:00
2015-09-03 01:46:31 +00:00
assert . equal ( notYetReady , true , "jQuery.isReady should not be true before DOM ready" ) ;
assert . 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 ) {
// 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-12-22 12:26:56 +00:00
jQuery ( makeHandler ( "a" ) ) ;
jQuery ( document ) . ready ( makeHandler ( "b" ) ) ;
jQuery ( document ) . on ( "ready.readytest" , makeHandler ( "c" ) ) ;
2013-08-29 20:18:17 +00:00
// Do it twice, just to be sure.
2015-12-22 12:26:56 +00:00
jQuery ( makeHandler ( "d" ) ) ;
jQuery ( document ) . ready ( makeHandler ( "e" ) ) ;
jQuery ( document ) . on ( "ready.readytest" , makeHandler ( "f" ) ) ;
2013-08-29 20:18:17 +00:00
noEarlyExecution = order . length === 0 ;
// This assumes that QUnit tests are run on DOM ready!
2015-12-22 13:00:15 +00:00
QUnit . test ( "jQuery ready" , function ( ) {
2015-12-22 12:26:56 +00:00
expect ( 10 ) ;
2013-08-29 20:18:17 +00:00
2015-12-22 12:26:56 +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-12-22 12:26:56 +00:00
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" ) ;
2013-08-29 20:18:17 +00:00
// Ensure handler argument is correct.
2015-12-22 12:26:56 +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" ) ;
ok ( args [ "c" ] instanceof jQuery . Event , "Argument passed to fn in jQuery(document).on( 'ready', fn ) should be an event object" ) ;
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-12-22 12:26:56 +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" ) ;
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" ) ;
} ) ;
2013-08-29 20:18:17 +00:00
2015-09-02 23:52:01 +00:00
} ) ( ) ;