Add Deferred.progress() unit tests and fixes some progress related typos and bugs.
This commit is contained in:
parent
bf04081b6c
commit
5f9dff6587
|
@ -1,7 +1,7 @@
|
|||
(function( jQuery ) {
|
||||
|
||||
var // Promise methods
|
||||
promiseMethods = "done fail progress isResolved isRejected promise then always pipe".split( " " ),
|
||||
promiseMethods = "done removeDone fail removeFail progress removeProgress isResolved isRejected promise then always pipe".split( " " ),
|
||||
// Static reference to slice
|
||||
sliceDeferred = [].slice;
|
||||
|
||||
|
@ -25,7 +25,7 @@ jQuery.extend({
|
|||
reject: failList.fire,
|
||||
rejectWith: failList.fireWith,
|
||||
ping: progressList.fire,
|
||||
pingWith: progressList.pingWith,
|
||||
pingWith: progressList.fireWith,
|
||||
isResolved: doneList.fired,
|
||||
isRejected: failList.fired,
|
||||
|
||||
|
@ -102,7 +102,8 @@ jQuery.extend({
|
|||
pCount = length,
|
||||
deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise ) ?
|
||||
firstParam :
|
||||
jQuery.Deferred();
|
||||
jQuery.Deferred(),
|
||||
promise = deferred.promise();
|
||||
function resolveFunc( i ) {
|
||||
return function( value ) {
|
||||
args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
|
||||
|
@ -114,9 +115,7 @@ jQuery.extend({
|
|||
function progressFunc( i ) {
|
||||
return function( value ) {
|
||||
pValues[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
|
||||
if ( !( --count ) ) {
|
||||
deferred.pingWith( deferred, pValue );
|
||||
}
|
||||
deferred.pingWith( promise, pValues );
|
||||
};
|
||||
}
|
||||
if ( length > 1 ) {
|
||||
|
@ -133,7 +132,7 @@ jQuery.extend({
|
|||
} else if ( deferred !== firstParam ) {
|
||||
deferred.resolveWith( deferred, length ? [ firstParam ] : [] );
|
||||
}
|
||||
return deferred.promise();
|
||||
return promise;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
|
|||
|
||||
test("jQuery.Deferred" + withNew, function() {
|
||||
|
||||
expect( 8 );
|
||||
expect( 14 );
|
||||
|
||||
createDeferred().resolve().then( function() {
|
||||
ok( true , "Success on resolve" );
|
||||
|
@ -34,6 +34,20 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
|
|||
}).then( function( value ) {
|
||||
strictEqual( value , "done" , "Passed function executed" );
|
||||
});
|
||||
|
||||
jQuery.each( "resolve reject".split( " " ), function( _, change ) {
|
||||
createDeferred( function( defer ) {
|
||||
var checked = 0;
|
||||
defer.progress(function( value ) {
|
||||
strictEqual( value, checked, "Progress: right value (" + value + ") received" )
|
||||
});
|
||||
for( checked = 0; checked < 3 ; checked++ ) {
|
||||
defer.ping( checked );
|
||||
}
|
||||
defer[ change ]();
|
||||
defer.ping();
|
||||
});
|
||||
});
|
||||
});
|
||||
} );
|
||||
|
||||
|
@ -101,6 +115,34 @@ test( "jQuery.Deferred.pipe - filtering (fail)", function() {
|
|||
} );
|
||||
});
|
||||
|
||||
test( "jQuery.Deferred.pipe - filtering (progress)", function() {
|
||||
|
||||
expect(3);
|
||||
|
||||
var defer = jQuery.Deferred(),
|
||||
piped = defer.pipe( null, null, function( a, b ) {
|
||||
return a * b;
|
||||
} ),
|
||||
value1,
|
||||
value2,
|
||||
value3;
|
||||
|
||||
piped.progress(function( result ) {
|
||||
value3 = result;
|
||||
});
|
||||
|
||||
defer.progress(function( a, b ) {
|
||||
value1 = a;
|
||||
value2 = b;
|
||||
});
|
||||
|
||||
defer.ping( 2, 3 );
|
||||
|
||||
strictEqual( value1, 2, "first reject value ok" );
|
||||
strictEqual( value2, 3, "second reject value ok" );
|
||||
strictEqual( value3, 6, "result of filter ok" );
|
||||
});
|
||||
|
||||
test( "jQuery.Deferred.pipe - deferred (done)", function() {
|
||||
|
||||
expect(3);
|
||||
|
@ -161,6 +203,36 @@ test( "jQuery.Deferred.pipe - deferred (fail)", function() {
|
|||
strictEqual( value3, 6, "result of filter ok" );
|
||||
});
|
||||
|
||||
test( "jQuery.Deferred.pipe - deferred (progress)", function() {
|
||||
|
||||
expect(3);
|
||||
|
||||
var defer = jQuery.Deferred(),
|
||||
piped = defer.pipe( null, null, function( a, b ) {
|
||||
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.ping( 2, 3 );
|
||||
|
||||
strictEqual( value1, 2, "first reject value ok" );
|
||||
strictEqual( value2, 3, "second reject value ok" );
|
||||
strictEqual( value3, 6, "result of filter ok" );
|
||||
});
|
||||
|
||||
test( "jQuery.when" , function() {
|
||||
|
||||
expect( 23 );
|
||||
|
@ -204,36 +276,54 @@ test( "jQuery.when" , function() {
|
|||
|
||||
test("jQuery.when - joined", function() {
|
||||
|
||||
expect(25);
|
||||
expect(50);
|
||||
|
||||
var deferreds = {
|
||||
value: 1,
|
||||
success: jQuery.Deferred().resolve( 1 ),
|
||||
error: jQuery.Deferred().reject( 0 ),
|
||||
futureSuccess: jQuery.Deferred(),
|
||||
futureError: jQuery.Deferred()
|
||||
futureSuccess: jQuery.Deferred().ping( true ),
|
||||
futureError: jQuery.Deferred().ping( true ),
|
||||
ping: jQuery.Deferred().ping( true )
|
||||
},
|
||||
willSucceed = {
|
||||
value: true,
|
||||
success: true,
|
||||
error: false,
|
||||
futureSuccess: true
|
||||
},
|
||||
willError = {
|
||||
error: true,
|
||||
futureError: true
|
||||
},
|
||||
willPing = {
|
||||
futureSuccess: true,
|
||||
futureError: false
|
||||
futureError: true,
|
||||
ping: true
|
||||
};
|
||||
|
||||
jQuery.each( deferreds, function( id1, defer1 ) {
|
||||
jQuery.each( deferreds, function( id2, defer2 ) {
|
||||
var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
|
||||
shouldError = willError[ id1 ] || willError[ id2 ],
|
||||
shouldPing = willPing[ id1 ] || willPing[ id2 ],
|
||||
expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
|
||||
expectedPing = shouldPing && [ willPing[ id1 ], willPing[ id2 ] ],
|
||||
code = id1 + "/" + id2;
|
||||
jQuery.when( defer1, defer2 ).done(function( a, b ) {
|
||||
|
||||
var promise = jQuery.when( defer1, defer2 ).done(function( a, b ) {
|
||||
if ( shouldResolve ) {
|
||||
same( [ a, b ], expected, code + " => resolve" );
|
||||
} else {
|
||||
ok( false , code + " => resolve" );
|
||||
}
|
||||
}).fail(function( a, b ) {
|
||||
if ( !shouldResolve ) {
|
||||
same( [ a, b ], expected, code + " => resolve" );
|
||||
if ( shouldError ) {
|
||||
same( [ a, b ], expected, code + " => reject" );
|
||||
} else {
|
||||
ok( false , code + " => reject" );
|
||||
}
|
||||
}).progress(function progress( a, b ) {
|
||||
same( [ a, b ], expectedPing, code + " => progress" );
|
||||
});
|
||||
} );
|
||||
} );
|
||||
|
|
Loading…
Reference in a new issue