queue.js 7.2 KB

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