aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorRichard Gibson <richard.gibson@gmail.com>2016-05-04 15:30:24 -0400
committerRichard Gibson <richard.gibson@gmail.com>2016-05-09 12:14:22 -0400
commitde71e9755fc0b5d45ee3fa1bac5481c2466dad6e (patch)
treee417c398310472e91e47af8cb9cbe0f11ccdf887 /test
parente8825a529b97a27b8b2e40eaaa7773189642c772 (diff)
downloadjquery-de71e9755fc0b5d45ee3fa1bac5481c2466dad6e.tar.gz
jquery-de71e9755fc0b5d45ee3fa1bac5481c2466dad6e.zip
Deferred: Make jQuery.when synchronous when possible
Closes gh-3102 Fixes gh-3100 Closes gh-3105
Diffstat (limited to 'test')
-rw-r--r--test/unit/deferred.js46
1 files changed, 40 insertions, 6 deletions
diff --git a/test/unit/deferred.js b/test/unit/deferred.js
index 830103eeb..906555319 100644
--- a/test/unit/deferred.js
+++ b/test/unit/deferred.js
@@ -824,24 +824,27 @@ QUnit.test( "jQuery.when(nonThenable) - like Promise.resolve", function( assert
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" ] )
@@ -857,6 +860,7 @@ QUnit.test( "jQuery.when(thenable) - like Promise.resolve", function( assert ) {
deferred: [ true ],
eventuallyFulfilled: [ true ],
secondaryFulfilled: [ true ],
+ eventuallySecondaryFulfilled: [ true ],
multiDeferred: [ "foo", "bar" ],
deferredWith: [ true ],
multiDeferredWith: [ "foo", "bar" ]
@@ -866,6 +870,7 @@ QUnit.test( "jQuery.when(thenable) - like Promise.resolve", function( assert ) {
rejectedDeferred: [ false ],
eventuallyRejected: [ false ],
secondaryRejected: [ false ],
+ eventuallySecondaryRejected: [ false ],
multiRejectedDeferred: [ "baz", "quux" ],
rejectedDeferredWith: [ false ],
multiRejectedDeferredWith: [ "baz", "quux" ]
@@ -875,7 +880,9 @@ QUnit.test( "jQuery.when(thenable) - like Promise.resolve", function( assert ) {
// 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 + " )",
@@ -917,6 +924,8 @@ QUnit.test( "jQuery.when(thenable) - like Promise.resolve", function( assert ) {
setTimeout( function() {
eventuallyFulfilled.resolve( true );
eventuallyRejected.reject( false );
+ inputs.eventuallySecondaryFulfilled.resolve( secondaryFulfilled );
+ inputs.eventuallySecondaryRejected.resolve( secondaryRejected );
}, 50 );
} );
@@ -1049,3 +1058,28 @@ QUnit.test( "jQuery.when - notify does not affect resolved", function( assert )
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";
+} );