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 ) {
|
(function( jQuery ) {
|
||||||
|
|
||||||
var // Promise methods
|
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
|
// Static reference to slice
|
||||||
sliceDeferred = [].slice;
|
sliceDeferred = [].slice;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ jQuery.extend({
|
||||||
reject: failList.fire,
|
reject: failList.fire,
|
||||||
rejectWith: failList.fireWith,
|
rejectWith: failList.fireWith,
|
||||||
ping: progressList.fire,
|
ping: progressList.fire,
|
||||||
pingWith: progressList.pingWith,
|
pingWith: progressList.fireWith,
|
||||||
isResolved: doneList.fired,
|
isResolved: doneList.fired,
|
||||||
isRejected: failList.fired,
|
isRejected: failList.fired,
|
||||||
|
|
||||||
|
@ -102,7 +102,8 @@ jQuery.extend({
|
||||||
pCount = length,
|
pCount = length,
|
||||||
deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise ) ?
|
deferred = length <= 1 && firstParam && jQuery.isFunction( firstParam.promise ) ?
|
||||||
firstParam :
|
firstParam :
|
||||||
jQuery.Deferred();
|
jQuery.Deferred(),
|
||||||
|
promise = deferred.promise();
|
||||||
function resolveFunc( i ) {
|
function resolveFunc( i ) {
|
||||||
return function( value ) {
|
return function( value ) {
|
||||||
args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
|
args[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
|
||||||
|
@ -114,9 +115,7 @@ jQuery.extend({
|
||||||
function progressFunc( i ) {
|
function progressFunc( i ) {
|
||||||
return function( value ) {
|
return function( value ) {
|
||||||
pValues[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
|
pValues[ i ] = arguments.length > 1 ? sliceDeferred.call( arguments, 0 ) : value;
|
||||||
if ( !( --count ) ) {
|
deferred.pingWith( promise, pValues );
|
||||||
deferred.pingWith( deferred, pValue );
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if ( length > 1 ) {
|
if ( length > 1 ) {
|
||||||
|
@ -133,7 +132,7 @@ jQuery.extend({
|
||||||
} else if ( deferred !== firstParam ) {
|
} else if ( deferred !== firstParam ) {
|
||||||
deferred.resolveWith( deferred, length ? [ 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() {
|
test("jQuery.Deferred" + withNew, function() {
|
||||||
|
|
||||||
expect( 8 );
|
expect( 14 );
|
||||||
|
|
||||||
createDeferred().resolve().then( function() {
|
createDeferred().resolve().then( function() {
|
||||||
ok( true , "Success on resolve" );
|
ok( true , "Success on resolve" );
|
||||||
|
@ -34,6 +34,20 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
|
||||||
}).then( function( value ) {
|
}).then( function( value ) {
|
||||||
strictEqual( value , "done" , "Passed function executed" );
|
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() {
|
test( "jQuery.Deferred.pipe - deferred (done)", function() {
|
||||||
|
|
||||||
expect(3);
|
expect(3);
|
||||||
|
@ -161,6 +203,36 @@ test( "jQuery.Deferred.pipe - deferred (fail)", function() {
|
||||||
strictEqual( value3, 6, "result of filter ok" );
|
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() {
|
test( "jQuery.when" , function() {
|
||||||
|
|
||||||
expect( 23 );
|
expect( 23 );
|
||||||
|
@ -204,36 +276,54 @@ test( "jQuery.when" , function() {
|
||||||
|
|
||||||
test("jQuery.when - joined", function() {
|
test("jQuery.when - joined", function() {
|
||||||
|
|
||||||
expect(25);
|
expect(50);
|
||||||
|
|
||||||
var deferreds = {
|
var deferreds = {
|
||||||
value: 1,
|
value: 1,
|
||||||
success: jQuery.Deferred().resolve( 1 ),
|
success: jQuery.Deferred().resolve( 1 ),
|
||||||
error: jQuery.Deferred().reject( 0 ),
|
error: jQuery.Deferred().reject( 0 ),
|
||||||
futureSuccess: jQuery.Deferred(),
|
futureSuccess: jQuery.Deferred().ping( true ),
|
||||||
futureError: jQuery.Deferred()
|
futureError: jQuery.Deferred().ping( true ),
|
||||||
|
ping: jQuery.Deferred().ping( true )
|
||||||
},
|
},
|
||||||
willSucceed = {
|
willSucceed = {
|
||||||
value: true,
|
value: true,
|
||||||
success: true,
|
success: true,
|
||||||
error: false,
|
futureSuccess: true
|
||||||
|
},
|
||||||
|
willError = {
|
||||||
|
error: true,
|
||||||
|
futureError: true
|
||||||
|
},
|
||||||
|
willPing = {
|
||||||
futureSuccess: true,
|
futureSuccess: true,
|
||||||
futureError: false
|
futureError: true,
|
||||||
|
ping: true
|
||||||
};
|
};
|
||||||
|
|
||||||
jQuery.each( deferreds, function( id1, defer1 ) {
|
jQuery.each( deferreds, function( id1, defer1 ) {
|
||||||
jQuery.each( deferreds, function( id2, defer2 ) {
|
jQuery.each( deferreds, function( id2, defer2 ) {
|
||||||
var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
|
var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
|
||||||
|
shouldError = willError[ id1 ] || willError[ id2 ],
|
||||||
|
shouldPing = willPing[ id1 ] || willPing[ id2 ],
|
||||||
expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
|
expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
|
||||||
code = id1 + "/" + id2;
|
expectedPing = shouldPing && [ willPing[ id1 ], willPing[ id2 ] ],
|
||||||
jQuery.when( defer1, defer2 ).done(function( a, b ) {
|
code = id1 + "/" + id2;
|
||||||
|
|
||||||
|
var promise = jQuery.when( defer1, defer2 ).done(function( a, b ) {
|
||||||
if ( shouldResolve ) {
|
if ( shouldResolve ) {
|
||||||
same( [ a, b ], expected, code + " => resolve" );
|
same( [ a, b ], expected, code + " => resolve" );
|
||||||
|
} else {
|
||||||
|
ok( false , code + " => resolve" );
|
||||||
}
|
}
|
||||||
}).fail(function( a, b ) {
|
}).fail(function( a, b ) {
|
||||||
if ( !shouldResolve ) {
|
if ( shouldError ) {
|
||||||
same( [ a, b ], expected, code + " => resolve" );
|
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