2011-03-03 19:38:06 +01:00
|
|
|
module("deferred", { teardown: moduleTeardown });
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
function createDeferred() {
|
|
|
|
return withNew ? new jQuery._Deferred() : jQuery._Deferred();
|
|
|
|
}
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test("jQuery._Deferred" + withNew, function() {
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
expect( 11 );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
var deferred,
|
|
|
|
object,
|
|
|
|
test;
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred = createDeferred();
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test = false;
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred.done( function( value ) {
|
|
|
|
equals( value , "value" , "Test pre-resolve callback" );
|
|
|
|
test = true;
|
|
|
|
} );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred.resolve( "value" );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
ok( test , "Test pre-resolve callbacks called right away" );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test = false;
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred.done( function( value ) {
|
|
|
|
equals( value , "value" , "Test post-resolve callback" );
|
|
|
|
test = true;
|
|
|
|
} );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
ok( test , "Test post-resolve callbacks called right away" );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred.cancel();
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test = true;
|
2011-03-03 19:38:06 +01:00
|
|
|
|
|
|
|
deferred.done( function() {
|
2011-04-07 05:49:32 +02:00
|
|
|
ok( false , "Cancel was ignored" );
|
|
|
|
test = false;
|
2011-03-03 19:38:06 +01:00
|
|
|
} );
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
ok( test , "Test cancel" );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred = createDeferred().resolve();
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
try {
|
|
|
|
deferred.done( function() {
|
|
|
|
throw "Error";
|
|
|
|
} , function() {
|
|
|
|
ok( true , "Test deferred do not cancel on exception" );
|
|
|
|
} );
|
|
|
|
} catch( e ) {
|
|
|
|
strictEqual( e , "Error" , "Test deferred propagates exceptions");
|
|
|
|
deferred.done();
|
|
|
|
}
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test = "";
|
|
|
|
deferred = createDeferred().done( function() {
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test += "A";
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
}, function() {
|
|
|
|
|
|
|
|
test += "B";
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
} ).resolve();
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
strictEqual( test , "AB" , "Test multiple done parameters" );
|
|
|
|
|
|
|
|
test = "";
|
2011-03-03 19:38:06 +01:00
|
|
|
|
|
|
|
deferred.done( function() {
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred.done( function() {
|
|
|
|
|
|
|
|
test += "C";
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
test += "A";
|
|
|
|
|
|
|
|
}, function() {
|
|
|
|
|
|
|
|
test += "B";
|
2011-03-03 19:38:06 +01:00
|
|
|
} );
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
strictEqual( test , "ABC" , "Test done callbacks order" );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred = createDeferred();
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
deferred.resolveWith( jQuery , [ document ] ).done( function( doc ) {
|
|
|
|
ok( this === jQuery && arguments.length === 1 && doc === document , "Test fire context & args" );
|
|
|
|
});
|
|
|
|
|
|
|
|
// #8421
|
|
|
|
deferred = createDeferred();
|
|
|
|
deferred.resolveWith().done(function() {
|
|
|
|
ok( true, "Test resolveWith can be called with no argument" );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} );
|
|
|
|
|
|
|
|
jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
|
|
|
|
|
|
|
|
function createDeferred( fn ) {
|
|
|
|
return withNew ? new jQuery.Deferred( fn ) : jQuery.Deferred( fn );
|
|
|
|
}
|
|
|
|
|
|
|
|
test("jQuery.Deferred" + withNew, function() {
|
|
|
|
|
|
|
|
expect( 8 );
|
|
|
|
|
|
|
|
createDeferred().resolve().then( function() {
|
|
|
|
ok( true , "Success on resolve" );
|
|
|
|
ok( this.isResolved(), "Deferred is resolved" );
|
|
|
|
}, function() {
|
|
|
|
ok( false , "Error on resolve" );
|
|
|
|
}).always( function() {
|
|
|
|
ok( true , "Always callback on resolve" );
|
|
|
|
});
|
|
|
|
|
|
|
|
createDeferred().reject().then( function() {
|
|
|
|
ok( false , "Success on reject" );
|
|
|
|
}, function() {
|
|
|
|
ok( true , "Error on reject" );
|
|
|
|
ok( this.isRejected(), "Deferred is rejected" );
|
|
|
|
}).always( function() {
|
|
|
|
ok( true , "Always callback on reject" );
|
|
|
|
});
|
|
|
|
|
|
|
|
createDeferred( function( defer ) {
|
|
|
|
ok( this === defer , "Defer passed as this & first argument" );
|
|
|
|
this.resolve( "done" );
|
|
|
|
}).then( function( value ) {
|
|
|
|
strictEqual( value , "done" , "Passed function executed" );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
test( "jQuery.Deferred.pipe - filtering (done)", function() {
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
expect(3);
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
var defer = jQuery.Deferred(),
|
2011-04-08 17:35:08 +02:00
|
|
|
piped = defer.pipe(function( a, b ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
return a * b;
|
|
|
|
}),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
piped.done(function( result ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
value3 = result;
|
|
|
|
});
|
|
|
|
|
|
|
|
defer.done(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
defer.resolve( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first resolve value ok" );
|
|
|
|
strictEqual( value2, 3, "second resolve value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
jQuery.Deferred().reject().pipe(function() {
|
|
|
|
ok( false, "pipe should not be called on reject" );
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
test( "jQuery.Deferred.pipe - filtering (fail)", function() {
|
2011-04-07 05:49:32 +02:00
|
|
|
|
|
|
|
expect(3);
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
var defer = jQuery.Deferred(),
|
2011-04-08 17:35:08 +02:00
|
|
|
piped = defer.pipe( null, function( a, b ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
return a * b;
|
|
|
|
} ),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
piped.fail(function( result ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
value3 = result;
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
defer.fail(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
defer.reject( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
jQuery.Deferred().resolve().pipe( null, function() {
|
|
|
|
ok( false, "pipe should not be called on resolve" );
|
2011-04-07 05:49:32 +02:00
|
|
|
} );
|
|
|
|
});
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
test( "jQuery.Deferred.pipe - deferred (done)", function() {
|
2011-04-07 05:49:32 +02:00
|
|
|
|
|
|
|
expect(3);
|
|
|
|
|
|
|
|
var defer = jQuery.Deferred(),
|
2011-04-08 17:35:08 +02:00
|
|
|
piped = defer.pipe(function( a, b ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
return jQuery.Deferred(function( defer ) {
|
|
|
|
defer.reject( a * b );
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
piped.fail(function( result ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
value3 = result;
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
defer.done(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
defer.resolve( 2, 3 );
|
|
|
|
|
|
|
|
strictEqual( value1, 2, "first resolve value ok" );
|
|
|
|
strictEqual( value2, 3, "second resolve value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
|
|
|
});
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
test( "jQuery.Deferred.pipe - deferred (fail)", function() {
|
2011-04-07 05:49:32 +02:00
|
|
|
|
|
|
|
expect(3);
|
|
|
|
|
|
|
|
var defer = jQuery.Deferred(),
|
2011-04-08 17:35:08 +02:00
|
|
|
piped = defer.pipe( null, function( a, b ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
return jQuery.Deferred(function( defer ) {
|
|
|
|
defer.resolve( a * b );
|
|
|
|
});
|
|
|
|
} ),
|
|
|
|
value1,
|
|
|
|
value2,
|
|
|
|
value3;
|
|
|
|
|
2011-04-08 17:35:08 +02:00
|
|
|
piped.done(function( result ) {
|
2011-04-07 05:49:32 +02:00
|
|
|
value3 = result;
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
defer.fail(function( a, b ) {
|
|
|
|
value1 = a;
|
|
|
|
value2 = b;
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
defer.reject( 2, 3 );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
strictEqual( value1, 2, "first reject value ok" );
|
|
|
|
strictEqual( value2, 3, "second reject value ok" );
|
|
|
|
strictEqual( value3, 6, "result of filter ok" );
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test( "jQuery.when" , function() {
|
2011-03-03 19:38:06 +01:00
|
|
|
|
|
|
|
expect( 23 );
|
|
|
|
|
|
|
|
// Some other objects
|
|
|
|
jQuery.each( {
|
|
|
|
|
|
|
|
"an empty string": "",
|
|
|
|
"a non-empty string": "some string",
|
|
|
|
"zero": 0,
|
|
|
|
"a number other than zero": 1,
|
|
|
|
"true": true,
|
|
|
|
"false": false,
|
|
|
|
"null": null,
|
|
|
|
"undefined": undefined,
|
|
|
|
"a plain object": {}
|
|
|
|
|
|
|
|
} , function( message , value ) {
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
ok( jQuery.isFunction( jQuery.when( value ).done(function( resolveValue ) {
|
2011-03-03 19:38:06 +01:00
|
|
|
strictEqual( resolveValue , value , "Test the promise was resolved with " + message );
|
2011-04-07 05:49:32 +02:00
|
|
|
}).promise ) , "Test " + message + " triggers the creation of a new Promise" );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
|
|
|
} );
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
ok( jQuery.isFunction( jQuery.when().done(function( resolveValue ) {
|
2011-03-03 19:38:06 +01:00
|
|
|
strictEqual( resolveValue , undefined , "Test the promise was resolved with no parameter" );
|
2011-04-07 05:49:32 +02:00
|
|
|
}).promise ) , "Test calling when with no parameter triggers the creation of a new Promise" );
|
2011-03-03 19:38:06 +01:00
|
|
|
|
|
|
|
var cache, i;
|
|
|
|
|
|
|
|
for( i = 1 ; i < 4 ; i++ ) {
|
|
|
|
jQuery.when( cache || jQuery.Deferred( function() {
|
|
|
|
this.resolve( i );
|
2011-04-07 05:49:32 +02:00
|
|
|
}) ).done(function( value ) {
|
2011-03-03 19:38:06 +01:00
|
|
|
strictEqual( value , 1 , "Function executed" + ( i > 1 ? " only once" : "" ) );
|
|
|
|
cache = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-04-07 05:49:32 +02:00
|
|
|
test("jQuery.when - joined", function() {
|
|
|
|
|
|
|
|
expect(25);
|
|
|
|
|
|
|
|
var deferreds = {
|
|
|
|
value: 1,
|
|
|
|
success: jQuery.Deferred().resolve( 1 ),
|
|
|
|
error: jQuery.Deferred().reject( 0 ),
|
|
|
|
futureSuccess: jQuery.Deferred(),
|
|
|
|
futureError: jQuery.Deferred()
|
|
|
|
},
|
|
|
|
willSucceed = {
|
|
|
|
value: true,
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
futureSuccess: true,
|
|
|
|
futureError: false
|
|
|
|
};
|
|
|
|
|
|
|
|
jQuery.each( deferreds, function( id1, defer1 ) {
|
|
|
|
jQuery.each( deferreds, function( id2, defer2 ) {
|
|
|
|
var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
|
|
|
|
expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
|
|
|
|
code = id1 + "/" + id2;
|
|
|
|
jQuery.when( defer1, defer2 ).done(function( a, b ) {
|
|
|
|
if ( shouldResolve ) {
|
|
|
|
same( [ a, b ], expected, code + " => resolve" );
|
|
|
|
}
|
|
|
|
}).fail(function( a, b ) {
|
|
|
|
if ( !shouldResolve ) {
|
|
|
|
same( [ a, b ], expected, code + " => resolve" );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
deferreds.futureSuccess.resolve( 1 );
|
|
|
|
deferreds.futureError.reject( 0 );
|
2011-03-03 19:38:06 +01:00
|
|
|
});
|