2015-09-03 01:03:44 +00:00
|
|
|
QUnit.module( "ready" );
|
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-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 ) {
|
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-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery ready", function( assert ) {
|
|
|
|
assert.expect( 8 );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( noEarlyExecution,
|
2015-05-06 17:47:07 +00:00
|
|
|
"Handlers bound to DOM ready should not execute before DOM ready" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
// Ensure execution order.
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.deepEqual( order, [ "a", "b", "c", "d" ],
|
2015-05-06 17:47:07 +00:00
|
|
|
"Bound DOM ready handlers should execute in on-order" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
// Ensure handler argument is correct.
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( args.a, jQuery,
|
2015-05-06 17:47:07 +00:00
|
|
|
"Argument passed to fn in jQuery( fn ) should be jQuery" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( args.b, jQuery,
|
2015-05-06 17:47:07 +00:00
|
|
|
"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" ) );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( order.pop(), "g", "Event handler should execute immediately" );
|
|
|
|
assert.equal( args.g, jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery" );
|
2015-05-06 17:47:07 +00:00
|
|
|
|
|
|
|
jQuery( document ).ready( makeHandler( "h" ) );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( order.pop(), "h", "Event handler should execute immediately" );
|
|
|
|
assert.equal( args.h, jQuery,
|
2015-05-06 17:47:07 +00:00
|
|
|
"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
|
|
|
} )();
|