deferred.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. module( "deferred", {
  2. teardown: moduleTeardown
  3. });
  4. jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
  5. function createDeferred( fn ) {
  6. return withNew ? new jQuery.Deferred( fn ) : jQuery.Deferred( fn );
  7. }
  8. test( "jQuery.Deferred" + withNew, function() {
  9. expect( 23 );
  10. var defer = createDeferred();
  11. strictEqual( defer.pipe, defer.then, "pipe is an alias of then" );
  12. createDeferred().resolve().done(function() {
  13. ok( true, "Success on resolve" );
  14. strictEqual( this.state(), "resolved", "Deferred is resolved (state)" );
  15. }).fail(function() {
  16. ok( false, "Error on resolve" );
  17. }).always(function() {
  18. ok( true, "Always callback on resolve" );
  19. });
  20. createDeferred().reject().done(function() {
  21. ok( false, "Success on reject" );
  22. }).fail(function() {
  23. ok( true, "Error on reject" );
  24. strictEqual( this.state(), "rejected", "Deferred is rejected (state)" );
  25. }).always(function() {
  26. ok( true, "Always callback on reject" );
  27. });
  28. createDeferred(function( defer ) {
  29. ok( this === defer, "Defer passed as this & first argument" );
  30. this.resolve("done");
  31. }).done(function( value ) {
  32. strictEqual( value, "done", "Passed function executed" );
  33. });
  34. createDeferred(function( defer ) {
  35. var promise = defer.promise(),
  36. func = function() {},
  37. funcPromise = defer.promise( func );
  38. strictEqual( defer.promise(), promise, "promise is always the same" );
  39. strictEqual( funcPromise, func, "non objects get extended" );
  40. jQuery.each( promise, function( key ) {
  41. if ( !jQuery.isFunction( promise[ key ] ) ) {
  42. ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" );
  43. }
  44. if ( promise[ key ] !== func[ key ] ) {
  45. strictEqual( func[ key ], promise[ key ], key + " is the same" );
  46. }
  47. });
  48. });
  49. jQuery.expandedEach = jQuery.each;
  50. jQuery.expandedEach( "resolve reject".split(" "), function( _, change ) {
  51. createDeferred(function( defer ) {
  52. strictEqual( defer.state(), "pending", "pending after creation" );
  53. var checked = 0;
  54. defer.progress(function( value ) {
  55. strictEqual( value, checked, "Progress: right value (" + value + ") received" );
  56. });
  57. for ( checked = 0; checked < 3; checked++ ) {
  58. defer.notify( checked );
  59. }
  60. strictEqual( defer.state(), "pending", "pending after notification" );
  61. defer[ change ]();
  62. notStrictEqual( defer.state(), "pending", "not pending after " + change );
  63. defer.notify();
  64. });
  65. });
  66. });
  67. });
  68. test( "jQuery.Deferred - chainability", function() {
  69. var defer = jQuery.Deferred();
  70. expect( 10 );
  71. jQuery.expandedEach = jQuery.each;
  72. jQuery.expandedEach( "resolve reject notify resolveWith rejectWith notifyWith done fail progress always".split(" "), function( _, method ) {
  73. var object = {
  74. m: defer[ method ]
  75. };
  76. strictEqual( object.m(), object, method + " is chainable" );
  77. });
  78. });
  79. test( "jQuery.Deferred.then - filtering (done)", function() {
  80. expect( 4 );
  81. var value1, value2, value3,
  82. defer = jQuery.Deferred(),
  83. piped = defer.then(function( a, b ) {
  84. return a * b;
  85. });
  86. piped.done(function( result ) {
  87. value3 = result;
  88. });
  89. defer.done(function( a, b ) {
  90. value1 = a;
  91. value2 = b;
  92. });
  93. defer.resolve( 2, 3 );
  94. strictEqual( value1, 2, "first resolve value ok" );
  95. strictEqual( value2, 3, "second resolve value ok" );
  96. strictEqual( value3, 6, "result of filter ok" );
  97. jQuery.Deferred().reject().then(function() {
  98. ok( false, "then should not be called on reject" );
  99. });
  100. jQuery.Deferred().resolve().then( jQuery.noop ).done(function( value ) {
  101. strictEqual( value, undefined, "then done callback can return undefined/null" );
  102. });
  103. });
  104. test( "jQuery.Deferred.then - filtering (fail)", function() {
  105. expect( 4 );
  106. var value1, value2, value3,
  107. defer = jQuery.Deferred(),
  108. piped = defer.then( null, function( a, b ) {
  109. return a * b;
  110. });
  111. piped.fail(function( result ) {
  112. value3 = result;
  113. });
  114. defer.fail(function( a, b ) {
  115. value1 = a;
  116. value2 = b;
  117. });
  118. defer.reject( 2, 3 );
  119. strictEqual( value1, 2, "first reject value ok" );
  120. strictEqual( value2, 3, "second reject value ok" );
  121. strictEqual( value3, 6, "result of filter ok" );
  122. jQuery.Deferred().resolve().then( null, function() {
  123. ok( false, "then should not be called on resolve" );
  124. });
  125. jQuery.Deferred().reject().then( null, jQuery.noop ).fail(function( value ) {
  126. strictEqual( value, undefined, "then fail callback can return undefined/null" );
  127. });
  128. });
  129. test( "jQuery.Deferred.then - filtering (progress)", function() {
  130. expect( 3 );
  131. var value1, value2, value3,
  132. defer = jQuery.Deferred(),
  133. piped = defer.then( null, null, function( a, b ) {
  134. return a * b;
  135. });
  136. piped.progress(function( result ) {
  137. value3 = result;
  138. });
  139. defer.progress(function( a, b ) {
  140. value1 = a;
  141. value2 = b;
  142. });
  143. defer.notify( 2, 3 );
  144. strictEqual( value1, 2, "first progress value ok" );
  145. strictEqual( value2, 3, "second progress value ok" );
  146. strictEqual( value3, 6, "result of filter ok" );
  147. });
  148. test( "jQuery.Deferred.then - deferred (done)", function() {
  149. expect( 3 );
  150. var value1, value2, value3,
  151. defer = jQuery.Deferred(),
  152. piped = defer.then(function( a, b ) {
  153. return jQuery.Deferred(function( defer ) {
  154. defer.reject( a * b );
  155. });
  156. });
  157. piped.fail(function( result ) {
  158. value3 = result;
  159. });
  160. defer.done(function( a, b ) {
  161. value1 = a;
  162. value2 = b;
  163. });
  164. defer.resolve( 2, 3 );
  165. strictEqual( value1, 2, "first resolve value ok" );
  166. strictEqual( value2, 3, "second resolve value ok" );
  167. strictEqual( value3, 6, "result of filter ok" );
  168. });
  169. test( "jQuery.Deferred.then - deferred (fail)", function() {
  170. expect( 3 );
  171. var value1, value2, value3,
  172. defer = jQuery.Deferred(),
  173. piped = defer.then( null, function( a, b ) {
  174. return jQuery.Deferred(function( defer ) {
  175. defer.resolve( a * b );
  176. });
  177. });
  178. piped.done(function( result ) {
  179. value3 = result;
  180. });
  181. defer.fail(function( a, b ) {
  182. value1 = a;
  183. value2 = b;
  184. });
  185. defer.reject( 2, 3 );
  186. strictEqual( value1, 2, "first reject value ok" );
  187. strictEqual( value2, 3, "second reject value ok" );
  188. strictEqual( value3, 6, "result of filter ok" );
  189. });
  190. test( "jQuery.Deferred.then - deferred (progress)", function() {
  191. expect( 3 );
  192. var value1, value2, value3,
  193. defer = jQuery.Deferred(),
  194. piped = defer.then( null, null, function( a, b ) {
  195. return jQuery.Deferred(function( defer ) {
  196. defer.resolve( a * b );
  197. });
  198. });
  199. piped.done(function( result ) {
  200. value3 = result;
  201. });
  202. defer.progress(function( a, b ) {
  203. value1 = a;
  204. value2 = b;
  205. });
  206. defer.notify( 2, 3 );
  207. strictEqual( value1, 2, "first progress value ok" );
  208. strictEqual( value2, 3, "second progress value ok" );
  209. strictEqual( value3, 6, "result of filter ok" );
  210. });
  211. test( "jQuery.Deferred.then - context", function() {
  212. expect( 7 );
  213. var defer, piped, defer2, piped2,
  214. context = {};
  215. jQuery.Deferred().resolveWith( context, [ 2 ] ).then(function( value ) {
  216. return value * 3;
  217. }).done(function( value ) {
  218. strictEqual( this, context, "custom context correctly propagated" );
  219. strictEqual( value, 6, "proper value received" );
  220. });
  221. jQuery.Deferred().resolve().then(function() {
  222. return jQuery.Deferred().resolveWith(context);
  223. }).done(function() {
  224. strictEqual( this, context, "custom context of returned deferred correctly propagated" );
  225. });
  226. defer = jQuery.Deferred();
  227. piped = defer.then(function( value ) {
  228. return value * 3;
  229. });
  230. defer.resolve( 2 );
  231. piped.done(function( value ) {
  232. strictEqual( this, piped, "default context gets updated to latest promise in the chain" );
  233. strictEqual( value, 6, "proper value received" );
  234. });
  235. defer2 = jQuery.Deferred();
  236. piped2 = defer2.then();
  237. defer2.resolve( 2 );
  238. piped2.done(function( value ) {
  239. strictEqual( this, piped2, "default context gets updated to latest promise in the chain (without passing function)" );
  240. strictEqual( value, 2, "proper value received (without passing function)" );
  241. });
  242. });
  243. test( "jQuery.when", function() {
  244. expect( 34 );
  245. // Some other objects
  246. jQuery.each({
  247. "an empty string": "",
  248. "a non-empty string": "some string",
  249. "zero": 0,
  250. "a number other than zero": 1,
  251. "true": true,
  252. "false": false,
  253. "null": null,
  254. "undefined": undefined,
  255. "a plain object": {}
  256. }, function( message, value ) {
  257. ok(
  258. jQuery.isFunction(
  259. jQuery.when( value ).done(function( resolveValue ) {
  260. strictEqual( this, window, "Context is the global object with " + message );
  261. strictEqual( resolveValue, value, "Test the promise was resolved with " + message );
  262. }).promise
  263. ),
  264. "Test " + message + " triggers the creation of a new Promise"
  265. );
  266. });
  267. ok(
  268. jQuery.isFunction(
  269. jQuery.when().done(function( resolveValue ) {
  270. strictEqual( this, window, "Test the promise was resolved with window as its context" );
  271. strictEqual( resolveValue, undefined, "Test the promise was resolved with no parameter" );
  272. }).promise
  273. ),
  274. "Test calling when with no parameter triggers the creation of a new Promise"
  275. );
  276. var cache,
  277. context = {};
  278. jQuery.when( jQuery.Deferred().resolveWith( context ) ).done(function() {
  279. strictEqual( this, context, "when( promise ) propagates context" );
  280. });
  281. jQuery.each([ 1, 2, 3 ], function( k, i ) {
  282. jQuery.when( cache || jQuery.Deferred(function() {
  283. this.resolve( i );
  284. })
  285. ).done(function( value ) {
  286. strictEqual( value, 1, "Function executed" + ( i > 1 ? " only once" : "" ) );
  287. cache = value;
  288. });
  289. });
  290. });
  291. test( "jQuery.when - joined", function() {
  292. expect( 119 );
  293. var deferreds = {
  294. value: 1,
  295. success: jQuery.Deferred().resolve( 1 ),
  296. error: jQuery.Deferred().reject( 0 ),
  297. futureSuccess: jQuery.Deferred().notify( true ),
  298. futureError: jQuery.Deferred().notify( true ),
  299. notify: jQuery.Deferred().notify( true )
  300. },
  301. willSucceed = {
  302. value: true,
  303. success: true,
  304. futureSuccess: true
  305. },
  306. willError = {
  307. error: true,
  308. futureError: true
  309. },
  310. willNotify = {
  311. futureSuccess: true,
  312. futureError: true,
  313. notify: true
  314. };
  315. jQuery.each( deferreds, function( id1, defer1 ) {
  316. jQuery.each( deferreds, function( id2, defer2 ) {
  317. var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
  318. shouldError = willError[ id1 ] || willError[ id2 ],
  319. shouldNotify = willNotify[ id1 ] || willNotify[ id2 ],
  320. expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
  321. expectedNotify = shouldNotify && [ willNotify[ id1 ], willNotify[ id2 ] ],
  322. code = id1 + "/" + id2,
  323. context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() : undefined,
  324. context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() : undefined;
  325. jQuery.when( defer1, defer2 ).done(function( a, b ) {
  326. if ( shouldResolve ) {
  327. deepEqual( [ a, b ], expected, code + " => resolve" );
  328. strictEqual( this[ 0 ], context1, code + " => first context OK" );
  329. strictEqual( this[ 1 ], context2, code + " => second context OK" );
  330. } else {
  331. ok( false, code + " => resolve" );
  332. }
  333. }).fail(function( a, b ) {
  334. if ( shouldError ) {
  335. deepEqual( [ a, b ], expected, code + " => reject" );
  336. } else {
  337. ok( false, code + " => reject" );
  338. }
  339. }).progress(function( a, b ) {
  340. deepEqual( [ a, b ], expectedNotify, code + " => progress" );
  341. strictEqual( this[ 0 ], expectedNotify[ 0 ] ? context1 : undefined, code + " => first context OK" );
  342. strictEqual( this[ 1 ], expectedNotify[ 1 ] ? context2 : undefined, code + " => second context OK" );
  343. });
  344. });
  345. });
  346. deferreds.futureSuccess.resolve( 1 );
  347. deferreds.futureError.reject( 0 );
  348. });