2015-09-03 01:46:31 +00:00
|
|
|
QUnit.module( "deferred", {
|
2012-10-16 12:16:53 +00:00
|
|
|
teardown: moduleTeardown
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
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-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.Deferred" + withNew, function( assert ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 23 );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
var defer = createDeferred();
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( jQuery.isFunction( defer.pipe ), "defer.pipe is a function" );
|
2012-04-01 23:29:39 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
createDeferred().resolve().done( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Success on resolve" );
|
|
|
|
assert.strictEqual( this.state(), "resolved", "Deferred is resolved (state)" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).fail( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "Error on resolve" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).always( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Always callback on resolve" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
createDeferred().reject().done( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "Success on reject" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).fail( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Error on reject" );
|
|
|
|
assert.strictEqual( this.state(), "rejected", "Deferred is rejected (state)" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).always( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Always callback on reject" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
createDeferred( function( defer ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( this === defer, "Defer passed as this & first argument" );
|
2015-09-02 23:52:01 +00:00
|
|
|
this.resolve( "done" );
|
|
|
|
} ).done( function( value ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( value, "done", "Passed function executed" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
createDeferred( function( defer ) {
|
2012-09-13 13:44:12 +00:00
|
|
|
var promise = defer.promise(),
|
|
|
|
func = function() {},
|
|
|
|
funcPromise = defer.promise( func );
|
2015-09-03 01:46:31 +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 ) {
|
2012-09-13 13:44:12 +00:00
|
|
|
if ( !jQuery.isFunction( promise[ key ] ) ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" );
|
2012-09-13 13:44:12 +00:00
|
|
|
}
|
|
|
|
if ( promise[ key ] !== func[ key ] ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( func[ key ], promise[ key ], key + " is the same" );
|
2012-09-13 13:44:12 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2012-09-13 13:44:12 +00:00
|
|
|
|
2012-07-05 19:52:13 +00:00
|
|
|
jQuery.expandedEach = jQuery.each;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.expandedEach( "resolve reject".split( " " ), function( _, change ) {
|
|
|
|
createDeferred( function( defer ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( defer.state(), "pending", "pending after creation" );
|
2011-09-12 15:37:41 +00:00
|
|
|
var checked = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.progress( function( value ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( value, checked, "Progress: right value (" + value + ") received" );
|
2015-09-02 23:52:01 +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-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( defer.state(), "pending", "pending after notification" );
|
2011-09-12 15:37:41 +00:00
|
|
|
defer[ change ]();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.notStrictEqual( defer.state(), "pending", "not pending after " + change );
|
2011-09-12 15:37:41 +00:00
|
|
|
defer.notify();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2012-09-13 13:44:12 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-03 01:46:31 +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-09-02 23:52:01 +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-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( object.m(), object, method + " is chainable" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-11-09 00:02:29 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
piped = defer.then( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
return a * b;
|
2015-09-02 23:52:01 +00:00
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.done( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.done( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} ).then( null, done.pop() );
|
2011-05-09 08:01:20 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.done( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} ).then( done.pop() );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +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(),
|
2015-09-02 23:52:01 +00:00
|
|
|
piped = defer[ "catch" ]( function( a, b ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
return a * b;
|
2015-09-02 23:52:01 +00:00
|
|
|
} ),
|
2015-07-06 14:13:01 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.done( function( result ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2015-07-06 14:13:01 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} ).then( done.pop() );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2015-07-06 14:13:01 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
|
2011-05-09 08:01:20 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.fail( function( result ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} ).then( done.pop() );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.progress( function( result ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.progress( function( a, b ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.fail( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.done( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.resolve( 2, 3 );
|
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
return jQuery.Deferred( function( defer ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.resolve( a * b );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.done( function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.fail( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
return jQuery.Deferred( function( defer ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
defer.resolve( a * b );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.progress( function( result ) {
|
|
|
|
return jQuery.Deferred().resolve().then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
return result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).then( function( result ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.progress( function( a, b ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer.notify( 2, 3 );
|
|
|
|
|
|
|
|
piped.then( null, null, function( result ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
return jQuery.Deferred().resolve().then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
return result;
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
return jQuery.Deferred( function( defer ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
defer.resolve( a * b );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} ),
|
2014-12-29 19:14:13 +00:00
|
|
|
done = assert.async();
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.done( function( result ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value3 = result;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.progress( function( a, b ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
|
|
|
defer.notify( 2, 3 );
|
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - context", function( assert ) {
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.expect( 7 );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var defer, piped, defer2, piped2,
|
2014-12-29 19:14:13 +00:00
|
|
|
context = {},
|
|
|
|
done = jQuery.map( new Array( 4 ), function() { return assert.async(); } );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.Deferred().resolveWith( context, [ 2 ] ).then( function( value ) {
|
2011-06-30 23:51:50 +00:00
|
|
|
return value * 3;
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.notStrictEqual( this, context, "custom context not propagated through .then" );
|
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.Deferred().resolve().then( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
return jQuery.Deferred().resolveWith( context );
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-01-08 15:27:13 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
defer = jQuery.Deferred();
|
2015-09-02 23:52:01 +00:00
|
|
|
piped = defer.then( function( value ) {
|
2013-04-09 15:45:09 +00:00
|
|
|
return value * 3;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
|
|
|
defer.resolve( 2 );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( this, piped,
|
|
|
|
"default context gets updated to latest promise in the chain" );
|
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-01-08 15:27:13 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
defer2 = jQuery.Deferred();
|
|
|
|
piped2 = defer2.then();
|
2013-01-08 15:27:13 +00:00
|
|
|
|
|
|
|
defer2.resolve( 2 );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped2.done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( this, piped2,
|
|
|
|
"default context updated to latest promise in the chain (without passing function)" );
|
|
|
|
assert.strictEqual( value, 2, "proper value received (without passing function)" );
|
|
|
|
done.pop().call();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-06-30 23:51:50 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - context", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
assert.expect( 7 );
|
|
|
|
|
|
|
|
var defer, piped, defer2, piped2,
|
|
|
|
context = {},
|
|
|
|
done = jQuery.map( new Array( 4 ), function() { return assert.async(); } );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
return value * 3;
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( this, context, "[PIPE ONLY] custom context correctly propagated" );
|
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.Deferred().resolve().pipe( function() {
|
|
|
|
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-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer = jQuery.Deferred();
|
2015-09-02 23:52:01 +00:00
|
|
|
piped = defer.pipe( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
return value * 3;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer.resolve( 2 );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped.done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( this, piped,
|
|
|
|
"default context gets updated to latest promise in the chain" );
|
|
|
|
assert.strictEqual( value, 6, "proper value received" );
|
|
|
|
done.pop().call();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
defer2 = jQuery.Deferred();
|
|
|
|
piped2 = defer2.pipe();
|
|
|
|
|
|
|
|
defer2.resolve( 2 );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
piped2.done( function( value ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
assert.strictEqual( this, piped2,
|
|
|
|
"default context updated to latest promise in the chain (without passing function)" );
|
|
|
|
assert.strictEqual( value, 2, "proper value received (without passing function)" );
|
|
|
|
done.pop().call();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.asyncTest( "jQuery.Deferred.then - spec compatibility", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 1 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var defer = jQuery.Deferred().done( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
setTimeout( start );
|
|
|
|
throw new Error();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
defer.then( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "errors in .done callbacks don't stop .then handlers" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
defer.resolve();
|
|
|
|
} catch ( _ ) {}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.Deferred - 1.x/2.x compatibility", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +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(),
|
|
|
|
done = jQuery.map( new Array( 8 ), function() { return assert.async(); } );
|
|
|
|
|
|
|
|
thenable.unwrapped = false;
|
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.Deferred.then - progress and thenables", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 2 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
var trigger = jQuery.Deferred().notify(),
|
|
|
|
expectedProgress = [ "baz", "baz" ],
|
|
|
|
done = jQuery.map( new Array( 2 ), function() { return assert.async(); } ),
|
|
|
|
failer = function( evt ) {
|
|
|
|
return function() {
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
setTimeout( function() {
|
2014-12-29 19:14:13 +00:00
|
|
|
notifier.notify( "bar" ).resolve( "baz" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
return notifier;
|
2015-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
trigger.notify();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.Deferred - notify and resolve", function( assert ) {
|
2015-04-14 17:30:58 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 7 );
|
2015-04-14 17:30:58 +00:00
|
|
|
|
|
|
|
var notifiedResolved = jQuery.Deferred().notify( "foo" )/*xxx .resolve( "bar" )*/,
|
|
|
|
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
|
|
|
|
|
|
|
|
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-09-02 23:52:01 +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-09-02 23:52:01 +00:00
|
|
|
} );
|
2015-04-14 17:30:58 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.when", function( assert ) {
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 37 );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
|
|
|
// Some other objects
|
2015-09-02 23:52:01 +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 ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok(
|
2012-10-16 12:16:53 +00:00
|
|
|
jQuery.isFunction(
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.when( value ).done( function( resolveValue ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( this, window, "Context is the global object with " + message );
|
|
|
|
assert.strictEqual( resolveValue, value, "Test the promise was resolved with " + message );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).promise
|
2012-10-16 12:16:53 +00:00
|
|
|
),
|
|
|
|
"Test " + message + " triggers the creation of a new Promise"
|
|
|
|
);
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok(
|
2012-10-16 12:16:53 +00:00
|
|
|
jQuery.isFunction(
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.when().done( function( resolveValue ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( this, window, "Test the promise was resolved with window as its context" );
|
|
|
|
assert.strictEqual( resolveValue, undefined, "Test the promise was resolved with no parameter" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).promise
|
2012-10-16 12:16:53 +00:00
|
|
|
),
|
|
|
|
"Test calling when with no parameter triggers the creation of a new Promise"
|
|
|
|
);
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var cache,
|
|
|
|
context = {};
|
2012-05-12 19:41:39 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.when( jQuery.Deferred().resolveWith( context ) ).done( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( this, context, "when( promise ) propagates context" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-05-12 19:41:39 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.each( [ 1, 2, 3 ], function( k, i ) {
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.when( cache || jQuery.Deferred( function() {
|
2012-06-21 19:30:24 +00:00
|
|
|
this.resolve( i );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
).done( function( value ) {
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( value, 1, "Function executed" + ( i > 1 ? " only once" : "" ) );
|
2011-03-03 18:38:06 +00:00
|
|
|
cache = value;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.when - joined", function( assert ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 195 );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
|
|
|
var deferreds = {
|
2014-12-29 19:14:13 +00:00
|
|
|
rawValue: 1,
|
|
|
|
fulfilled: jQuery.Deferred().resolve( 1 ),
|
|
|
|
rejected: jQuery.Deferred().reject( 0 ),
|
|
|
|
notified: jQuery.Deferred().notify( true ),
|
|
|
|
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
|
|
|
},
|
|
|
|
willNotify = {
|
2014-12-29 19:14:13 +00:00
|
|
|
notified: true,
|
|
|
|
eventuallyFulfilled: true,
|
|
|
|
eventuallyRejected: true
|
|
|
|
},
|
|
|
|
counter = 49;
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.stop();
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
function restart() {
|
|
|
|
if ( !--counter ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.start();
|
2014-12-29 19:14:13 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-07 03:49:32 +00:00
|
|
|
|
|
|
|
jQuery.each( deferreds, function( id1, defer1 ) {
|
|
|
|
jQuery.each( deferreds, function( id2, defer2 ) {
|
|
|
|
var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
|
2011-09-12 15:37:41 +00:00
|
|
|
shouldError = willError[ id1 ] || willError[ id2 ],
|
|
|
|
shouldNotify = willNotify[ id1 ] || willNotify[ id2 ],
|
2011-04-07 03:49:32 +00:00
|
|
|
expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
|
2012-10-16 12:16:53 +00:00
|
|
|
expectedNotify = shouldNotify && [ willNotify[ id1 ], willNotify[ id2 ] ],
|
2014-12-29 19:14:13 +00:00
|
|
|
code = "jQuery.when( " + id1 + ", " + id2 + " )",
|
|
|
|
context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() :
|
|
|
|
( defer1.then ? window : undefined ),
|
|
|
|
context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() :
|
|
|
|
( defer2.then ? window : undefined );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.when( defer1, defer2 ).done( function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
if ( shouldResolve ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.deepEqual( [ a, b ], expected, code + " => resolve" );
|
|
|
|
assert.strictEqual( this[ 0 ], context1, code + " => first context OK" );
|
|
|
|
assert.strictEqual( this[ 1 ], context2, code + " => second context OK" );
|
2011-09-12 15:37:41 +00:00
|
|
|
} else {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, code + " => resolve" );
|
2011-04-07 03:49:32 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).fail( function( a, b ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
if ( shouldError ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.deepEqual( [ a, b ], expected, code + " => reject" );
|
2011-09-12 15:37:41 +00:00
|
|
|
} else {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, code + " => reject" );
|
2011-04-07 03:49:32 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).progress( function( a, b ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.deepEqual( [ a, b ], expectedNotify, code + " => progress" );
|
|
|
|
assert.strictEqual( this[ 0 ], expectedNotify[ 0 ] ? context1 : undefined, code + " => first context OK" );
|
|
|
|
assert.strictEqual( this[ 1 ], expectedNotify[ 1 ] ? context2 : undefined, code + " => second context OK" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).always( restart );
|
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
deferreds.eventuallyFulfilled.resolve( 1 );
|
|
|
|
deferreds.eventuallyRejected.reject( 0 );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-09 12:57:15 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.when - resolved", function( assert ) {
|
2014-12-09 12:57:15 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 6 );
|
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 );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.when( a, b, c ).progress( function( a, b, c ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( a, 1, "first notify value ok" );
|
|
|
|
assert.strictEqual( b, 2, "second notify value ok" );
|
|
|
|
assert.strictEqual( c, 3, "third notify value ok" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).done( function( a, b, c ) {
|
2015-09-03 01:46:31 +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-09-02 23:52:01 +00:00
|
|
|
} ).fail( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "Error on resolve" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.when - filtering", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 2 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
function increment( x ) {
|
|
|
|
return x + 1;
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.stop();
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
jQuery.when(
|
|
|
|
jQuery.Deferred().resolve( 3 ).then( increment ),
|
|
|
|
jQuery.Deferred().reject( 5 ).then( null, increment )
|
2015-09-02 23:52:01 +00:00
|
|
|
).done( function( four, six ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( four, 4, "resolved value incremented" );
|
|
|
|
assert.strictEqual( six, 6, "rejected value incremented" );
|
|
|
|
QUnit.start();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.when - exceptions", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 2 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
function woops() {
|
|
|
|
throw "exception thrown";
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.stop();
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.Deferred().resolve().then( woops ).fail( function( doneException ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( doneException, "exception thrown", "throwing in done handler" );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.Deferred().reject().then( null, woops ).fail( function( failException ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( failException, "exception thrown", "throwing in fail handler" );
|
|
|
|
QUnit.start();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.when - chaining", function( assert ) {
|
2014-12-29 19:14:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 4 );
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
var defer = jQuery.Deferred();
|
|
|
|
|
|
|
|
function chain() {
|
|
|
|
return defer;
|
|
|
|
}
|
|
|
|
|
|
|
|
function chainStandard() {
|
|
|
|
return Promise.resolve( "std deferred" );
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.stop();
|
2014-12-29 19:14:13 +00:00
|
|
|
|
|
|
|
jQuery.when(
|
|
|
|
jQuery.Deferred().resolve( 3 ).then( chain ),
|
|
|
|
jQuery.Deferred().reject( 5 ).then( null, chain ),
|
|
|
|
jQuery.Deferred().resolve( 3 ).then( chainStandard ),
|
|
|
|
jQuery.Deferred().reject( 5 ).then( null, chainStandard )
|
2015-09-02 23:52:01 +00:00
|
|
|
).done( function( v1, v2, s1, s2 ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( v1, "other deferred", "chaining in done handler" );
|
|
|
|
assert.strictEqual( v2, "other deferred", "chaining in fail handler" );
|
|
|
|
assert.strictEqual( s1, "std deferred", "chaining thenable in done handler" );
|
|
|
|
assert.strictEqual( s2, "std deferred", "chaining thenable in fail handler" );
|
|
|
|
QUnit.start();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-09 12:57:15 +00:00
|
|
|
|
2014-12-29 19:14:13 +00:00
|
|
|
defer.resolve( "other deferred" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|