queue.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. module( "queue", { teardown: moduleTeardown });
  2. test( "queue() with other types", 14, function() {
  3. stop();
  4. var $div = jQuery({}),
  5. counter = 0;
  6. $div.promise( "foo" ).done(function() {
  7. equal( counter, 0, "Deferred for collection with no queue is automatically resolved" );
  8. });
  9. $div
  10. .queue("foo",function(){
  11. equal( ++counter, 1, "Dequeuing" );
  12. jQuery.dequeue(this,"foo");
  13. })
  14. .queue("foo",function(){
  15. equal( ++counter, 2, "Dequeuing" );
  16. jQuery(this).dequeue("foo");
  17. })
  18. .queue("foo",function(){
  19. equal( ++counter, 3, "Dequeuing" );
  20. })
  21. .queue("foo",function(){
  22. equal( ++counter, 4, "Dequeuing" );
  23. });
  24. $div.promise("foo").done(function() {
  25. equal( counter, 4, "Testing previous call to dequeue in deferred" );
  26. start();
  27. });
  28. equal( $div.queue("foo").length, 4, "Testing queue length" );
  29. equal( $div.queue("foo", undefined).queue("foo").length, 4, ".queue('name',undefined) does nothing but is chainable (#5571)");
  30. $div.dequeue("foo");
  31. equal( counter, 3, "Testing previous call to dequeue" );
  32. equal( $div.queue("foo").length, 1, "Testing queue length" );
  33. $div.dequeue("foo");
  34. equal( counter, 4, "Testing previous call to dequeue" );
  35. equal( $div.queue("foo").length, 0, "Testing queue length" );
  36. $div.dequeue("foo");
  37. equal( counter, 4, "Testing previous call to dequeue" );
  38. equal( $div.queue("foo").length, 0, "Testing queue length" );
  39. });
  40. test("queue(name) passes in the next item in the queue as a parameter", function() {
  41. expect(2);
  42. var div = jQuery({}),
  43. counter = 0;
  44. div.queue("foo", function(next) {
  45. equal(++counter, 1, "Dequeueing");
  46. next();
  47. }).queue("foo", function(next) {
  48. equal(++counter, 2, "Next was called");
  49. next();
  50. }).queue("bar", function() {
  51. equal(++counter, 3, "Other queues are not triggered by next()");
  52. });
  53. div.dequeue("foo");
  54. });
  55. test("queue() passes in the next item in the queue as a parameter to fx queues", function() {
  56. expect(3);
  57. stop();
  58. var div = jQuery({}),
  59. counter = 0;
  60. div.queue(function(next) {
  61. equal(++counter, 1, "Dequeueing");
  62. setTimeout(function() { next(); }, 500);
  63. }).queue(function(next) {
  64. equal(++counter, 2, "Next was called");
  65. next();
  66. }).queue("bar", function() {
  67. equal(++counter, 3, "Other queues are not triggered by next()");
  68. });
  69. jQuery.when( div.promise("fx"), div ).done(function() {
  70. equal(counter, 2, "Deferreds resolved");
  71. start();
  72. });
  73. });
  74. test("callbacks keep their place in the queue", function() {
  75. expect(5);
  76. stop();
  77. var div = jQuery("<div>"),
  78. counter = 0;
  79. div.queue(function( next ) {
  80. equal( ++counter, 1, "Queue/callback order: first called" );
  81. setTimeout( next, 200 );
  82. }).delay( 100 ).queue(function( next ) {
  83. equal( ++counter, 2, "Queue/callback order: second called" );
  84. jQuery( this ).delay( 100 ).queue(function( next ) {
  85. equal( ++counter, 4, "Queue/callback order: fourth called" );
  86. next();
  87. });
  88. next();
  89. }).queue(function( next ) {
  90. equal( ++counter, 3, "Queue/callback order: third called" );
  91. next();
  92. });
  93. div.promise("fx").done(function() {
  94. equal(counter, 4, "Deferreds resolved");
  95. start();
  96. });
  97. });
  98. test( "jQuery.queue should return array while manipulating the queue", 1, function() {
  99. var div = document.createElement("div");
  100. ok( jQuery.isArray( jQuery.queue( div, "fx", jQuery.noop ) ), "jQuery.queue should return an array while manipulating the queue" );
  101. });
  102. test("delay()", function() {
  103. expect(2);
  104. stop();
  105. var foo = jQuery({}), run = 0;
  106. foo.delay(100).queue(function(){
  107. run = 1;
  108. ok( true, "The function was dequeued." );
  109. start();
  110. });
  111. equal( run, 0, "The delay delayed the next function from running." );
  112. });
  113. test("clearQueue(name) clears the queue", function() {
  114. expect(2);
  115. stop();
  116. var div = jQuery({}),
  117. counter = 0;
  118. div.queue("foo", function( next ) {
  119. counter++;
  120. jQuery(this).clearQueue("foo");
  121. next();
  122. }).queue("foo", function() {
  123. counter++;
  124. });
  125. div.promise("foo").done(function() {
  126. ok( true, "dequeue resolves the deferred" );
  127. start();
  128. });
  129. div.dequeue("foo");
  130. equal(counter, 1, "the queue was cleared");
  131. });
  132. test("clearQueue() clears the fx queue", function() {
  133. expect(1);
  134. var div = jQuery({}),
  135. counter = 0;
  136. div.queue(function( next ) {
  137. counter++;
  138. var self = this;
  139. setTimeout(function() { jQuery(self).clearQueue(); next(); }, 50);
  140. }).queue(function() {
  141. counter++;
  142. });
  143. equal(counter, 1, "the queue was cleared");
  144. div.removeData();
  145. });
  146. asyncTest( "fn.promise() - called when fx queue is empty", 3, function() {
  147. var foo = jQuery( "#foo" ).clone().addBack(),
  148. promised = false;
  149. foo.queue( function( next ) {
  150. // called twice!
  151. ok( !promised, "Promised hasn't been called" );
  152. setTimeout( next, 10 );
  153. });
  154. foo.promise().done( function() {
  155. ok( promised = true, "Promised" );
  156. start();
  157. });
  158. });
  159. asyncTest( "fn.promise( \"queue\" ) - called whenever last queue function is dequeued", 5, function() {
  160. var foo = jQuery( "#foo" ),
  161. test;
  162. foo.promise( "queue" ).done( function() {
  163. strictEqual( test, undefined, "called immediately when queue was already empty" );
  164. });
  165. test = 1;
  166. foo.queue( "queue", function( next ) {
  167. strictEqual( test++, 1, "step one" );
  168. setTimeout( next, 0 );
  169. }).queue( "queue", function( next ) {
  170. strictEqual( test++, 2, "step two" );
  171. setTimeout( function() {
  172. next();
  173. strictEqual( test++, 4, "step four" );
  174. start();
  175. }, 10 );
  176. }).promise( "queue" ).done( function() {
  177. strictEqual( test++, 3, "step three" );
  178. });
  179. foo.dequeue( "queue" );
  180. });
  181. asyncTest( "fn.promise( \"queue\" ) - waits for animation to complete before resolving", 2, function() {
  182. var foo = jQuery( "#foo" ),
  183. test = 1;
  184. foo.animate({
  185. top: 100
  186. }, {
  187. duration: 1,
  188. queue: "queue",
  189. complete: function() {
  190. strictEqual( test++, 1, "step one" );
  191. }
  192. }).dequeue( "queue" );
  193. foo.promise( "queue" ).done( function() {
  194. strictEqual( test++, 2, "step two" );
  195. start();
  196. });
  197. });
  198. test( ".promise(obj)", function() {
  199. expect(2);
  200. var obj = {},
  201. promise = jQuery( "#foo" ).promise( "promise", obj );
  202. ok( jQuery.isFunction( promise.promise ), ".promise(type, obj) returns a promise" );
  203. strictEqual( promise, obj, ".promise(type, obj) returns obj" );
  204. });
  205. if ( jQuery.fn.stop ) {
  206. test("delay() can be stopped", function() {
  207. expect( 3 );
  208. stop();
  209. var done = {};
  210. jQuery({})
  211. .queue( "alternate", function( next ) {
  212. done.alt1 = true;
  213. ok( true, "This first function was dequeued" );
  214. next();
  215. })
  216. .delay( 1000, "alternate" )
  217. .queue( "alternate", function() {
  218. done.alt2 = true;
  219. ok( true, "The function was dequeued immediately, the delay was stopped" );
  220. })
  221. .dequeue( "alternate" )
  222. // stop( "alternate", false ) will NOT clear the queue, so it should automatically dequeue the next
  223. .stop( "alternate", false, false )
  224. // this test
  225. .delay( 1 )
  226. .queue(function() {
  227. done.default1 = true;
  228. ok( false, "This queue should never run" );
  229. })
  230. // stop( clearQueue ) should clear the queue
  231. .stop( true, false );
  232. deepEqual( done, { alt1: true, alt2: true }, "Queue ran the proper functions" );
  233. setTimeout(function() {
  234. start();
  235. }, 1500 );
  236. });
  237. asyncTest( "queue stop hooks", 2, function() {
  238. var foo = jQuery( "#foo" );
  239. foo.queue( function( next, hooks ) {
  240. hooks.stop = function( gotoEnd ) {
  241. equal( !!gotoEnd, false, "Stopped without gotoEnd" );
  242. };
  243. });
  244. foo.stop();
  245. foo.queue( function( next, hooks ) {
  246. hooks.stop = function( gotoEnd ) {
  247. equal( gotoEnd, true, "Stopped with gotoEnd" );
  248. start();
  249. };
  250. });
  251. foo.stop( false, true );
  252. });
  253. } // if ( jQuery.fn.stop )