QUnit.test( "jQuery.when(thenable) - like Promise.resolve", function( assert ) {
"use strict";
- assert.expect( 56 );
-
- var slice = [].slice,
+ var CASES = 16,
+ slice = [].slice,
sentinel = { context: "explicit" },
eventuallyFulfilled = jQuery.Deferred().notify( true ),
eventuallyRejected = jQuery.Deferred().notify( true ),
+ secondaryFulfilled = jQuery.Deferred().resolve( eventuallyFulfilled ),
+ secondaryRejected = jQuery.Deferred().resolve( eventuallyRejected ),
inputs = {
promise: Promise.resolve( true ),
rejectedPromise: Promise.reject( false ),
deferred: jQuery.Deferred().resolve( true ),
eventuallyFulfilled: eventuallyFulfilled,
- secondaryFulfilled: jQuery.Deferred().resolve( eventuallyFulfilled ),
+ secondaryFulfilled: secondaryFulfilled,
+ eventuallySecondaryFulfilled: jQuery.Deferred().notify( true ),
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,
- secondaryRejected: jQuery.Deferred().resolve( eventuallyRejected ),
+ secondaryRejected: secondaryRejected,
+ eventuallySecondaryRejected: jQuery.Deferred().notify( true ),
multiRejectedDeferred: jQuery.Deferred().reject( "baz", "quux" ),
rejectedDeferredWith: jQuery.Deferred().rejectWith( sentinel, [ false ] ),
multiRejectedDeferredWith: jQuery.Deferred().rejectWith( sentinel, [ "baz", "quux" ] )
deferred: [ true ],
eventuallyFulfilled: [ true ],
secondaryFulfilled: [ true ],
+ eventuallySecondaryFulfilled: [ true ],
multiDeferred: [ "foo", "bar" ],
deferredWith: [ true ],
multiDeferredWith: [ "foo", "bar" ]
rejectedDeferred: [ false ],
eventuallyRejected: [ false ],
secondaryRejected: [ false ],
+ eventuallySecondaryRejected: [ false ],
multiRejectedDeferred: [ "baz", "quux" ],
rejectedDeferredWith: [ false ],
multiRejectedDeferredWith: [ "baz", "quux" ]
// Strict mode functions invoked without .call/.apply get global-object context
defaultContext = (function getDefaultContext() { return this; }).call(),
- done = assert.async( 28 );
+ done = assert.async( CASES * 2 );
+
+ assert.expect( CASES * 4 );
jQuery.each( inputs, function( message, value ) {
var code = "jQuery.when( " + message + " )",
setTimeout( function() {
eventuallyFulfilled.resolve( true );
eventuallyRejected.reject( false );
+ inputs.eventuallySecondaryFulfilled.resolve( secondaryFulfilled );
+ inputs.eventuallySecondaryRejected.resolve( secondaryRejected );
}, 50 );
} );
assert.ok( false, "Error on resolve" );
} );
} );
+
+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";
+} );