( function() {
var notYetReady, noEarlyExecution,
+ whenified = jQuery.when( jQuery.ready ),
promisified = Promise.resolve( jQuery.ready ),
order = [],
args = {};
// Bind to the ready event in every possible way.
jQuery( makeHandler( "a" ) );
jQuery( document ).ready( makeHandler( "b" ) );
+ jQuery.ready.then( makeHandler( "c" ) );
- // Throw in an error to ensure other callbacks are called
+ // Throw in some errors
jQuery( function() {
throwError( 1 );
} );
-
- // Throw two errors in a row
jQuery( function() {
throwError( 2 );
} );
- jQuery.when( jQuery.ready ).done( makeHandler( "c" ) );
- // Do it twice, just to be sure.
+ // Bind again to ensure that the errors didn't lock everything up
jQuery( makeHandler( "d" ) );
jQuery( document ).ready( makeHandler( "e" ) );
- jQuery.when( jQuery.ready ).done( makeHandler( "f" ) );
+ jQuery.ready.then( makeHandler( "f" ) );
noEarlyExecution = order.length === 0;
// This assumes that QUnit tests are run on DOM ready!
QUnit.test( "jQuery ready", function( assert ) {
- assert.expect( 8 );
+ assert.expect( 10 );
assert.ok( noEarlyExecution,
"Handlers bound to DOM ready should not execute before DOM ready" );
// Ensure execution order.
assert.deepEqual( order, [ "a", "b", "c", "d", "e", "f" ],
- "Bound DOM ready handlers should execute in on-order" );
+ "Bound DOM ready handlers should execute in bind order" );
// Ensure handler argument is correct.
assert.equal( args.a, jQuery,
var done = assert.async();
jQuery( makeHandler( "g" ) );
jQuery( document ).ready( makeHandler( "h" ) );
+ jQuery.ready.then( makeHandler( "i" ) );
window.setTimeout( function() {
- assert.equal( order.shift(), "g", "Event handler should execute immediately, but async" );
- assert.equal( args.g, jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery" );
+ assert.equal( order.shift(), "g",
+ "Event handler should execute immediately, but async" );
+ assert.equal( args.g, jQuery,
+ "Argument passed to fn in jQuery( fn ) should be jQuery" );
- assert.equal( order.shift(), "h", "Event handler should execute immediately, but async" );
+ assert.equal( order.shift(), "h",
+ "Event handler should execute immediately, but async" );
assert.equal( args.h, jQuery,
"Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" );
+
+ assert.equal( order.shift(), "i",
+ "Event handler should execute immediately, but async" );
+ assert.equal( args.h, jQuery,
+ "Argument passed to fn in jQuery.ready.then( fn ) should be jQuery" );
+
done();
} );
} );
+ QUnit.test( "jQuery.when(jQuery.ready)", function( assert ) {
+ assert.expect( 2 );
+ var done = jQuery.map( new Array( 2 ), function() { return assert.async(); } );
+
+ whenified.then( function() {
+ assert.ok( jQuery.isReady, "jQuery.when Deferred resolved" );
+ done.pop()();
+ } );
+
+ jQuery.when( jQuery.ready ).then( function() {
+ assert.ok( jQuery.isReady, "jQuery.when Deferred resolved" );
+ done.pop()();
+ } );
+ } );
+
QUnit.test( "Promise.resolve(jQuery.ready)", function( assert ) {
assert.expect( 2 );
var done = jQuery.map( new Array( 2 ), function() { return assert.async(); } );