Tests: Weaken sync-assumption from jQuery.when to jQuery.ready.then

This commit is contained in:
Richard Gibson 2016-04-26 10:14:53 -04:00
parent 305f193aa5
commit f496182216

View File

@ -2,6 +2,7 @@ QUnit.module( "ready" );
( function() { ( function() {
var notYetReady, noEarlyExecution, var notYetReady, noEarlyExecution,
whenified = jQuery.when( jQuery.ready ),
promisified = Promise.resolve( jQuery.ready ), promisified = Promise.resolve( jQuery.ready ),
order = [], order = [],
args = {}; args = {};
@ -41,35 +42,33 @@ QUnit.module( "ready" );
// Bind to the ready event in every possible way. // Bind to the ready event in every possible way.
jQuery( makeHandler( "a" ) ); jQuery( makeHandler( "a" ) );
jQuery( document ).ready( makeHandler( "b" ) ); 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() { jQuery( function() {
throwError( 1 ); throwError( 1 );
} ); } );
// Throw two errors in a row
jQuery( function() { jQuery( function() {
throwError( 2 ); 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( makeHandler( "d" ) );
jQuery( document ).ready( makeHandler( "e" ) ); jQuery( document ).ready( makeHandler( "e" ) );
jQuery.when( jQuery.ready ).done( makeHandler( "f" ) ); jQuery.ready.then( makeHandler( "f" ) );
noEarlyExecution = order.length === 0; noEarlyExecution = order.length === 0;
// This assumes that QUnit tests are run on DOM ready! // This assumes that QUnit tests are run on DOM ready!
QUnit.test( "jQuery ready", function( assert ) { QUnit.test( "jQuery ready", function( assert ) {
assert.expect( 8 ); assert.expect( 10 );
assert.ok( noEarlyExecution, assert.ok( noEarlyExecution,
"Handlers bound to DOM ready should not execute before DOM ready" ); "Handlers bound to DOM ready should not execute before DOM ready" );
// Ensure execution order. // Ensure execution order.
assert.deepEqual( order, [ "a", "b", "c", "d", "e", "f" ], 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. // Ensure handler argument is correct.
assert.equal( args.a, jQuery, assert.equal( args.a, jQuery,
@ -84,17 +83,42 @@ QUnit.module( "ready" );
var done = assert.async(); var done = assert.async();
jQuery( makeHandler( "g" ) ); jQuery( makeHandler( "g" ) );
jQuery( document ).ready( makeHandler( "h" ) ); jQuery( document ).ready( makeHandler( "h" ) );
jQuery.ready.then( makeHandler( "i" ) );
window.setTimeout( function() { window.setTimeout( function() {
assert.equal( order.shift(), "g", "Event handler should execute immediately, but async" ); assert.equal( order.shift(), "g",
assert.equal( args.g, jQuery, "Argument passed to fn in jQuery( fn ) should be jQuery" ); "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, assert.equal( args.h, jQuery,
"Argument passed to fn in jQuery(document).ready( fn ) should be 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(); 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 ) { QUnit.test( "Promise.resolve(jQuery.ready)", function( assert ) {
assert.expect( 2 ); assert.expect( 2 );
var done = jQuery.map( new Array( 2 ), function() { return assert.async(); } ); var done = jQuery.map( new Array( 2 ), function() { return assert.async(); } );