]> source.dussan.org Git - jquery.git/commitdiff
deferred.promise(obj) should work with non-objects. Fixes #12521. Much needed unit...
authorjaubourg <j@ubourg.net>
Thu, 13 Sep 2012 13:44:12 +0000 (15:44 +0200)
committerjaubourg <j@ubourg.net>
Thu, 13 Sep 2012 13:44:12 +0000 (15:44 +0200)
src/deferred.js
test/unit/deferred.js

index f0b1f96e65ffe477f66d4d79025394cb267f5062..e525fc06871ce053990853c7f69070e53cf22206 100644 (file)
@@ -44,7 +44,7 @@ jQuery.extend({
                                // Get a promise for this deferred
                                // If obj is provided, the promise aspect is added to the object
                                promise: function( obj ) {
-                                       return typeof obj === "object" ? jQuery.extend( obj, promise ) : promise;
+                                       return obj != null ? jQuery.extend( obj, promise ) : promise;
                                }
                        },
                        deferred = {};
index 8f512dac7acf531cf823f787d6ed1b0d41eaf2aa..a786a05a9332264ba43f50cbdfad6347a2a054ac 100644 (file)
@@ -8,7 +8,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
 
        test("jQuery.Deferred" + withNew, function() {
 
-               expect( 21 );
+               expect( 23 );
 
                var defer = createDeferred();
 
@@ -39,6 +39,22 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
                        strictEqual( value , "done" , "Passed function executed" );
                });
 
+               createDeferred(function( defer ) {
+                       var promise = defer.promise(),
+                               func = function() {},
+                               funcPromise = defer.promise( func );
+                       strictEqual( defer.promise(), promise, "promise is always the same" );
+                       strictEqual( funcPromise, func, "non objects get extended" );
+                       jQuery.each( promise, function( key, value ) {
+                               if ( !jQuery.isFunction( promise[ key ] ) ) {
+                                       ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" );
+                               }
+                               if ( promise[ key ] !== func[ key ] ) {
+                                       strictEqual( func[ key ], promise[ key ], key + " is the same" );
+                               }
+                       });
+               });
+
                jQuery.expandedEach = jQuery.each;
                jQuery.expandedEach( "resolve reject".split( " " ), function( _, change ) {
                        createDeferred( function( defer ) {
@@ -59,6 +75,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
        });
 } );
 
+
 test( "jQuery.Deferred - chainability", function() {
 
        var defer = jQuery.Deferred();