2011-03-03 18:38:06 +00:00
|
|
|
module("deferred", { teardown: moduleTeardown });
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
test("jQuery.Deferred" + withNew, function() {
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
expect( 23 );
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
var defer = createDeferred();
|
|
|
|
|
|
|
|
strictEqual( defer.pipe, defer.then, "pipe is an alias of then" );
|
|
|
|
|
|
|
|
createDeferred().resolve().done(function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( true , "Success on resolve" );
|
|
|
|
ok( this.isResolved(), "Deferred is resolved" );
|
2011-10-12 00:23:56 +00:00
|
|
|
strictEqual( this.state(), "resolved", "Deferred is resolved (state)" );
|
2012-04-01 23:29:39 +00:00
|
|
|
}).fail(function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( false , "Error on resolve" );
|
2012-04-01 23:29:39 +00:00
|
|
|
}).always(function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( true , "Always callback on resolve" );
|
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
createDeferred().reject().done(function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( false , "Success on reject" );
|
2012-04-01 23:29:39 +00:00
|
|
|
}).fail(function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( true , "Error on reject" );
|
|
|
|
ok( this.isRejected(), "Deferred is rejected" );
|
2011-10-12 00:23:56 +00:00
|
|
|
strictEqual( this.state(), "rejected", "Deferred is rejected (state)" );
|
2012-04-01 23:29:39 +00:00
|
|
|
}).always(function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( true , "Always callback on reject" );
|
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
createDeferred(function( defer ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( this === defer , "Defer passed as this & first argument" );
|
|
|
|
this.resolve( "done" );
|
2012-04-01 23:29:39 +00:00
|
|
|
}).done( function( value ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
strictEqual( value , "done" , "Passed function executed" );
|
|
|
|
});
|
2011-09-12 15:37:41 +00:00
|
|
|
|
|
|
|
jQuery.each( "resolve reject".split( " " ), function( _, change ) {
|
|
|
|
createDeferred( function( defer ) {
|
2011-10-12 00:23:56 +00:00
|
|
|
strictEqual( defer.state(), "pending", "pending after creation" );
|
2011-09-12 15:37:41 +00:00
|
|
|
var checked = 0;
|
|
|
|
defer.progress(function( value ) {
|
|
|
|
strictEqual( value, checked, "Progress: right value (" + value + ") received" );
|
|
|
|
});
|
|
|
|
for( checked = 0; checked < 3 ; checked++ ) {
|
|
|
|
defer.notify( checked );
|
|
|
|
}
|
2011-10-12 00:23:56 +00:00
|
|
|
strictEqual( defer.state(), "pending", "pending after notification" );
|
2011-09-12 15:37:41 +00:00
|
|
|
defer[ change ]();
|
2011-10-12 00:23:56 +00:00
|
|
|
notStrictEqual( defer.state(), "pending", "not pending after " + change );
|
2011-09-12 15:37:41 +00:00
|
|
|
defer.notify();
|
|
|
|
});
|
|
|
|
});
|
2011-04-07 03:49:32 +00:00
|
|
|
});
|
|
|
|
} );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-11-09 00:02:29 +00:00
|
|
|
test( "jQuery.Deferred - chainability", function() {
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
var methods = "resolve reject notify resolveWith rejectWith notifyWith done fail progress always".split( " " ),
|
2011-11-09 00:02:29 +00:00
|
|
|
defer = jQuery.Deferred();
|
|
|
|
|
|
|
|
expect( methods.length );
|
|
|
|
|
|
|
|
jQuery.each( methods, function( _, method ) {
|
|
|
|
var object = { m: defer[ method ] };
|
|
|
|
strictEqual( object.m(), object, method + " is chainable" );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
test( "jQuery.Deferred.then - filtering (done)", function() {
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-05-09 08:01:20 +00:00
|
|
|
expect(4);
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
var defer = jQuery.Deferred(),
|
2012-04-01 23:29:39 +00:00
|
|
|
piped = defer.then(function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
return a * b;
|
|
|
|
}),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
2011-04-08 15:35:08 +00:00
|
|
|
piped.done(function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer.done(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.resolve( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first resolve value ok" );
|
|
|
|
strictEqual( value2, 3, "second resolve value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
jQuery.Deferred().reject().then(function() {
|
|
|
|
ok( false, "then should not be called on reject" );
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
2011-05-09 08:01:20 +00:00
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
jQuery.Deferred().resolve().then( jQuery.noop ).done(function( value ) {
|
|
|
|
strictEqual( value, undefined, "then done callback can return undefined/null" );
|
2011-05-09 08:01:20 +00:00
|
|
|
});
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
test( "jQuery.Deferred.then - filtering (fail)", function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
2011-05-09 08:01:20 +00:00
|
|
|
expect(4);
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
var 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;
|
|
|
|
} ),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
2011-03-03 18:38:06 +00:00
|
|
|
|
2011-04-08 15:35:08 +00:00
|
|
|
piped.fail(function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.fail(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.reject( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
jQuery.Deferred().resolve().then( null, function() {
|
|
|
|
ok( false, "then should not be called on resolve" );
|
2011-04-07 03:49:32 +00:00
|
|
|
} );
|
2011-05-09 08:01:20 +00:00
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
jQuery.Deferred().reject().then( null, jQuery.noop ).fail(function( value ) {
|
|
|
|
strictEqual( value, undefined, "then fail callback can return undefined/null" );
|
2011-05-09 08:01:20 +00:00
|
|
|
});
|
2011-04-07 03:49:32 +00:00
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
test( "jQuery.Deferred.then - filtering (progress)", function() {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
|
|
|
expect(3);
|
|
|
|
|
|
|
|
var 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;
|
|
|
|
} ),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
|
|
|
piped.progress(function( result ) {
|
|
|
|
value3 = result;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer.progress(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer.notify( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first progress value ok" );
|
|
|
|
strictEqual( value2, 3, "second progress value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
test( "jQuery.Deferred.then - deferred (done)", function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
|
|
|
expect(3);
|
|
|
|
|
|
|
|
var defer = jQuery.Deferred(),
|
2012-04-01 23:29:39 +00:00
|
|
|
piped = defer.then(function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
return jQuery.Deferred(function( defer ) {
|
|
|
|
defer.reject( a * b );
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
2011-04-08 15:35:08 +00:00
|
|
|
piped.fail(function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.done(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.resolve( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first resolve value ok" );
|
|
|
|
strictEqual( value2, 3, "second resolve value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
test( "jQuery.Deferred.then - deferred (fail)", function() {
|
2011-04-07 03:49:32 +00:00
|
|
|
|
|
|
|
expect(3);
|
|
|
|
|
|
|
|
var 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 jQuery.Deferred(function( defer ) {
|
|
|
|
defer.resolve( a * b );
|
|
|
|
});
|
|
|
|
} ),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
2011-04-08 15:35:08 +00:00
|
|
|
piped.done(function( result ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
value3 = result;
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
defer.fail(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
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
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
test( "jQuery.Deferred.then - deferred (progress)", function() {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
|
|
|
expect(3);
|
|
|
|
|
|
|
|
var 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 jQuery.Deferred(function( defer ) {
|
|
|
|
defer.resolve( a * b );
|
|
|
|
});
|
|
|
|
} ),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
|
|
|
piped.done(function( result ) {
|
|
|
|
value3 = result;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer.progress(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer.notify( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first progress value ok" );
|
|
|
|
strictEqual( value2, 3, "second progress value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
});
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
test( "jQuery.Deferred.then - context", function() {
|
2011-06-30 23:51:50 +00:00
|
|
|
|
|
|
|
expect(4);
|
|
|
|
|
|
|
|
var context = {};
|
|
|
|
|
2012-04-01 23:29:39 +00:00
|
|
|
jQuery.Deferred().resolveWith( context, [ 2 ] ).then(function( value ) {
|
2011-06-30 23:51:50 +00:00
|
|
|
return value * 3;
|
|
|
|
}).done(function( value ) {
|
|
|
|
strictEqual( this, context, "custom context correctly propagated" );
|
|
|
|
strictEqual( value, 6, "proper value received" );
|
|
|
|
});
|
|
|
|
|
|
|
|
var defer = jQuery.Deferred(),
|
2012-04-01 23:29:39 +00:00
|
|
|
piped = defer.then(function( value ) {
|
2011-06-30 23:51:50 +00:00
|
|
|
return value * 3;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer.resolve( 2 );
|
|
|
|
|
|
|
|
piped.done(function( value ) {
|
|
|
|
strictEqual( this.promise(), piped, "default context gets updated to latest defer in the chain" );
|
|
|
|
strictEqual( value, 6, "proper value received" );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
test( "jQuery.when" , function() {
|
2011-03-03 18:38:06 +00:00
|
|
|
|
|
|
|
expect( 23 );
|
|
|
|
|
|
|
|
// Some other objects
|
|
|
|
jQuery.each( {
|
|
|
|
|
|
|
|
"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,
|
|
|
|
"a plain object": {}
|
|
|
|
|
|
|
|
} , function( message , value ) {
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( jQuery.isFunction( jQuery.when( value ).done(function( resolveValue ) {
|
2011-03-03 18:38:06 +00:00
|
|
|
strictEqual( resolveValue , value , "Test the promise was resolved with " + message );
|
2011-04-07 03:49:32 +00:00
|
|
|
}).promise ) , "Test " + message + " triggers the creation of a new Promise" );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
|
|
|
} );
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
ok( jQuery.isFunction( jQuery.when().done(function( resolveValue ) {
|
2011-03-03 18:38:06 +00:00
|
|
|
strictEqual( resolveValue , undefined , "Test the promise was resolved with no parameter" );
|
2011-04-07 03:49:32 +00:00
|
|
|
}).promise ) , "Test calling when with no parameter triggers the creation of a new Promise" );
|
2011-03-03 18:38:06 +00:00
|
|
|
|
|
|
|
var cache, i;
|
|
|
|
|
|
|
|
for( i = 1 ; i < 4 ; i++ ) {
|
|
|
|
jQuery.when( cache || jQuery.Deferred( function() {
|
|
|
|
this.resolve( i );
|
2011-04-07 03:49:32 +00:00
|
|
|
}) ).done(function( value ) {
|
2011-03-03 18:38:06 +00:00
|
|
|
strictEqual( value , 1 , "Function executed" + ( i > 1 ? " only once" : "" ) );
|
|
|
|
cache = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-04-07 03:49:32 +00:00
|
|
|
test("jQuery.when - joined", function() {
|
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
expect(53);
|
2011-04-07 03:49:32 +00:00
|
|
|
|
|
|
|
var deferreds = {
|
|
|
|
value: 1,
|
|
|
|
success: jQuery.Deferred().resolve( 1 ),
|
|
|
|
error: jQuery.Deferred().reject( 0 ),
|
2011-09-12 15:37:41 +00:00
|
|
|
futureSuccess: jQuery.Deferred().notify( true ),
|
|
|
|
futureError: jQuery.Deferred().notify( true ),
|
|
|
|
notify: jQuery.Deferred().notify( true )
|
2011-04-07 03:49:32 +00:00
|
|
|
},
|
|
|
|
willSucceed = {
|
|
|
|
value: true,
|
|
|
|
success: true,
|
2011-09-12 15:37:41 +00:00
|
|
|
futureSuccess: true
|
|
|
|
},
|
|
|
|
willError = {
|
|
|
|
error: true,
|
|
|
|
futureError: true
|
|
|
|
},
|
|
|
|
willNotify = {
|
2011-04-07 03:49:32 +00:00
|
|
|
futureSuccess: true,
|
2011-09-12 15:37:41 +00:00
|
|
|
futureError: true,
|
|
|
|
notify: true
|
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 ],
|
2011-09-12 15:37:41 +00:00
|
|
|
expectedNotify = shouldNotify && [ willNotify[ id1 ], willNotify[ id2 ] ],
|
|
|
|
code = id1 + "/" + id2;
|
|
|
|
|
|
|
|
var promise = jQuery.when( defer1, defer2 ).done(function( a, b ) {
|
2011-04-07 03:49:32 +00:00
|
|
|
if ( shouldResolve ) {
|
2011-11-06 20:27:42 +00:00
|
|
|
deepEqual( [ a, b ], expected, code + " => resolve" );
|
2011-09-12 15:37:41 +00:00
|
|
|
} else {
|
|
|
|
ok( false , code + " => resolve" );
|
2011-04-07 03:49:32 +00:00
|
|
|
}
|
|
|
|
}).fail(function( a, b ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
if ( shouldError ) {
|
2011-11-06 20:27:42 +00:00
|
|
|
deepEqual( [ a, b ], expected, code + " => reject" );
|
2011-09-12 15:37:41 +00:00
|
|
|
} else {
|
|
|
|
ok( false , code + " => reject" );
|
2011-04-07 03:49:32 +00:00
|
|
|
}
|
2011-09-12 15:37:41 +00:00
|
|
|
}).progress(function progress( a, b ) {
|
2011-11-06 20:27:42 +00:00
|
|
|
deepEqual( [ a, b ], expectedNotify, code + " => progress" );
|
2011-04-07 03:49:32 +00:00
|
|
|
});
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
deferreds.futureSuccess.resolve( 1 );
|
|
|
|
deferreds.futureError.reject( 0 );
|
2011-03-03 18:38:06 +00:00
|
|
|
});
|