2015-08-16 06:06:13 +00:00
|
|
|
QUnit.module( "ready" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
( function() {
|
2013-08-29 20:18:17 +00:00
|
|
|
var notYetReady, noEarlyExecution,
|
2020-01-27 17:54:47 +00:00
|
|
|
whenified = jQuery.when && jQuery.when( jQuery.ready ),
|
2016-01-19 19:47:52 +00:00
|
|
|
promisified = Promise.resolve( jQuery.ready ),
|
2016-09-25 04:12:20 +00:00
|
|
|
start = new Date(),
|
2013-08-29 20:18:17 +00:00
|
|
|
order = [],
|
|
|
|
args = {};
|
|
|
|
|
|
|
|
notYetReady = !jQuery.isReady;
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.isReady", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
args[ testId ] = arg;
|
2013-08-29 20:18:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:47:52 +00:00
|
|
|
function throwError( num ) {
|
|
|
|
|
|
|
|
// Not a global QUnit failure
|
|
|
|
var onerror = window.onerror;
|
|
|
|
window.onerror = function() {
|
|
|
|
window.onerror = onerror;
|
|
|
|
};
|
|
|
|
|
|
|
|
throw new Error( "Ready error " + num );
|
|
|
|
}
|
|
|
|
|
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" ) );
|
2016-04-26 14:14:53 +00:00
|
|
|
jQuery.ready.then( makeHandler( "c" ) );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2016-04-26 14:14:53 +00:00
|
|
|
// Throw in some errors
|
2016-01-19 19:47:52 +00:00
|
|
|
jQuery( function() {
|
|
|
|
throwError( 1 );
|
|
|
|
} );
|
|
|
|
jQuery( function() {
|
|
|
|
throwError( 2 );
|
|
|
|
} );
|
|
|
|
|
2016-04-26 14:14:53 +00:00
|
|
|
// Bind again to ensure that the errors didn't lock everything up
|
2016-01-19 19:47:52 +00:00
|
|
|
jQuery( makeHandler( "d" ) );
|
|
|
|
jQuery( document ).ready( makeHandler( "e" ) );
|
2016-04-26 14:14:53 +00:00
|
|
|
jQuery.ready.then( 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-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery ready", function( assert ) {
|
2016-04-26 14:14:53 +00:00
|
|
|
assert.expect( 10 );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2015-08-16 03:45:28 +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.
|
2016-01-19 19:47:52 +00:00
|
|
|
assert.deepEqual( order, [ "a", "b", "c", "d", "e", "f" ],
|
2016-04-26 14:14:53 +00:00
|
|
|
"Bound DOM ready handlers should execute in bind order" );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
|
|
|
// Ensure handler argument is correct.
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +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 = [];
|
|
|
|
|
2016-01-19 19:47:52 +00:00
|
|
|
// Now that the ready event has fired, again bind to the ready event.
|
|
|
|
// These ready handlers should execute asynchronously.
|
|
|
|
var done = assert.async();
|
2015-05-06 17:47:07 +00:00
|
|
|
jQuery( makeHandler( "g" ) );
|
|
|
|
jQuery( document ).ready( makeHandler( "h" ) );
|
2016-04-26 14:14:53 +00:00
|
|
|
jQuery.ready.then( makeHandler( "i" ) );
|
2016-01-19 19:47:52 +00:00
|
|
|
window.setTimeout( function() {
|
2016-04-26 14:14:53 +00:00
|
|
|
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" );
|
2016-01-19 19:47:52 +00:00
|
|
|
|
2016-04-26 14:14:53 +00:00
|
|
|
assert.equal( order.shift(), "h",
|
|
|
|
"Event handler should execute immediately, but async" );
|
2016-01-19 19:47:52 +00:00
|
|
|
assert.equal( args.h, jQuery,
|
|
|
|
"Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" );
|
2016-04-26 14:14:53 +00:00
|
|
|
|
|
|
|
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" );
|
|
|
|
|
2016-01-19 19:47:52 +00:00
|
|
|
done();
|
|
|
|
} );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-08-29 20:18:17 +00:00
|
|
|
|
2020-01-27 17:54:47 +00:00
|
|
|
QUnit[ jQuery.when ? "test" : "skip" ]( "jQuery.when(jQuery.ready)", function( assert ) {
|
2016-04-26 14:14:53 +00:00
|
|
|
assert.expect( 2 );
|
2020-03-02 21:15:06 +00:00
|
|
|
var done = assert.async( 2 );
|
2016-04-26 14:14:53 +00:00
|
|
|
|
|
|
|
whenified.then( function() {
|
|
|
|
assert.ok( jQuery.isReady, "jQuery.when Deferred resolved" );
|
2020-03-02 21:15:06 +00:00
|
|
|
done();
|
2016-04-26 14:14:53 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
jQuery.when( jQuery.ready ).then( function() {
|
|
|
|
assert.ok( jQuery.isReady, "jQuery.when Deferred resolved" );
|
2020-03-02 21:15:06 +00:00
|
|
|
done();
|
2016-04-26 14:14:53 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2016-01-19 19:47:52 +00:00
|
|
|
QUnit.test( "Promise.resolve(jQuery.ready)", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2020-03-02 21:15:06 +00:00
|
|
|
var done = assert.async( 2 );
|
2016-01-19 19:47:52 +00:00
|
|
|
|
|
|
|
promisified.then( function() {
|
|
|
|
assert.ok( jQuery.isReady, "Native promised resolved" );
|
2020-03-02 21:15:06 +00:00
|
|
|
done();
|
2016-01-19 19:47:52 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
Promise.resolve( jQuery.ready ).then( function() {
|
|
|
|
assert.ok( jQuery.isReady, "Native promised resolved" );
|
2020-03-02 21:15:06 +00:00
|
|
|
done();
|
2016-01-19 19:47:52 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( "Error in ready callback does not halt all future executions (gh-1823)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
var done = assert.async();
|
|
|
|
|
|
|
|
jQuery( function() {
|
|
|
|
throwError( 3 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
jQuery( function() {
|
|
|
|
assert.ok( true, "Subsequent handler called" );
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
2016-09-25 04:12:20 +00:00
|
|
|
|
2020-01-27 17:54:47 +00:00
|
|
|
// jQuery.holdReady is deprecated, skip the test if it was excluded.
|
|
|
|
if ( jQuery.holdReady ) {
|
|
|
|
testIframe(
|
|
|
|
"holdReady test needs to be a standalone test since it deals with DOM ready",
|
|
|
|
"readywait.html",
|
|
|
|
function( assert, jQuery, window, document, releaseCalled ) {
|
|
|
|
assert.expect( 2 );
|
|
|
|
var now = new Date();
|
|
|
|
assert.ok( now - start >= 300, "Needs to have waited at least half a second" );
|
|
|
|
assert.ok( releaseCalled, "The release function was called, which resulted in ready" );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-09-25 04:12:20 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} )();
|