Build: Bump the github-actions group across 1 directory with 2 updates
[jquery.git] / test / unit / queue.js
blobed9f261f83e53a45fd8a9f251cd2d75573510b02
1 QUnit.module( "queue", { afterEach: moduleTeardown } );
3 ( function() {
5 if ( !includesModule( "queue" ) ) {
6 return;
9 QUnit.test( "queue() with other types", function( assert ) {
10 var done = assert.async( 2 );
11 assert.expect( 14 );
14 var $div = jQuery( {} ),
15 counter = 0;
17 $div.promise( "foo" ).done( function() {
18 assert.equal( counter, 0, "Deferred for collection with no queue is automatically resolved" );
19 } );
21 $div
22 .queue( "foo", function() {
23 assert.equal( ++counter, 1, "Dequeuing" );
24 jQuery.dequeue( this, "foo" );
25 } )
26 .queue( "foo", function() {
27 assert.equal( ++counter, 2, "Dequeuing" );
28 jQuery( this ).dequeue( "foo" );
29 } )
30 .queue( "foo", function() {
31 assert.equal( ++counter, 3, "Dequeuing" );
32 } )
33 .queue( "foo", function() {
34 assert.equal( ++counter, 4, "Dequeuing" );
35 } );
37 $div.promise( "foo" ).done( function() {
38 assert.equal( counter, 4, "Testing previous call to dequeue in deferred" );
39 done();
40 } );
42 assert.equal( $div.queue( "foo" ).length, 4, "Testing queue length" );
44 assert.equal( $div.queue( "foo", undefined ).queue( "foo" ).length, 4, ".queue('name',undefined) does nothing but is chainable (trac-5571)" );
46 $div.dequeue( "foo" );
48 assert.equal( counter, 3, "Testing previous call to dequeue" );
49 assert.equal( $div.queue( "foo" ).length, 1, "Testing queue length" );
51 $div.dequeue( "foo" );
53 assert.equal( counter, 4, "Testing previous call to dequeue" );
54 assert.equal( $div.queue( "foo" ).length, 0, "Testing queue length" );
56 $div.dequeue( "foo" );
58 assert.equal( counter, 4, "Testing previous call to dequeue" );
59 assert.equal( $div.queue( "foo" ).length, 0, "Testing queue length" );
60 done();
61 } );
63 QUnit.test( "queue(name) passes in the next item in the queue as a parameter", function( assert ) {
64 assert.expect( 2 );
66 var div = jQuery( {} ),
67 counter = 0;
69 div.queue( "foo", function( next ) {
70 assert.equal( ++counter, 1, "Dequeueing" );
71 next();
72 } ).queue( "foo", function( next ) {
73 assert.equal( ++counter, 2, "Next was called" );
74 next();
75 } ).queue( "bar", function() {
76 assert.equal( ++counter, 3, "Other queues are not triggered by next()" );
77 } );
79 div.dequeue( "foo" );
80 } );
82 QUnit.test( "queue() passes in the next item in the queue as a parameter to fx queues", function( assert ) {
83 var done = assert.async();
84 assert.expect( 3 );
86 var div = jQuery( {} ),
87 counter = 0;
89 div.queue( function( next ) {
90 assert.equal( ++counter, 1, "Dequeueing" );
91 setTimeout( function() {
92 next();
93 }, 500 );
94 } ).queue( function( next ) {
95 assert.equal( ++counter, 2, "Next was called" );
96 next();
97 } ).queue( "bar", function() {
98 assert.equal( ++counter, 3, "Other queues are not triggered by next()" );
99 } );
101 jQuery.when( div.promise( "fx" ), div ).done( function() {
102 assert.equal( counter, 2, "Deferreds resolved" );
103 done();
104 } );
105 } );
107 QUnit.test( "callbacks keep their place in the queue", function( assert ) {
108 var done = assert.async();
109 assert.expect( 5 );
110 var div = jQuery( "<div>" ),
111 counter = 0;
113 div.queue( function( next ) {
114 assert.equal( ++counter, 1, "Queue/callback order: first called" );
115 setTimeout( next, 200 );
116 } ).delay( 100 ).queue( function( next ) {
117 assert.equal( ++counter, 2, "Queue/callback order: second called" );
118 jQuery( this ).delay( 100 ).queue( function( next ) {
119 assert.equal( ++counter, 4, "Queue/callback order: fourth called" );
120 next();
121 } );
122 next();
123 } ).queue( function( next ) {
124 assert.equal( ++counter, 3, "Queue/callback order: third called" );
125 next();
126 } );
128 div.promise( "fx" ).done( function() {
129 assert.equal( counter, 4, "Deferreds resolved" );
130 done();
131 } );
132 } );
134 QUnit.test( "jQuery.queue should return array while manipulating the queue", function( assert ) {
135 assert.expect( 1 );
137 var div = document.createElement( "div" );
139 assert.ok( Array.isArray( jQuery.queue( div, "fx", jQuery.noop ) ), "jQuery.queue should return an array while manipulating the queue" );
140 } );
142 QUnit.test( "delay()", function( assert ) {
143 var done = assert.async();
144 assert.expect( 2 );
146 var foo = jQuery( {} ), run = 0;
148 foo.delay( 100 ).queue( function() {
149 run = 1;
150 assert.ok( true, "The function was dequeued." );
151 done();
152 } );
154 assert.equal( run, 0, "The delay delayed the next function from running." );
155 } );
157 QUnit.test( "clearQueue(name) clears the queue", function( assert ) {
158 var done = assert.async( 2 );
159 assert.expect( 2 );
161 var div = jQuery( {} ),
162 counter = 0;
164 div.queue( "foo", function( next ) {
165 counter++;
166 jQuery( this ).clearQueue( "foo" );
167 next();
168 } ).queue( "foo", function() {
169 counter++;
170 } );
172 div.promise( "foo" ).done( function() {
173 assert.ok( true, "dequeue resolves the deferred" );
174 done();
175 } );
177 div.dequeue( "foo" );
179 assert.equal( counter, 1, "the queue was cleared" );
180 done();
181 } );
183 QUnit.test( "clearQueue() clears the fx queue", function( assert ) {
184 assert.expect( 1 );
186 var div = jQuery( {} ),
187 counter = 0;
189 div.queue( function( next ) {
190 counter++;
191 var self = this;
192 setTimeout( function() {
193 jQuery( self ).clearQueue(); next();
194 }, 50 );
195 } ).queue( function() {
196 counter++;
197 } );
199 assert.equal( counter, 1, "the queue was cleared" );
201 div.removeData();
202 } );
204 QUnit.test( "fn.promise() - called when fx queue is empty", function( assert ) {
205 assert.expect( 3 );
206 var foo = jQuery( "#foo" ).clone().addBack(),
207 promised = false,
208 done = assert.async();
210 foo.queue( function( next ) {
212 // called twice!
213 assert.ok( !promised, "Promised hasn't been called" );
214 setTimeout( next, 10 );
215 } );
216 foo.promise().done( function() {
217 assert.ok( promised = true, "Promised" );
218 done();
219 } );
220 } );
222 QUnit.test( "fn.promise( \"queue\" ) - called whenever last queue function is dequeued", function( assert ) {
223 assert.expect( 5 );
224 var done = assert.async();
225 var foo = jQuery( "#foo" ),
226 test;
227 foo.promise( "queue" ).done( function() {
228 assert.strictEqual( test, undefined, "called immediately when queue was already empty" );
229 } );
230 test = 1;
231 foo.queue( "queue", function( next ) {
232 assert.strictEqual( test++, 1, "step one" );
233 setTimeout( next, 0 );
234 } ).queue( "queue", function( next ) {
235 assert.strictEqual( test++, 2, "step two" );
236 setTimeout( function() {
237 next();
238 assert.strictEqual( test++, 4, "step four" );
239 done();
240 }, 10 );
241 } ).promise( "queue" ).done( function() {
242 assert.strictEqual( test++, 3, "step three" );
243 } );
245 foo.dequeue( "queue" );
246 } );
248 if ( includesModule( "effects" ) ) {
250 QUnit.test( "fn.promise( \"queue\" ) - waits for animation to complete before resolving", function( assert ) {
251 assert.expect( 2 );
252 var done = assert.async();
253 var foo = jQuery( "#foo" ),
254 test = 1;
256 foo.animate( {
257 top: 100
258 }, {
259 duration: 1,
260 queue: "queue",
261 complete: function() {
262 assert.strictEqual( test++, 1, "step one" );
264 } ).dequeue( "queue" );
266 foo.promise( "queue" ).done( function() {
267 assert.strictEqual( test++, 2, "step two" );
268 done();
269 } );
271 } );
274 QUnit.test( ".promise(obj)", function( assert ) {
275 assert.expect( 2 );
277 var obj = {},
278 promise = jQuery( "#foo" ).promise( "promise", obj );
280 assert.ok( typeof promise.promise === "function", ".promise(type, obj) returns a promise" );
281 assert.strictEqual( promise, obj, ".promise(type, obj) returns obj" );
282 } );
284 QUnit[ includesModule( "effects" ) ? "test" : "skip" ]( "delay() can be stopped", function( assert ) {
285 var done = assert.async();
286 assert.expect( 3 );
287 var storage = {};
288 jQuery( {} )
289 .queue( "alternate", function( next ) {
290 storage.alt1 = true;
291 assert.ok( true, "This first function was dequeued" );
292 next();
294 .delay( 1000, "alternate" )
295 .queue( "alternate", function() {
296 storage.alt2 = true;
297 assert.ok( true, "The function was dequeued immediately, the delay was stopped" );
299 .dequeue( "alternate" )
301 // stop( "alternate", false ) will NOT clear the queue, so it should automatically dequeue the next
302 .stop( "alternate", false, false )
304 // this test
305 .delay( 1 )
306 .queue( function() {
307 storage.default1 = true;
308 assert.ok( false, "This queue should never run" );
311 // stop( clearQueue ) should clear the queue
312 .stop( true, false );
314 assert.deepEqual( storage, { alt1: true, alt2: true }, "Queue ran the proper functions" );
316 setTimeout( function() {
317 done();
318 }, 1500 );
319 } );
321 QUnit[ includesModule( "effects" ) ? "test" : "skip" ]( "queue stop hooks", function( assert ) {
322 assert.expect( 2 );
323 var done = assert.async();
324 var foo = jQuery( "#foo" );
326 foo.queue( function( next, hooks ) {
327 hooks.stop = function( gotoEnd ) {
328 assert.equal( !!gotoEnd, false, "Stopped without gotoEnd" );
330 } );
331 foo.stop();
333 foo.queue( function( next, hooks ) {
334 hooks.stop = function( gotoEnd ) {
335 assert.equal( gotoEnd, true, "Stopped with gotoEnd" );
336 done();
338 } );
340 foo.stop( false, true );
341 } );
343 } )();