2015-08-16 03:45:28 +00:00
|
|
|
QUnit.module( "deferred", {
|
2019-02-18 18:02:38 +00:00
|
|
|
afterEach: moduleTeardown
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2020-01-27 17:54:47 +00:00
|
|
|
( function() {
|
|
|
|
|
2022-07-12 15:12:27 +00:00
|
|
|
if ( !includesModule( "deferred" ) ) {
|
2020-01-27 17:54:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
|
|
|
|
|
|
|
|
function createDeferred( fn ) {
|
|
|
|
return withNew ? new jQuery.Deferred( fn ) : jQuery.Deferred( fn );
|
|
|
|
}
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred" + withNew, function( assert ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 23 );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
var defer = createDeferred();
|
|
|
|
|
2017-12-13 06:43:30 +00:00
|
|
|
assert.ok( typeof defer.pipe === "function", "defer.pipe is a function" );
|
2012-04-01 23:29:39 +00:00
|
|
|
|
2016-04-15 21:13:59 +00:00
|
|
|
defer.resolve().done( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Success on resolve" );
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( defer.state(), "resolved", "Deferred is resolved (state)" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).fail( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "Error on resolve" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).always( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Always callback on resolve" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2016-04-15 21:13:59 +00:00
|
|
|
defer = createDeferred();
|
|
|
|
defer.reject().done( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "Success on reject" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).fail( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Error on reject" );
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( defer.state(), "rejected", "Deferred is rejected (state)" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).always( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Always callback on reject" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
createDeferred( function( defer ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( this === defer, "Defer passed as this & first argument" );
|
2015-08-16 06:59:58 +00:00
|
|
|
this.resolve( "done" );
|
|
|
|
} ).done( function( value ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( value, "done", "Passed function executed" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
createDeferred( function( defer ) {
|
2012-09-13 13:44:12 +00:00
|
|
|
var promise = defer.promise(),
|
|
|
|
func = function() {},
|
|
|
|
funcPromise = defer.promise( func );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( defer.promise(), promise, "promise is always the same" );
|
|
|
|
assert.strictEqual( funcPromise, func, "non objects get extended" );
|
2013-04-09 15:45:09 +00:00
|
|
|
jQuery.each( promise, function( key ) {
|
2017-12-13 06:43:30 +00:00
|
|
|
if ( typeof promise[ key ] !== "function" ) {
|
2018-01-14 08:46:20 +00:00
|
|
|
assert.ok( false, key + " is a function (" + typeof( promise[ key ] ) + ")" );
|
2012-09-13 13:44:12 +00:00
|
|
|
}
|
|
|
|
if ( promise[ key ] !== func[ key ] ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( func[ key ], promise[ key ], key + " is the same" );
|
2012-09-13 13:44:12 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2012-09-13 13:44:12 +00:00
|
|
|
|
2012-07-05 19:52:13 +00:00
|
|
|
jQuery.expandedEach = jQuery.each;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.expandedEach( "resolve reject".split( " " ), function( _, change ) {
|
|
|
|
createDeferred( function( defer ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( defer.state(), "pending", "pending after creation" );
|
2011-09-12 15:37:41 +00:00
|
|
|
var checked = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.progress( function( value ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( value, checked, "Progress: right value (" + value + ") received" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-10-16 12:16:53 +00:00
|
|
|
for ( checked = 0; checked < 3; checked++ ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
defer.notify( checked );
|
|
|
|
}
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( defer.state(), "pending", "pending after notification" );
|
2011-09-12 15:37:41 +00:00
|
|
|
defer[ change ]();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.notStrictEqual( defer.state(), "pending", "not pending after " + change );
|
2011-09-12 15:37:41 +00:00
|
|
|
defer.notify();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2012-09-13 13:44:12 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred - chainability", function( assert ) {
|
2011-11-09 00:02:29 +00:00
|
|
|
|
2012-07-05 19:52:13 +00:00
|
|
|
var defer = jQuery.Deferred();
|
2011-11-09 00:02:29 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 10 );
|
2011-11-09 00:02:29 +00:00
|
|
|
|
2012-07-05 19:52:13 +00:00
|
|
|
jQuery.expandedEach = jQuery.each;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.expandedEach( "resolve reject notify resolveWith rejectWith notifyWith done fail progress always".split( " " ), function( _, method ) {
|
2012-10-16 12:16:53 +00:00
|
|
|
var object = {
|
|
|
|
m: defer[ method ]
|
|
|
|
};
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( object.m(), object, method + " is chainable" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-11-09 00:02:29 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - filtering (done)", function( assert ) {
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.expect( 4 );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2012-10-16 12:16:53 +00:00
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
2015-08-16 06:59:58 +00:00
|
|
|
piped = defer.then( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
return a * b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ),
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.done( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.resolve( 2, 3 ).then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value1, 2, "first resolve value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second resolve value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().reject().then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.ok( false, "then should not be called on reject" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).then( null, done.pop() );
|
2011-05-09 08:01:20 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolve().then( jQuery.noop ).done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, undefined, "then done callback can return undefined/null" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - filtering (fail)", function( assert ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.expect( 4 );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2012-10-16 12:16:53 +00:00
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
2012-04-01 23:29:39 +00:00
|
|
|
piped = defer.then( null, function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
return a * b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ),
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
defer.reject( 2, 3 ).then( null, function() {
|
|
|
|
assert.strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
jQuery.Deferred().resolve().then( null, function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.ok( false, "then should not be called on resolve" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).then( done.pop() );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().reject().then( null, jQuery.noop ).done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, undefined, "then fail callback can return undefined/null" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.catch", function( assert ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
assert.expect( 4 );
|
|
|
|
|
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
2023-09-20 22:18:42 +00:00
|
|
|
piped = defer.catch( function( a, b ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
return a * b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ),
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function( result ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
defer.reject( 2, 3 ).catch( function() {
|
2015-07-06 14:13:01 +00:00
|
|
|
assert.strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
jQuery.Deferred().resolve().catch( function() {
|
2015-07-06 14:13:01 +00:00
|
|
|
assert.ok( false, "then should not be called on resolve" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).then( done.pop() );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2023-09-20 22:18:42 +00:00
|
|
|
jQuery.Deferred().reject().catch( jQuery.noop ).done( function( value ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
assert.strictEqual( value, undefined, "then fail callback can return undefined/null" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - filtering (fail)", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
assert.expect( 4 );
|
|
|
|
|
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
|
|
|
piped = defer.pipe( null, function( a, b ) {
|
|
|
|
return a * b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ),
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2011-05-09 08:01:20 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.fail( function( result ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer.reject( 2, 3 ).pipe( null, function() {
|
|
|
|
assert.strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
jQuery.Deferred().resolve().pipe( null, function() {
|
|
|
|
assert.ok( false, "then should not be called on resolve" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).then( done.pop() );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().reject().pipe( null, jQuery.noop ).fail( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, undefined, "then fail callback can return undefined/null" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - filtering (progress)", function( assert ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.expect( 3 );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-10-16 12:16:53 +00:00
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
2012-04-01 23:29:39 +00:00
|
|
|
piped = defer.then( null, null, function( a, b ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
return a * b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.progress( function( result ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.progress( function( a, b ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
defer.notify( 2, 3 ).then( null, null, function() {
|
|
|
|
assert.strictEqual( value1, 2, "first progress value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second progress value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - deferred (done)", function( assert ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.expect( 3 );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2012-10-16 12:16:53 +00:00
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
2015-08-16 06:59:58 +00:00
|
|
|
piped = defer.then( function( a, b ) {
|
|
|
|
return jQuery.Deferred( function( defer ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.reject( a * b );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.fail( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.done( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.resolve( 2, 3 );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.fail( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value1, 2, "first resolve value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second resolve value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - deferred (fail)", function( assert ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.expect( 3 );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2012-10-16 12:16:53 +00:00
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
2012-04-01 23:29:39 +00:00
|
|
|
piped = defer.then( null, function( a, b ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
return jQuery.Deferred( function( defer ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.resolve( a * b );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.reject( 2, 3 );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - deferred (progress)", function( assert ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.expect( 3 );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-10-16 12:16:53 +00:00
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
2012-04-01 23:29:39 +00:00
|
|
|
piped = defer.then( null, null, function( a, b ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
return jQuery.Deferred( function( defer ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
defer.resolve( a * b );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.progress( function( result ) {
|
|
|
|
return jQuery.Deferred().resolve().then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
return result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).then( function( result ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.progress( function( a, b ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer.notify( 2, 3 );
|
|
|
|
|
|
|
|
piped.then( null, null, function( result ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
return jQuery.Deferred().resolve().then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
return result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value1, 2, "first progress value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second progress value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - deferred (progress)", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
assert.expect( 3 );
|
|
|
|
|
|
|
|
var value1, value2, value3,
|
|
|
|
defer = jQuery.Deferred(),
|
|
|
|
piped = defer.pipe( null, null, function( a, b ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
return jQuery.Deferred( function( defer ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
defer.resolve( a * b );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function( result ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value3 = result;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.progress( function( a, b ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
|
|
|
defer.notify( 2, 3 );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value1, 2, "first progress value ok" );
|
|
|
|
assert.strictEqual( value2, 3, "second progress value ok" );
|
|
|
|
assert.strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
done();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - context", function( assert ) {
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.expect( 11 );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var defer, piped, defer2, piped2,
|
2016-04-15 21:13:59 +00:00
|
|
|
context = { custom: true },
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 5 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolveWith( context, [ 2 ] ).then( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, context, "custom context received by .then handler" );
|
2011-06-30 23:51:50 +00:00
|
|
|
return value * 3;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).done( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.notStrictEqual( this, context,
|
|
|
|
"custom context not propagated through .then handler" );
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2016-04-15 21:13:59 +00:00
|
|
|
jQuery.Deferred().resolveWith( context, [ 2 ] ).then().done( function( value ) {
|
|
|
|
assert.strictEqual( this, context,
|
|
|
|
"custom context propagated through .then without handler" );
|
|
|
|
assert.strictEqual( value, 2, "proper value received" );
|
|
|
|
done.pop().call();
|
|
|
|
} );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolve().then( function() {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, window, "default context in .then handler" );
|
2014-12-29 19:14:13 +00:00
|
|
|
return jQuery.Deferred().resolveWith( context );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).done( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( this, context,
|
|
|
|
"custom context of returned deferred correctly propagated" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-01-06 08:09:28 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
defer = jQuery.Deferred();
|
2015-08-16 06:59:58 +00:00
|
|
|
piped = defer.then( function( value ) {
|
2013-04-09 15:45:09 +00:00
|
|
|
return value * 3;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
|
|
|
defer.resolve( 2 );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, window, ".then handler does not introduce context" );
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-01-06 08:09:28 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
defer2 = jQuery.Deferred();
|
|
|
|
piped2 = defer2.then();
|
2013-01-06 08:09:28 +00:00
|
|
|
|
|
|
|
defer2.resolve( 2 );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped2.done( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, window, ".then without handler does not introduce context" );
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, 2, "proper value received (without passing function)" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - context", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.expect( 11 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
var defer, piped, defer2, piped2,
|
2016-04-15 21:13:59 +00:00
|
|
|
context = { custom: true },
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 5 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, context, "custom context received by .pipe handler" );
|
2014-12-29 19:14:13 +00:00
|
|
|
return value * 3;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).done( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, context,
|
|
|
|
"[PIPE ONLY] custom context propagated through .pipe handler" );
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2016-04-15 21:13:59 +00:00
|
|
|
jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe().done( function( value ) {
|
|
|
|
assert.strictEqual( this, context,
|
|
|
|
"[PIPE ONLY] custom context propagated through .pipe without handler" );
|
|
|
|
assert.strictEqual( value, 2, "proper value received" );
|
|
|
|
done.pop().call();
|
|
|
|
} );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolve().pipe( function() {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, window, "default context in .pipe handler" );
|
2015-08-16 06:59:58 +00:00
|
|
|
return jQuery.Deferred().resolveWith( context );
|
|
|
|
} ).done( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( this, context,
|
|
|
|
"custom context of returned deferred correctly propagated" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer = jQuery.Deferred();
|
2015-08-16 06:59:58 +00:00
|
|
|
piped = defer.pipe( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
return value * 3;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer.resolve( 2 );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped.done( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, window, ".pipe handler does not introduce context" );
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer2 = jQuery.Deferred();
|
|
|
|
piped2 = defer2.pipe();
|
|
|
|
|
|
|
|
defer2.resolve( 2 );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
piped2.done( function( value ) {
|
2016-04-15 21:13:59 +00:00
|
|
|
assert.strictEqual( this, window, ".pipe without handler does not introduce context" );
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, 2, "proper value received (without passing function)" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-08 00:26:29 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - spec compatibility", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2017-04-24 16:39:25 +00:00
|
|
|
var done = assert.async(),
|
|
|
|
defer = jQuery.Deferred();
|
2015-09-08 00:26:29 +00:00
|
|
|
|
2017-04-24 16:39:25 +00:00
|
|
|
defer.done( function() {
|
2015-09-08 00:26:29 +00:00
|
|
|
setTimeout( done );
|
2014-12-29 19:14:13 +00:00
|
|
|
throw new Error();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
defer.then( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "errors in .done callbacks don't stop .then handlers" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
defer.resolve();
|
|
|
|
} catch ( _ ) {}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2017-04-24 16:39:25 +00:00
|
|
|
QUnit[ typeof Symbol === "function" && Symbol.toStringTag ? "test" : "skip" ](
|
|
|
|
"jQuery.Deferred.then - IsCallable determination (gh-3596)",
|
|
|
|
function( assert ) {
|
|
|
|
|
|
|
|
assert.expect( 1 );
|
|
|
|
|
|
|
|
var done = assert.async(),
|
|
|
|
defer = jQuery.Deferred();
|
|
|
|
|
|
|
|
function faker() {
|
|
|
|
assert.ok( true, "handler with non-'Function' @@toStringTag gets invoked" );
|
|
|
|
}
|
|
|
|
faker[ Symbol.toStringTag ] = "String";
|
|
|
|
|
|
|
|
defer.then( faker ).then( done );
|
|
|
|
|
|
|
|
defer.resolve();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-05-11 01:50:00 +00:00
|
|
|
// Test fails in IE9 but is skipped there because console is not active
|
2015-11-23 18:57:10 +00:00
|
|
|
QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook", function( assert ) {
|
|
|
|
|
2016-05-11 01:50:00 +00:00
|
|
|
assert.expect( 2 );
|
2015-11-23 18:57:10 +00:00
|
|
|
|
|
|
|
var done = assert.async(),
|
|
|
|
defer = jQuery.Deferred(),
|
|
|
|
oldWarn = window.console.warn;
|
|
|
|
|
2016-05-11 01:50:00 +00:00
|
|
|
window.console.warn = function() {
|
2016-01-20 12:54:31 +00:00
|
|
|
|
2016-03-23 14:03:06 +00:00
|
|
|
// Support: Chrome <=41 only
|
2016-01-20 12:54:31 +00:00
|
|
|
// Some Chrome versions newer than 30 but older than 42 display the "undefined is
|
|
|
|
// not a function" error, not mentioning the function name. This has been fixed
|
|
|
|
// in Chrome 42. Relax this test there.
|
|
|
|
// This affects our Android 5.0 & Yandex.Browser testing.
|
2016-05-11 01:50:00 +00:00
|
|
|
var msg = Array.prototype.join.call( arguments, " " ),
|
|
|
|
oldChromium = false;
|
2016-01-20 12:54:31 +00:00
|
|
|
if ( /chrome/i.test( navigator.userAgent ) ) {
|
|
|
|
oldChromium = parseInt(
|
|
|
|
navigator.userAgent.match( /chrome\/(\d+)/i )[ 1 ], 10 ) < 42;
|
|
|
|
}
|
|
|
|
if ( oldChromium ) {
|
2016-05-11 01:50:00 +00:00
|
|
|
assert.ok( /(?:barf|undefined)/.test( msg ), "Message (weak assertion): " + msg );
|
2016-01-20 12:54:31 +00:00
|
|
|
} else {
|
|
|
|
assert.ok( /barf/.test( msg ), "Message: " + msg );
|
|
|
|
}
|
2015-11-23 18:57:10 +00:00
|
|
|
};
|
|
|
|
jQuery.when(
|
|
|
|
defer.then( function() {
|
2016-01-20 12:54:31 +00:00
|
|
|
|
2015-11-23 18:57:10 +00:00
|
|
|
// Should get an error
|
|
|
|
jQuery.barf();
|
|
|
|
} ).then( null, jQuery.noop ),
|
2016-01-20 12:54:31 +00:00
|
|
|
|
2015-11-23 18:57:10 +00:00
|
|
|
defer.then( function() {
|
2016-01-20 12:54:31 +00:00
|
|
|
|
2015-11-23 18:57:10 +00:00
|
|
|
// Should NOT get an error
|
|
|
|
throw new Error( "Make me a sandwich" );
|
|
|
|
} ).then( null, jQuery.noop )
|
2016-05-11 01:50:00 +00:00
|
|
|
).then( function barf( ) {
|
|
|
|
jQuery.thisDiesToo();
|
|
|
|
} ).then( null, function( ) {
|
2015-11-23 18:57:10 +00:00
|
|
|
window.console.warn = oldWarn;
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
|
|
|
defer.resolve();
|
|
|
|
} );
|
|
|
|
|
2023-03-15 11:21:35 +00:00
|
|
|
// Support: IE 9 only
|
2016-05-11 01:50:00 +00:00
|
|
|
// Test fails in IE9 but is skipped there because console is not active
|
2015-11-23 18:57:10 +00:00
|
|
|
QUnit[ window.console ? "test" : "skip" ]( "jQuery.Deferred.exceptionHook with stack hooks", function( assert ) {
|
|
|
|
|
|
|
|
assert.expect( 2 );
|
|
|
|
|
|
|
|
var done = assert.async(),
|
|
|
|
defer = jQuery.Deferred(),
|
|
|
|
oldWarn = window.console.warn;
|
|
|
|
|
2023-03-14 21:32:57 +00:00
|
|
|
jQuery.Deferred.getErrorHook = function() {
|
2016-01-20 12:54:31 +00:00
|
|
|
|
2015-11-23 18:57:10 +00:00
|
|
|
// Default exceptionHook assumes the stack is in a form console.warn can log,
|
2023-03-14 21:32:57 +00:00
|
|
|
// but a custom getErrorHook+exceptionHook pair could save a raw form and
|
2015-11-23 18:57:10 +00:00
|
|
|
// format it to a string only when an exception actually occurs.
|
|
|
|
// For the unit test we just ensure the plumbing works.
|
2023-03-14 21:32:57 +00:00
|
|
|
return "NO ERROR FOR YOU";
|
2015-11-23 18:57:10 +00:00
|
|
|
};
|
|
|
|
|
2016-05-11 01:50:00 +00:00
|
|
|
window.console.warn = function() {
|
2016-01-20 12:54:31 +00:00
|
|
|
|
2016-03-23 14:03:06 +00:00
|
|
|
// Support: Chrome <=41 only
|
2016-01-20 12:54:31 +00:00
|
|
|
// Some Chrome versions newer than 30 but older than 42 display the "undefined is
|
|
|
|
// not a function" error, not mentioning the function name. This has been fixed
|
|
|
|
// in Chrome 42. Relax this test there.
|
|
|
|
// This affects our Android 5.0 & Yandex.Browser testing.
|
2016-05-11 01:50:00 +00:00
|
|
|
var msg = Array.prototype.join.call( arguments, " " ),
|
|
|
|
oldChromium = false;
|
2016-01-20 12:54:31 +00:00
|
|
|
if ( /chrome/i.test( navigator.userAgent ) ) {
|
|
|
|
oldChromium = parseInt(
|
|
|
|
navigator.userAgent.match( /chrome\/(\d+)/i )[ 1 ], 10 ) < 42;
|
|
|
|
}
|
|
|
|
if ( oldChromium ) {
|
2016-05-11 01:50:00 +00:00
|
|
|
assert.ok( /(?:cough_up_hairball|undefined)/.test( msg ),
|
|
|
|
"Function mentioned (weak assertion): " + msg );
|
2016-01-20 12:54:31 +00:00
|
|
|
} else {
|
|
|
|
assert.ok( /cough_up_hairball/.test( msg ), "Function mentioned: " + msg );
|
|
|
|
}
|
2023-03-14 21:32:57 +00:00
|
|
|
assert.ok( /NO ERROR FOR YOU/.test( msg ), "Error included: " + msg );
|
2015-11-23 18:57:10 +00:00
|
|
|
};
|
|
|
|
defer.then( function() {
|
|
|
|
jQuery.cough_up_hairball();
|
|
|
|
} ).then( null, function( ) {
|
|
|
|
window.console.warn = oldWarn;
|
2023-03-14 21:32:57 +00:00
|
|
|
delete jQuery.Deferred.getErrorHook;
|
2015-11-23 18:57:10 +00:00
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
|
|
|
defer.resolve();
|
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred - 1.x/2.x compatibility", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 8 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
var context = { id: "callback context" },
|
|
|
|
thenable = jQuery.Deferred().resolve( "thenable fulfillment" ).promise(),
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 8 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
thenable.unwrapped = false;
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolve( 1, 2 ).then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ],
|
|
|
|
".then fulfillment callbacks receive all resolution values" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
jQuery.Deferred().reject( 1, 2 ).then( null, function() {
|
|
|
|
assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ],
|
|
|
|
".then rejection callbacks receive all rejection values" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
jQuery.Deferred().notify( 1, 2 ).then( null, null, function() {
|
|
|
|
assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ],
|
|
|
|
".then progress callbacks receive all progress values" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolveWith( context ).then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.deepEqual( this, context, ".then fulfillment callbacks receive context" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
jQuery.Deferred().rejectWith( context ).then( null, function() {
|
|
|
|
assert.deepEqual( this, context, ".then rejection callbacks receive context" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
jQuery.Deferred().notifyWith( context ).then( null, null, function() {
|
|
|
|
assert.deepEqual( this, context, ".then progress callbacks receive context" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.Deferred().resolve( thenable ).done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( value, thenable, ".done doesn't unwrap thenables" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
jQuery.Deferred().notify( thenable ).then().then( null, null, function( value ) {
|
|
|
|
assert.strictEqual( value, "thenable fulfillment",
|
|
|
|
".then implicit progress callbacks unwrap thenables" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - progress and thenables", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 2 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
var trigger = jQuery.Deferred().notify(),
|
|
|
|
expectedProgress = [ "baz", "baz" ],
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 2 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
failer = function( evt ) {
|
|
|
|
return function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "no unexpected " + evt );
|
2014-12-29 19:14:13 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
trigger.then( null, null, function() {
|
|
|
|
var notifier = jQuery.Deferred().notify( "foo" );
|
2015-08-16 06:59:58 +00:00
|
|
|
setTimeout( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
notifier.notify( "bar" ).resolve( "baz" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
return notifier;
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).then( failer( "fulfill" ), failer( "reject" ), function( v ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( v, expectedProgress.shift(), "expected progress value" );
|
|
|
|
done.pop().call();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
trigger.notify();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Deferred - notify and resolve", function( assert ) {
|
2015-04-14 17:30:58 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 7 );
|
2015-04-14 17:30:58 +00:00
|
|
|
|
|
|
|
var notifiedResolved = jQuery.Deferred().notify( "foo" )/*xxx .resolve( "bar" )*/,
|
2023-09-20 22:18:42 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() {
|
|
|
|
return assert.async();
|
|
|
|
} );
|
2015-04-14 17:30:58 +00:00
|
|
|
|
|
|
|
notifiedResolved.progress( function( v ) {
|
|
|
|
assert.strictEqual( v, "foo", "progress value" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
notifiedResolved.pipe().progress( function( v ) {
|
|
|
|
assert.strictEqual( v, "foo", "piped progress value" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
notifiedResolved.pipe( null, null, function() {
|
|
|
|
return "baz";
|
|
|
|
} ).progress( function( v ) {
|
|
|
|
assert.strictEqual( v, "baz", "replaced piped progress value" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
notifiedResolved.pipe( null, null, function() {
|
|
|
|
return jQuery.Deferred().notify( "baz" ).resolve( "quux" );
|
|
|
|
} ).progress( function( v ) {
|
|
|
|
assert.strictEqual( v, "baz", "deferred replaced piped progress value" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
notifiedResolved.then().progress( function( v ) {
|
|
|
|
assert.strictEqual( v, "foo", "then'd progress value" );
|
|
|
|
done.pop().call();
|
|
|
|
} );
|
|
|
|
|
|
|
|
notifiedResolved.then( null, null, function() {
|
|
|
|
return "baz";
|
|
|
|
} ).progress( function( v ) {
|
|
|
|
assert.strictEqual( v, "baz", "replaced then'd progress value" );
|
|
|
|
done.pop().call();
|
|
|
|
} );
|
|
|
|
|
|
|
|
notifiedResolved.then( null, null, function() {
|
|
|
|
return jQuery.Deferred().notify( "baz" ).resolve( "quux" );
|
|
|
|
} ).progress( function( v ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2015-04-14 17:30:58 +00:00
|
|
|
// Progress from the surrogate deferred is ignored
|
|
|
|
assert.strictEqual( v, "quux", "deferred replaced then'd progress value" );
|
|
|
|
done.pop().call();
|
|
|
|
} );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-04-14 17:30:58 +00:00
|
|
|
|
2016-06-08 12:22:13 +00:00
|
|
|
QUnit.test( "jQuery.Deferred - resolved to a notifying deferred", function( assert ) {
|
|
|
|
|
|
|
|
assert.expect( 2 );
|
|
|
|
|
|
|
|
var deferred = jQuery.Deferred(),
|
|
|
|
done = assert.async( 2 );
|
|
|
|
|
|
|
|
deferred.resolve( jQuery.Deferred( function( notifyingDeferred ) {
|
2016-05-10 09:12:28 +00:00
|
|
|
notifyingDeferred.notify( "foo", "bar" );
|
|
|
|
notifyingDeferred.resolve( "baz", "quux" );
|
2016-06-08 12:22:13 +00:00
|
|
|
} ) );
|
|
|
|
|
|
|
|
// Apply an empty then to force thenable unwrapping.
|
|
|
|
// See https://github.com/jquery/jquery/issues/3000 for more info.
|
|
|
|
deferred.then().then( function() {
|
|
|
|
assert.deepEqual(
|
|
|
|
[].slice.call( arguments ),
|
|
|
|
[ "baz", "quux" ],
|
|
|
|
"The fulfilled handler receives proper params"
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
}, null, function() {
|
|
|
|
assert.deepEqual(
|
|
|
|
[].slice.call( arguments ),
|
|
|
|
[ "foo", "bar" ],
|
|
|
|
"The progress handler receives proper params"
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2016-04-15 03:59:30 +00:00
|
|
|
QUnit.test( "jQuery.when(nonThenable) - like Promise.resolve", function( assert ) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
assert.expect( 44 );
|
|
|
|
|
|
|
|
var
|
|
|
|
|
|
|
|
// Support: Android 4.0 only
|
|
|
|
// Strict mode functions invoked without .call/.apply get global-object context
|
2023-09-20 22:18:42 +00:00
|
|
|
defaultContext = ( function getDefaultContext() {
|
|
|
|
return this;
|
|
|
|
} ).call(),
|
2016-04-15 03:59:30 +00:00
|
|
|
|
|
|
|
done = assert.async( 20 );
|
|
|
|
|
|
|
|
jQuery.when()
|
|
|
|
.done( function( resolveValue ) {
|
2016-12-16 16:45:35 +00:00
|
|
|
assert.strictEqual( arguments.length, 0, "Resolved .done with no arguments" );
|
2016-04-15 03:59:30 +00:00
|
|
|
assert.strictEqual( this, defaultContext, "Default .done context with no arguments" );
|
|
|
|
} )
|
|
|
|
.then( function( resolveValue ) {
|
2016-12-16 16:45:35 +00:00
|
|
|
assert.strictEqual( arguments.length, 0, "Resolved .then with no arguments" );
|
2016-04-15 03:59:30 +00:00
|
|
|
assert.strictEqual( this, defaultContext, "Default .then context with no arguments" );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.each( {
|
2011-03-03 18:38:06 +00:00
|
|
|
"an empty string": "",
|
|
|
|
"a non-empty string": "some string",
|
|
|
|
"zero": 0,
|
|
|
|
"a number other than zero": 1,
|
|
|
|
"true": true,
|
|
|
|
"false": false,
|
|
|
|
"null": null,
|
|
|
|
"undefined": undefined,
|
2013-07-19 01:17:29 +00:00
|
|
|
"a plain object": {},
|
|
|
|
"an array": [ 1, 2, 3 ]
|
2012-10-16 12:16:53 +00:00
|
|
|
}, function( message, value ) {
|
2016-04-15 03:59:30 +00:00
|
|
|
var code = "jQuery.when( " + message + " )",
|
|
|
|
onFulfilled = function( method ) {
|
|
|
|
var call = code + "." + method;
|
|
|
|
return function( resolveValue ) {
|
|
|
|
assert.strictEqual( resolveValue, value, call + " resolve" );
|
|
|
|
assert.strictEqual( this, defaultContext, call + " context" );
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
},
|
|
|
|
onRejected = function( method ) {
|
|
|
|
var call = code + "." + method;
|
|
|
|
return function() {
|
|
|
|
assert.ok( false, call + " reject" );
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
};
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2016-04-15 03:59:30 +00:00
|
|
|
jQuery.when( value )
|
|
|
|
.done( onFulfilled( "done" ) )
|
|
|
|
.fail( onRejected( "done" ) )
|
|
|
|
.then( onFulfilled( "then" ), onRejected( "then" ) );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2016-04-15 03:59:30 +00:00
|
|
|
QUnit.test( "jQuery.when(thenable) - like Promise.resolve", function( assert ) {
|
|
|
|
"use strict";
|
|
|
|
|
2017-04-24 16:39:25 +00:00
|
|
|
var customToStringThen = {
|
|
|
|
then: function( onFulfilled ) {
|
2023-09-20 22:18:42 +00:00
|
|
|
|
2018-02-12 19:24:58 +00:00
|
|
|
// Support: Android 4.0 only
|
|
|
|
// Strict mode functions invoked without .call/.apply get global-object context
|
|
|
|
onFulfilled.call();
|
2017-04-24 16:39:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if ( typeof Symbol === "function" ) {
|
|
|
|
customToStringThen.then[ Symbol.toStringTag ] = "String";
|
|
|
|
}
|
|
|
|
|
|
|
|
var slice = [].slice,
|
2016-04-15 03:59:30 +00:00
|
|
|
sentinel = { context: "explicit" },
|
|
|
|
eventuallyFulfilled = jQuery.Deferred().notify( true ),
|
|
|
|
eventuallyRejected = jQuery.Deferred().notify( true ),
|
2016-05-04 19:30:24 +00:00
|
|
|
secondaryFulfilled = jQuery.Deferred().resolve( eventuallyFulfilled ),
|
|
|
|
secondaryRejected = jQuery.Deferred().resolve( eventuallyRejected ),
|
2016-04-15 03:59:30 +00:00
|
|
|
inputs = {
|
|
|
|
promise: Promise.resolve( true ),
|
2017-04-24 16:39:25 +00:00
|
|
|
customToStringThen: customToStringThen,
|
2016-04-15 03:59:30 +00:00
|
|
|
rejectedPromise: Promise.reject( false ),
|
|
|
|
deferred: jQuery.Deferred().resolve( true ),
|
|
|
|
eventuallyFulfilled: eventuallyFulfilled,
|
2016-05-04 19:30:24 +00:00
|
|
|
secondaryFulfilled: secondaryFulfilled,
|
|
|
|
eventuallySecondaryFulfilled: jQuery.Deferred().notify( true ),
|
2016-04-15 03:59:30 +00:00
|
|
|
multiDeferred: jQuery.Deferred().resolve( "foo", "bar" ),
|
|
|
|
deferredWith: jQuery.Deferred().resolveWith( sentinel, [ true ] ),
|
|
|
|
multiDeferredWith: jQuery.Deferred().resolveWith( sentinel, [ "foo", "bar" ] ),
|
|
|
|
rejectedDeferred: jQuery.Deferred().reject( false ),
|
|
|
|
eventuallyRejected: eventuallyRejected,
|
2016-05-04 19:30:24 +00:00
|
|
|
secondaryRejected: secondaryRejected,
|
|
|
|
eventuallySecondaryRejected: jQuery.Deferred().notify( true ),
|
2016-04-15 03:59:30 +00:00
|
|
|
multiRejectedDeferred: jQuery.Deferred().reject( "baz", "quux" ),
|
|
|
|
rejectedDeferredWith: jQuery.Deferred().rejectWith( sentinel, [ false ] ),
|
|
|
|
multiRejectedDeferredWith: jQuery.Deferred().rejectWith( sentinel, [ "baz", "quux" ] )
|
|
|
|
},
|
|
|
|
contexts = {
|
|
|
|
deferredWith: sentinel,
|
|
|
|
multiDeferredWith: sentinel,
|
|
|
|
rejectedDeferredWith: sentinel,
|
|
|
|
multiRejectedDeferredWith: sentinel
|
|
|
|
},
|
|
|
|
willSucceed = {
|
|
|
|
promise: [ true ],
|
2017-04-24 16:39:25 +00:00
|
|
|
customToStringThen: [],
|
2016-04-15 03:59:30 +00:00
|
|
|
deferred: [ true ],
|
|
|
|
eventuallyFulfilled: [ true ],
|
|
|
|
secondaryFulfilled: [ true ],
|
2016-05-04 19:30:24 +00:00
|
|
|
eventuallySecondaryFulfilled: [ true ],
|
2016-04-15 03:59:30 +00:00
|
|
|
multiDeferred: [ "foo", "bar" ],
|
|
|
|
deferredWith: [ true ],
|
|
|
|
multiDeferredWith: [ "foo", "bar" ]
|
|
|
|
},
|
|
|
|
willError = {
|
|
|
|
rejectedPromise: [ false ],
|
|
|
|
rejectedDeferred: [ false ],
|
|
|
|
eventuallyRejected: [ false ],
|
|
|
|
secondaryRejected: [ false ],
|
2016-05-04 19:30:24 +00:00
|
|
|
eventuallySecondaryRejected: [ false ],
|
2016-04-15 03:59:30 +00:00
|
|
|
multiRejectedDeferred: [ "baz", "quux" ],
|
|
|
|
rejectedDeferredWith: [ false ],
|
|
|
|
multiRejectedDeferredWith: [ "baz", "quux" ]
|
|
|
|
},
|
2017-04-24 16:39:25 +00:00
|
|
|
numCases = Object.keys( willSucceed ).length + Object.keys( willError ).length,
|
2016-04-15 03:59:30 +00:00
|
|
|
|
|
|
|
// Support: Android 4.0 only
|
|
|
|
// Strict mode functions invoked without .call/.apply get global-object context
|
2023-09-20 22:18:42 +00:00
|
|
|
defaultContext = ( function getDefaultContext() {
|
|
|
|
return this;
|
|
|
|
} ).call(),
|
2016-04-15 03:59:30 +00:00
|
|
|
|
2017-04-24 16:39:25 +00:00
|
|
|
done = assert.async( numCases * 2 );
|
2016-05-04 19:30:24 +00:00
|
|
|
|
2017-04-24 16:39:25 +00:00
|
|
|
assert.expect( numCases * 4 );
|
2016-04-15 03:59:30 +00:00
|
|
|
|
|
|
|
jQuery.each( inputs, function( message, value ) {
|
|
|
|
var code = "jQuery.when( " + message + " )",
|
|
|
|
shouldResolve = willSucceed[ message ],
|
|
|
|
shouldError = willError[ message ],
|
|
|
|
context = contexts[ message ] || defaultContext,
|
|
|
|
onFulfilled = function( method ) {
|
|
|
|
var call = code + "." + method;
|
|
|
|
return function() {
|
|
|
|
if ( shouldResolve ) {
|
|
|
|
assert.deepEqual( slice.call( arguments ), shouldResolve,
|
|
|
|
call + " resolve" );
|
|
|
|
assert.strictEqual( this, context, call + " context" );
|
|
|
|
} else {
|
|
|
|
assert.ok( false, call + " resolve" );
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
},
|
|
|
|
onRejected = function( method ) {
|
|
|
|
var call = code + "." + method;
|
|
|
|
return function() {
|
|
|
|
if ( shouldError ) {
|
|
|
|
assert.deepEqual( slice.call( arguments ), shouldError, call + " reject" );
|
|
|
|
assert.strictEqual( this, context, call + " context" );
|
|
|
|
} else {
|
|
|
|
assert.ok( false, call + " reject" );
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
jQuery.when( value )
|
|
|
|
.done( onFulfilled( "done" ) )
|
|
|
|
.fail( onRejected( "done" ) )
|
|
|
|
.then( onFulfilled( "then" ), onRejected( "then" ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
eventuallyFulfilled.resolve( true );
|
|
|
|
eventuallyRejected.reject( false );
|
2016-05-04 19:30:24 +00:00
|
|
|
inputs.eventuallySecondaryFulfilled.resolve( secondaryFulfilled );
|
|
|
|
inputs.eventuallySecondaryRejected.resolve( secondaryRejected );
|
2016-04-15 03:59:30 +00:00
|
|
|
}, 50 );
|
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2016-04-15 03:59:30 +00:00
|
|
|
QUnit.test( "jQuery.when(a, b) - like Promise.all", function( assert ) {
|
|
|
|
"use strict";
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2016-04-15 03:59:30 +00:00
|
|
|
assert.expect( 196 );
|
|
|
|
|
|
|
|
var slice = [].slice,
|
|
|
|
deferreds = {
|
2014-12-29 19:14:13 +00:00
|
|
|
rawValue: 1,
|
|
|
|
fulfilled: jQuery.Deferred().resolve( 1 ),
|
|
|
|
rejected: jQuery.Deferred().reject( 0 ),
|
|
|
|
eventuallyFulfilled: jQuery.Deferred().notify( true ),
|
|
|
|
eventuallyRejected: jQuery.Deferred().notify( true ),
|
|
|
|
fulfilledStandardPromise: Promise.resolve( 1 ),
|
|
|
|
rejectedStandardPromise: Promise.reject( 0 )
|
2011-04-07 03:49:32 +00:00
|
|
|
},
|
|
|
|
willSucceed = {
|
2014-12-29 19:14:13 +00:00
|
|
|
rawValue: true,
|
|
|
|
fulfilled: true,
|
|
|
|
eventuallyFulfilled: true,
|
|
|
|
fulfilledStandardPromise: true
|
2011-09-12 15:37:41 +00:00
|
|
|
},
|
|
|
|
willError = {
|
2014-12-29 19:14:13 +00:00
|
|
|
rejected: true,
|
|
|
|
eventuallyRejected: true,
|
|
|
|
rejectedStandardPromise: true
|
2011-09-12 15:37:41 +00:00
|
|
|
},
|
2016-04-27 16:46:14 +00:00
|
|
|
|
|
|
|
// Support: Android 4.0 only
|
|
|
|
// Strict mode functions invoked without .call/.apply get global-object context
|
2023-09-20 22:18:42 +00:00
|
|
|
defaultContext = ( function getDefaultContext() {
|
|
|
|
return this;
|
|
|
|
} ).call(),
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2016-04-15 03:59:30 +00:00
|
|
|
done = assert.async( 98 );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2016-04-15 03:59:30 +00:00
|
|
|
jQuery.each( deferreds, function( id1, v1 ) {
|
|
|
|
jQuery.each( deferreds, function( id2, v2 ) {
|
|
|
|
var code = "jQuery.when( " + id1 + ", " + id2 + " )",
|
|
|
|
shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
|
2011-09-12 15:37:41 +00:00
|
|
|
shouldError = willError[ id1 ] || willError[ id2 ],
|
2016-04-15 03:59:30 +00:00
|
|
|
expected = shouldResolve ? [ 1, 1 ] : [ 0 ],
|
|
|
|
context = shouldResolve ? [ defaultContext, defaultContext ] : defaultContext,
|
|
|
|
onFulfilled = function( method ) {
|
|
|
|
var call = code + "." + method;
|
|
|
|
return function() {
|
|
|
|
if ( shouldResolve ) {
|
|
|
|
assert.deepEqual( slice.call( arguments ), expected,
|
|
|
|
call + " resolve" );
|
|
|
|
assert.deepEqual( this, context, code + " context" );
|
|
|
|
} else {
|
|
|
|
assert.ok( false, call + " resolve" );
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
},
|
|
|
|
onRejected = function( method ) {
|
|
|
|
var call = code + "." + method;
|
|
|
|
return function() {
|
|
|
|
if ( shouldError ) {
|
|
|
|
assert.deepEqual( slice.call( arguments ), expected, call + " reject" );
|
|
|
|
assert.deepEqual( this, context, code + " context" );
|
|
|
|
} else {
|
|
|
|
assert.ok( false, call + " reject" );
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
jQuery.when( v1, v2 )
|
|
|
|
.done( onFulfilled( "done" ) )
|
|
|
|
.fail( onRejected( "done" ) )
|
|
|
|
.then( onFulfilled( "then" ), onRejected( "then" ) );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
deferreds.eventuallyFulfilled.resolve( 1 );
|
|
|
|
deferreds.eventuallyRejected.reject( 0 );
|
|
|
|
}, 50 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( "jQuery.when - always returns a new promise", function( assert ) {
|
|
|
|
|
|
|
|
assert.expect( 42 );
|
|
|
|
|
|
|
|
jQuery.each( {
|
|
|
|
"no arguments": [],
|
|
|
|
"non-thenable": [ "foo" ],
|
|
|
|
"promise": [ Promise.resolve( "bar" ) ],
|
|
|
|
"rejected promise": [ Promise.reject( "bar" ) ],
|
|
|
|
"deferred": [ jQuery.Deferred().resolve( "baz" ) ],
|
|
|
|
"rejected deferred": [ jQuery.Deferred().reject( "baz" ) ],
|
|
|
|
"multi-resolved deferred": [ jQuery.Deferred().resolve( "qux", "quux" ) ],
|
|
|
|
"multiple non-thenables": [ "corge", "grault" ],
|
|
|
|
"multiple deferreds": [
|
|
|
|
jQuery.Deferred().resolve( "garply" ),
|
|
|
|
jQuery.Deferred().resolve( "waldo" )
|
|
|
|
]
|
|
|
|
}, function( label, args ) {
|
|
|
|
var result = jQuery.when.apply( jQuery, args );
|
|
|
|
|
2017-12-13 06:43:30 +00:00
|
|
|
assert.ok( typeof result.then === "function", "Thenable returned from " + label );
|
2016-04-15 03:59:30 +00:00
|
|
|
assert.strictEqual( result.resolve, undefined, "Non-deferred returned from " + label );
|
|
|
|
assert.strictEqual( result.promise(), result, "Promise returned from " + label );
|
|
|
|
|
|
|
|
jQuery.each( args, function( i, arg ) {
|
|
|
|
assert.notStrictEqual( result, arg, "Returns distinct from arg " + i + " of " + label );
|
|
|
|
if ( arg.promise ) {
|
|
|
|
assert.notStrictEqual( result, arg.promise(),
|
|
|
|
"Returns distinct from promise of arg " + i + " of " + label );
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2014-12-09 12:57:15 +00:00
|
|
|
|
2016-01-12 00:10:28 +00:00
|
|
|
QUnit.test( "jQuery.when - notify does not affect resolved", function( assert ) {
|
2014-12-09 12:57:15 +00:00
|
|
|
|
2016-01-12 00:10:28 +00:00
|
|
|
assert.expect( 3 );
|
2014-12-09 12:57:15 +00:00
|
|
|
|
|
|
|
var a = jQuery.Deferred().notify( 1 ).resolve( 4 ),
|
|
|
|
b = jQuery.Deferred().notify( 2 ).resolve( 5 ),
|
|
|
|
c = jQuery.Deferred().notify( 3 ).resolve( 6 );
|
|
|
|
|
2016-01-12 00:10:28 +00:00
|
|
|
jQuery.when( a, b, c ).done( function( a, b, c ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( a, 4, "first resolve value ok" );
|
|
|
|
assert.strictEqual( b, 5, "second resolve value ok" );
|
|
|
|
assert.strictEqual( c, 6, "third resolve value ok" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).fail( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "Error on resolve" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2016-05-04 19:30:24 +00:00
|
|
|
|
|
|
|
QUnit.test( "jQuery.when(...) - opportunistically synchronous", function( assert ) {
|
|
|
|
|
|
|
|
assert.expect( 5 );
|
|
|
|
|
|
|
|
var when = "before",
|
|
|
|
resolved = jQuery.Deferred().resolve( true ),
|
|
|
|
rejected = jQuery.Deferred().reject( false ),
|
|
|
|
validate = function( label ) {
|
|
|
|
return function() {
|
|
|
|
assert.equal( when, "before", label );
|
|
|
|
};
|
|
|
|
},
|
|
|
|
done = assert.async( 5 );
|
|
|
|
|
|
|
|
jQuery.when().done( validate( "jQuery.when()" ) ).always( done );
|
|
|
|
jQuery.when( when ).done( validate( "jQuery.when(nonThenable)" ) ).always( done );
|
|
|
|
jQuery.when( resolved ).done( validate( "jQuery.when(alreadyFulfilled)" ) ).always( done );
|
|
|
|
jQuery.when( rejected ).fail( validate( "jQuery.when(alreadyRejected)" ) ).always( done );
|
|
|
|
jQuery.when( resolved, rejected )
|
|
|
|
.always( validate( "jQuery.when(alreadyFulfilled, alreadyRejected)" ) )
|
|
|
|
.always( done );
|
|
|
|
|
|
|
|
when = "after";
|
|
|
|
} );
|
2020-01-27 17:54:47 +00:00
|
|
|
|
|
|
|
} )();
|