1 module("deferred", { teardown
: moduleTeardown
});
3 test("jQuery._Deferred()", function() {
11 deferred
= jQuery
._Deferred();
15 deferred
.done( function( value
) {
16 equals( value
, "value" , "Test pre-resolve callback" );
20 deferred
.resolve( "value" );
22 ok( test
, "Test pre-resolve callbacks called right away" );
26 deferred
.done( function( value
) {
27 equals( value
, "value" , "Test post-resolve callback" );
31 ok( test
, "Test post-resolve callbacks called right away" );
37 deferred
.done( function() {
38 ok( false , "Cancel was ignored" );
42 ok( test
, "Test cancel" );
44 deferred
= jQuery
._Deferred().resolve();
47 deferred
.done( function() {
50 ok( true , "Test deferred do not cancel on exception" );
53 strictEqual( e
, "Error" , "Test deferred propagates exceptions");
58 deferred
= jQuery
._Deferred().done( function() {
68 strictEqual( test
, "AB" , "Test multiple done parameters" );
72 deferred
.done( function() {
74 deferred
.done( function() {
87 strictEqual( test
, "ABC" , "Test done callbacks order" );
89 deferred
= jQuery
._Deferred();
91 deferred
.resolveWith( jQuery
, [ document
] ).done( function( doc
) {
92 ok( this === jQuery
&& arguments
.length
=== 1 && doc
=== document
, "Test fire context & args" );
96 deferred
= jQuery
._Deferred();
97 deferred
.resolveWith().done(function() {
98 ok( true, "Test resolveWith can be called with no argument" );
102 test("jQuery.Deferred()", function() {
106 jQuery
.Deferred( function( defer
) {
107 strictEqual( this , defer
, "Defer passed as this & first argument" );
108 this.resolve( "done" );
109 }).then( function( value
) {
110 strictEqual( value
, "done" , "Passed function executed" );
113 jQuery
.Deferred().resolve().then( function() {
114 ok( true , "Success on resolve" );
116 ok( false , "Error on resolve" );
119 jQuery
.Deferred().reject().then( function() {
120 ok( false , "Success on reject" );
122 ok( true , "Error on reject" );
125 ( new jQuery
.Deferred( function( defer
) {
126 strictEqual( this , defer
, "Defer passed as this & first argument (new)" );
127 this.resolve( "done" );
128 }) ).then( function( value
) {
129 strictEqual( value
, "done" , "Passed function executed (new)" );
132 ( new jQuery
.Deferred() ).resolve().then( function() {
133 ok( true , "Success on resolve (new)" );
135 ok( false , "Error on resolve (new)" );
138 ( new jQuery
.Deferred() ).reject().then( function() {
139 ok( false , "Success on reject (new)" );
141 ok( true , "Error on reject (new)" );
144 var tmp
= jQuery
.Deferred();
146 strictEqual( tmp
.promise() , tmp
.promise() , "Test deferred always return same promise" );
147 strictEqual( tmp
.promise() , tmp
.promise().promise() , "Test deferred's promise always return same promise as deferred" );
150 test("jQuery.when()", function() {
154 // Some other objects
157 "an empty string": "",
158 "a non-empty string": "some string",
160 "a number other than zero": 1,
164 "undefined": undefined,
167 } , function( message
, value
) {
169 ok( jQuery
.isFunction( jQuery
.when( value
).then( function( resolveValue
) {
170 strictEqual( resolveValue
, value
, "Test the promise was resolved with " + message
);
171 } ).promise
) , "Test " + message
+ " triggers the creation of a new Promise" );
175 ok( jQuery
.isFunction( jQuery
.when().then( function( resolveValue
) {
176 strictEqual( resolveValue
, undefined , "Test the promise was resolved with no parameter" );
177 } ).promise
) , "Test calling when with no parameter triggers the creation of a new Promise" );
181 for( i
= 1 ; i
< 4 ; i
++ ) {
182 jQuery
.when( cache
|| jQuery
.Deferred( function() {
184 }) ).then( function( value
) {
185 strictEqual( value
, 1 , "Function executed" + ( i
> 1 ? " only once" : "" ) );
188 ok( false , "Fail called" );
193 test("jQuery.when() - joined", function() {
197 jQuery
.when( 1, 2, 3 ).done( function( a
, b
, c
) {
198 strictEqual( a
, 1 , "Test first param is first resolved value - non-observables" );
199 strictEqual( b
, 2 , "Test second param is second resolved value - non-observables" );
200 strictEqual( c
, 3 , "Test third param is third resolved value - non-observables" );
201 }).fail( function() {
202 ok( false , "Test the created deferred was resolved - non-observables");
205 var successDeferred
= jQuery
.Deferred().resolve( 1 , 2 , 3 ),
206 errorDeferred
= jQuery
.Deferred().reject( "error" , "errorParam" );
208 jQuery
.when( 1 , successDeferred
, 3 ).done( function( a
, b
, c
) {
209 strictEqual( a
, 1 , "Test first param is first resolved value - resolved observable" );
210 same( b
, [ 1 , 2 , 3 ] , "Test second param is second resolved value - resolved observable" );
211 strictEqual( c
, 3 , "Test third param is third resolved value - resolved observable" );
212 }).fail( function() {
213 ok( false , "Test the created deferred was resolved - resolved observable");
216 jQuery
.when( 1 , errorDeferred
, 3 ).done( function() {
217 ok( false , "Test the created deferred was rejected - rejected observable");
218 }).fail( function( error
, errorParam
) {
219 strictEqual( error
, "error" , "Test first param is first rejected value - rejected observable" );
220 strictEqual( errorParam
, "errorParam" , "Test second param is second rejected value - rejected observable" );