1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
|
QUnit.module( "deferred", {
teardown: moduleTeardown
} );
jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
function createDeferred( fn ) {
return withNew ? new jQuery.Deferred( fn ) : jQuery.Deferred( fn );
}
QUnit.test( "jQuery.Deferred" + withNew, function( assert ) {
assert.expect( 23 );
var defer = createDeferred();
assert.ok( jQuery.isFunction( defer.pipe ), "defer.pipe is a function" );
createDeferred().resolve().done( function() {
assert.ok( true, "Success on resolve" );
assert.strictEqual( this.state(), "resolved", "Deferred is resolved (state)" );
} ).fail( function() {
assert.ok( false, "Error on resolve" );
} ).always( function() {
assert.ok( true, "Always callback on resolve" );
} );
createDeferred().reject().done( function() {
assert.ok( false, "Success on reject" );
} ).fail( function() {
assert.ok( true, "Error on reject" );
assert.strictEqual( this.state(), "rejected", "Deferred is rejected (state)" );
} ).always( function() {
assert.ok( true, "Always callback on reject" );
} );
createDeferred( function( defer ) {
assert.ok( this === defer, "Defer passed as this & first argument" );
this.resolve( "done" );
} ).done( function( value ) {
assert.strictEqual( value, "done", "Passed function executed" );
} );
createDeferred( function( defer ) {
var promise = defer.promise(),
func = function() {},
funcPromise = defer.promise( func );
assert.strictEqual( defer.promise(), promise, "promise is always the same" );
assert.strictEqual( funcPromise, func, "non objects get extended" );
jQuery.each( promise, function( key ) {
if ( !jQuery.isFunction( promise[ key ] ) ) {
assert.ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" );
}
if ( promise[ key ] !== func[ key ] ) {
assert.strictEqual( func[ key ], promise[ key ], key + " is the same" );
}
} );
} );
jQuery.expandedEach = jQuery.each;
jQuery.expandedEach( "resolve reject".split( " " ), function( _, change ) {
createDeferred( function( defer ) {
assert.strictEqual( defer.state(), "pending", "pending after creation" );
var checked = 0;
defer.progress( function( value ) {
assert.strictEqual( value, checked, "Progress: right value (" + value + ") received" );
} );
for ( checked = 0; checked < 3; checked++ ) {
defer.notify( checked );
}
assert.strictEqual( defer.state(), "pending", "pending after notification" );
defer[ change ]();
assert.notStrictEqual( defer.state(), "pending", "not pending after " + change );
defer.notify();
} );
} );
} );
} );
QUnit.test( "jQuery.Deferred - chainability", function( assert ) {
var defer = jQuery.Deferred();
assert.expect( 10 );
jQuery.expandedEach = jQuery.each;
jQuery.expandedEach( "resolve reject notify resolveWith rejectWith notifyWith done fail progress always".split( " " ), function( _, method ) {
var object = {
m: defer[ method ]
};
assert.strictEqual( object.m(), object, method + " is chainable" );
} );
} );
QUnit.test( "jQuery.Deferred.then - filtering (done)", function( assert ) {
assert.expect( 4 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.then( function( a, b ) {
return a * b;
} ),
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
piped.done( function( result ) {
value3 = result;
} );
defer.done( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.resolve( 2, 3 ).then( function() {
assert.strictEqual( value1, 2, "first resolve value ok" );
assert.strictEqual( value2, 3, "second resolve value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done.pop().call();
} );
jQuery.Deferred().reject().then( function() {
assert.ok( false, "then should not be called on reject" );
} ).then( null, done.pop() );
jQuery.Deferred().resolve().then( jQuery.noop ).done( function( value ) {
assert.strictEqual( value, undefined, "then done callback can return undefined/null" );
done.pop().call();
} );
} );
QUnit.test( "jQuery.Deferred.then - filtering (fail)", function( assert ) {
assert.expect( 4 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.then( null, function( a, b ) {
return a * b;
} ),
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
piped.done( function( result ) {
value3 = result;
} );
defer.fail( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.reject( 2, 3 ).then( null, function() {
assert.strictEqual( value1, 2, "first reject value ok" );
assert.strictEqual( value2, 3, "second reject value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done.pop().call();
} );
jQuery.Deferred().resolve().then( null, function() {
assert.ok( false, "then should not be called on resolve" );
} ).then( done.pop() );
jQuery.Deferred().reject().then( null, jQuery.noop ).done( function( value ) {
assert.strictEqual( value, undefined, "then fail callback can return undefined/null" );
done.pop().call();
} );
} );
QUnit.test( "jQuery.Deferred.catch", function( assert ) {
assert.expect( 4 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer[ "catch" ]( function( a, b ) {
return a * b;
} ),
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
piped.done( function( result ) {
value3 = result;
} );
defer.fail( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.reject( 2, 3 )[ "catch" ]( function() {
assert.strictEqual( value1, 2, "first reject value ok" );
assert.strictEqual( value2, 3, "second reject value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done.pop().call();
} );
jQuery.Deferred().resolve()[ "catch" ]( function() {
assert.ok( false, "then should not be called on resolve" );
} ).then( done.pop() );
jQuery.Deferred().reject()[ "catch" ]( jQuery.noop ).done( function( value ) {
assert.strictEqual( value, undefined, "then fail callback can return undefined/null" );
done.pop().call();
} );
} );
QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - filtering (fail)", function( assert ) {
assert.expect( 4 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.pipe( null, function( a, b ) {
return a * b;
} ),
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
piped.fail( function( result ) {
value3 = result;
} );
defer.fail( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.reject( 2, 3 ).pipe( null, function() {
assert.strictEqual( value1, 2, "first reject value ok" );
assert.strictEqual( value2, 3, "second reject value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done.pop().call();
} );
jQuery.Deferred().resolve().pipe( null, function() {
assert.ok( false, "then should not be called on resolve" );
} ).then( done.pop() );
jQuery.Deferred().reject().pipe( null, jQuery.noop ).fail( function( value ) {
assert.strictEqual( value, undefined, "then fail callback can return undefined/null" );
done.pop().call();
} );
} );
QUnit.test( "jQuery.Deferred.then - filtering (progress)", function( assert ) {
assert.expect( 3 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.then( null, null, function( a, b ) {
return a * b;
} ),
done = assert.async();
piped.progress( function( result ) {
value3 = result;
} );
defer.progress( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.notify( 2, 3 ).then( null, null, function() {
assert.strictEqual( value1, 2, "first progress value ok" );
assert.strictEqual( value2, 3, "second progress value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done();
} );
} );
QUnit.test( "jQuery.Deferred.then - deferred (done)", function( assert ) {
assert.expect( 3 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.then( function( a, b ) {
return jQuery.Deferred( function( defer ) {
defer.reject( a * b );
} );
} ),
done = assert.async();
piped.fail( function( result ) {
value3 = result;
} );
defer.done( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.resolve( 2, 3 );
piped.fail( function() {
assert.strictEqual( value1, 2, "first resolve value ok" );
assert.strictEqual( value2, 3, "second resolve value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done();
} );
} );
QUnit.test( "jQuery.Deferred.then - deferred (fail)", function( assert ) {
assert.expect( 3 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.then( null, function( a, b ) {
return jQuery.Deferred( function( defer ) {
defer.resolve( a * b );
} );
} ),
done = assert.async();
piped.done( function( result ) {
value3 = result;
} );
defer.fail( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.reject( 2, 3 );
piped.done( function() {
assert.strictEqual( value1, 2, "first reject value ok" );
assert.strictEqual( value2, 3, "second reject value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done();
} );
} );
QUnit.test( "jQuery.Deferred.then - deferred (progress)", function( assert ) {
assert.expect( 3 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.then( null, null, function( a, b ) {
return jQuery.Deferred( function( defer ) {
defer.resolve( a * b );
} );
} ),
done = assert.async();
piped.progress( function( result ) {
return jQuery.Deferred().resolve().then( function() {
return result;
} ).then( function( result ) {
value3 = result;
} );
} );
defer.progress( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.notify( 2, 3 );
piped.then( null, null, function( result ) {
return jQuery.Deferred().resolve().then( function() {
return result;
} ).then( function() {
assert.strictEqual( value1, 2, "first progress value ok" );
assert.strictEqual( value2, 3, "second progress value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done();
} );
} );
} );
QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - deferred (progress)", function( assert ) {
assert.expect( 3 );
var value1, value2, value3,
defer = jQuery.Deferred(),
piped = defer.pipe( null, null, function( a, b ) {
return jQuery.Deferred( function( defer ) {
defer.resolve( a * b );
} );
} ),
done = assert.async();
piped.done( function( result ) {
value3 = result;
} );
defer.progress( function( a, b ) {
value1 = a;
value2 = b;
} );
defer.notify( 2, 3 );
piped.done( function() {
assert.strictEqual( value1, 2, "first progress value ok" );
assert.strictEqual( value2, 3, "second progress value ok" );
assert.strictEqual( value3, 6, "result of filter ok" );
done();
} );
} );
QUnit.test( "jQuery.Deferred.then - context", function( assert ) {
assert.expect( 7 );
var defer, piped, defer2, piped2,
context = {},
done = jQuery.map( new Array( 4 ), function() { return assert.async(); } );
jQuery.Deferred().resolveWith( context, [ 2 ] ).then( function( value ) {
return value * 3;
} ).done( function( value ) {
assert.notStrictEqual( this, context, "custom context not propagated through .then" );
assert.strictEqual( value, 6, "proper value received" );
done.pop().call();
} );
jQuery.Deferred().resolve().then( function() {
return jQuery.Deferred().resolveWith( context );
} ).done( function() {
assert.strictEqual( this, context,
"custom context of returned deferred correctly propagated" );
done.pop().call();
} );
defer = jQuery.Deferred();
piped = defer.then( function( value ) {
return value * 3;
} );
defer.resolve( 2 );
piped.done( function( value ) {
assert.strictEqual( this, piped,
"default context gets updated to latest promise in the chain" );
assert.strictEqual( value, 6, "proper value received" );
done.pop().call();
} );
defer2 = jQuery.Deferred();
piped2 = defer2.then();
defer2.resolve( 2 );
piped2.done( function( value ) {
assert.strictEqual( this, piped2,
"default context updated to latest promise in the chain (without passing function)" );
assert.strictEqual( value, 2, "proper value received (without passing function)" );
done.pop().call();
} );
} );
QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - context", function( assert ) {
assert.expect( 7 );
var defer, piped, defer2, piped2,
context = {},
done = jQuery.map( new Array( 4 ), function() { return assert.async(); } );
jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe( function( value ) {
return value * 3;
} ).done( function( value ) {
assert.strictEqual( this, context, "[PIPE ONLY] custom context correctly propagated" );
assert.strictEqual( value, 6, "proper value received" );
done.pop().call();
} );
jQuery.Deferred().resolve().pipe( function() {
return jQuery.Deferred().resolveWith( context );
} ).done( function() {
assert.strictEqual( this, context,
"custom context of returned deferred correctly propagated" );
done.pop().call();
} );
defer = jQuery.Deferred();
piped = defer.pipe( function( value ) {
return value * 3;
} );
defer.resolve( 2 );
piped.done( function( value ) {
assert.strictEqual( this, piped,
"default context gets updated to latest promise in the chain" );
assert.strictEqual( value, 6, "proper value received" );
done.pop().call();
} );
defer2 = jQuery.Deferred();
piped2 = defer2.pipe();
defer2.resolve( 2 );
piped2.done( function( value ) {
assert.strictEqual( this, piped2,
"default context updated to latest promise in the chain (without passing function)" );
assert.strictEqual( value, 2, "proper value received (without passing function)" );
done.pop().call();
} );
} );
QUnit.test( "jQuery.Deferred.then - spec compatibility", function( assert ) {
assert.expect( 1 );
var done = assert.async();
var defer = jQuery.Deferred().done( function() {
setTimeout( done );
throw new Error();
} );
defer.then( function() {
assert.ok( true, "errors in .done callbacks don't stop .then handlers" );
} );
try {
defer.resolve();
} catch ( _ ) {}
} );
QUnit.test( "jQuery.Deferred - 1.x/2.x compatibility", function( assert ) {
assert.expect( 8 );
var context = { id: "callback context" },
thenable = jQuery.Deferred().resolve( "thenable fulfillment" ).promise(),
done = jQuery.map( new Array( 8 ), function() { return assert.async(); } );
thenable.unwrapped = false;
jQuery.Deferred().resolve( 1, 2 ).then( function() {
assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ],
".then fulfillment callbacks receive all resolution values" );
done.pop().call();
} );
jQuery.Deferred().reject( 1, 2 ).then( null, function() {
assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ],
".then rejection callbacks receive all rejection values" );
done.pop().call();
} );
jQuery.Deferred().notify( 1, 2 ).then( null, null, function() {
assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ],
".then progress callbacks receive all progress values" );
done.pop().call();
} );
jQuery.Deferred().resolveWith( context ).then( function() {
assert.deepEqual( this, context, ".then fulfillment callbacks receive context" );
done.pop().call();
} );
jQuery.Deferred().rejectWith( context ).then( null, function() {
assert.deepEqual( this, context, ".then rejection callbacks receive context" );
done.pop().call();
} );
jQuery.Deferred().notifyWith( context ).then( null, null, function() {
assert.deepEqual( this, context, ".then progress callbacks receive context" );
done.pop().call();
} );
jQuery.Deferred().resolve( thenable ).done( function( value ) {
assert.strictEqual( value, thenable, ".done doesn't unwrap thenables" );
done.pop().call();
} );
jQuery.Deferred().notify( thenable ).then().then( null, null, function( value ) {
assert.strictEqual( value, "thenable fulfillment",
".then implicit progress callbacks unwrap thenables" );
done.pop().call();
} );
} );
QUnit.test( "jQuery.Deferred.then - progress and thenables", function( assert ) {
assert.expect( 2 );
var trigger = jQuery.Deferred().notify(),
expectedProgress = [ "baz", "baz" ],
done = jQuery.map( new Array( 2 ), function() { return assert.async(); } ),
failer = function( evt ) {
return function() {
assert.ok( false, "no unexpected " + evt );
};
};
trigger.then( null, null, function() {
var notifier = jQuery.Deferred().notify( "foo" );
setTimeout( function() {
notifier.notify( "bar" ).resolve( "baz" );
} );
return notifier;
} ).then( failer( "fulfill" ), failer( "reject" ), function( v ) {
assert.strictEqual( v, expectedProgress.shift(), "expected progress value" );
done.pop().call();
} );
trigger.notify();
} );
QUnit.test( "jQuery.Deferred - notify and resolve", function( assert ) {
assert.expect( 7 );
var notifiedResolved = jQuery.Deferred().notify( "foo" )/*xxx .resolve( "bar" )*/,
done = jQuery.map( new Array( 3 ), function() { return assert.async(); } );
notifiedResolved.progress( function( v ) {
assert.strictEqual( v, "foo", "progress value" );
} );
notifiedResolved.pipe().progress( function( v ) {
assert.strictEqual( v, "foo", "piped progress value" );
} );
notifiedResolved.pipe( null, null, function() {
return "baz";
} ).progress( function( v ) {
assert.strictEqual( v, "baz", "replaced piped progress value" );
} );
notifiedResolved.pipe( null, null, function() {
return jQuery.Deferred().notify( "baz" ).resolve( "quux" );
} ).progress( function( v ) {
assert.strictEqual( v, "baz", "deferred replaced piped progress value" );
} );
notifiedResolved.then().progress( function( v ) {
assert.strictEqual( v, "foo", "then'd progress value" );
done.pop().call();
} );
notifiedResolved.then( null, null, function() {
return "baz";
} ).progress( function( v ) {
assert.strictEqual( v, "baz", "replaced then'd progress value" );
done.pop().call();
} );
notifiedResolved.then( null, null, function() {
return jQuery.Deferred().notify( "baz" ).resolve( "quux" );
} ).progress( function( v ) {
// Progress from the surrogate deferred is ignored
assert.strictEqual( v, "quux", "deferred replaced then'd progress value" );
done.pop().call();
} );
} );
QUnit.test( "jQuery.when", function( assert ) {
assert.expect( 37 );
// Some other objects
jQuery.each( {
"an empty string": "",
"a non-empty string": "some string",
"zero": 0,
"a number other than zero": 1,
"true": true,
"false": false,
"null": null,
"undefined": undefined,
"a plain object": {},
"an array": [ 1, 2, 3 ]
}, function( message, value ) {
assert.ok(
jQuery.isFunction(
jQuery.when( value ).done( function( resolveValue ) {
assert.strictEqual( this, window, "Context is the global object with " + message );
assert.strictEqual( resolveValue, value, "Test the promise was resolved with " + message );
} ).promise
),
"Test " + message + " triggers the creation of a new Promise"
);
} );
assert.ok(
jQuery.isFunction(
jQuery.when().done( function( resolveValue ) {
assert.strictEqual( this, window, "Test the promise was resolved with window as its context" );
assert.strictEqual( resolveValue, undefined, "Test the promise was resolved with no parameter" );
} ).promise
),
"Test calling when with no parameter triggers the creation of a new Promise"
);
var cache,
context = {};
jQuery.when( jQuery.Deferred().resolveWith( context ) ).done( function() {
assert.strictEqual( this, context, "when( promise ) propagates context" );
} );
jQuery.each( [ 1, 2, 3 ], function( k, i ) {
jQuery.when( cache || jQuery.Deferred( function() {
this.resolve( i );
} )
).done( function( value ) {
assert.strictEqual( value, 1, "Function executed" + ( i > 1 ? " only once" : "" ) );
cache = value;
} );
} );
} );
QUnit.test( "jQuery.when - joined", function( assert ) {
assert.expect( 195 );
var deferreds = {
rawValue: 1,
fulfilled: jQuery.Deferred().resolve( 1 ),
rejected: jQuery.Deferred().reject( 0 ),
notified: jQuery.Deferred().notify( true ),
eventuallyFulfilled: jQuery.Deferred().notify( true ),
eventuallyRejected: jQuery.Deferred().notify( true ),
fulfilledStandardPromise: Promise.resolve( 1 ),
rejectedStandardPromise: Promise.reject( 0 )
},
willSucceed = {
rawValue: true,
fulfilled: true,
eventuallyFulfilled: true,
fulfilledStandardPromise: true
},
willError = {
rejected: true,
eventuallyRejected: true,
rejectedStandardPromise: true
},
willNotify = {
notified: true,
eventuallyFulfilled: true,
eventuallyRejected: true
},
counter = 49;
QUnit.stop();
function restart() {
if ( !--counter ) {
QUnit.start();
}
}
jQuery.each( deferreds, function( id1, defer1 ) {
jQuery.each( deferreds, function( id2, defer2 ) {
var shouldResolve = willSucceed[ id1 ] && willSucceed[ id2 ],
shouldError = willError[ id1 ] || willError[ id2 ],
shouldNotify = willNotify[ id1 ] || willNotify[ id2 ],
expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ],
expectedNotify = shouldNotify && [ willNotify[ id1 ], willNotify[ id2 ] ],
code = "jQuery.when( " + id1 + ", " + id2 + " )",
context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() :
( defer1.then ? window : undefined ),
context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() :
( defer2.then ? window : undefined );
jQuery.when( defer1, defer2 ).done( function( a, b ) {
if ( shouldResolve ) {
assert.deepEqual( [ a, b ], expected, code + " => resolve" );
assert.strictEqual( this[ 0 ], context1, code + " => first context OK" );
assert.strictEqual( this[ 1 ], context2, code + " => second context OK" );
} else {
assert.ok( false, code + " => resolve" );
}
} ).fail( function( a, b ) {
if ( shouldError ) {
assert.deepEqual( [ a, b ], expected, code + " => reject" );
} else {
assert.ok( false, code + " => reject" );
}
} ).progress( function( a, b ) {
assert.deepEqual( [ a, b ], expectedNotify, code + " => progress" );
assert.strictEqual( this[ 0 ], expectedNotify[ 0 ] ? context1 : undefined, code + " => first context OK" );
assert.strictEqual( this[ 1 ], expectedNotify[ 1 ] ? context2 : undefined, code + " => second context OK" );
} ).always( restart );
} );
} );
deferreds.eventuallyFulfilled.resolve( 1 );
deferreds.eventuallyRejected.reject( 0 );
} );
QUnit.test( "jQuery.when - resolved", function( assert ) {
assert.expect( 6 );
var a = jQuery.Deferred().notify( 1 ).resolve( 4 ),
b = jQuery.Deferred().notify( 2 ).resolve( 5 ),
c = jQuery.Deferred().notify( 3 ).resolve( 6 );
jQuery.when( a, b, c ).progress( function( a, b, c ) {
assert.strictEqual( a, 1, "first notify value ok" );
assert.strictEqual( b, 2, "second notify value ok" );
assert.strictEqual( c, 3, "third notify value ok" );
} ).done( function( a, b, c ) {
assert.strictEqual( a, 4, "first resolve value ok" );
assert.strictEqual( b, 5, "second resolve value ok" );
assert.strictEqual( c, 6, "third resolve value ok" );
} ).fail( function() {
assert.ok( false, "Error on resolve" );
} );
} );
QUnit.test( "jQuery.when - filtering", function( assert ) {
assert.expect( 2 );
function increment( x ) {
return x + 1;
}
QUnit.stop();
jQuery.when(
jQuery.Deferred().resolve( 3 ).then( increment ),
jQuery.Deferred().reject( 5 ).then( null, increment )
).done( function( four, six ) {
assert.strictEqual( four, 4, "resolved value incremented" );
assert.strictEqual( six, 6, "rejected value incremented" );
QUnit.start();
} );
} );
QUnit.test( "jQuery.when - exceptions", function( assert ) {
assert.expect( 2 );
function woops() {
throw "exception thrown";
}
QUnit.stop();
jQuery.Deferred().resolve().then( woops ).fail( function( doneException ) {
assert.strictEqual( doneException, "exception thrown", "throwing in done handler" );
jQuery.Deferred().reject().then( null, woops ).fail( function( failException ) {
assert.strictEqual( failException, "exception thrown", "throwing in fail handler" );
QUnit.start();
} );
} );
} );
QUnit.test( "jQuery.when - chaining", function( assert ) {
assert.expect( 4 );
var defer = jQuery.Deferred();
function chain() {
return defer;
}
function chainStandard() {
return Promise.resolve( "std deferred" );
}
QUnit.stop();
jQuery.when(
jQuery.Deferred().resolve( 3 ).then( chain ),
jQuery.Deferred().reject( 5 ).then( null, chain ),
jQuery.Deferred().resolve( 3 ).then( chainStandard ),
jQuery.Deferred().reject( 5 ).then( null, chainStandard )
).done( function( v1, v2, s1, s2 ) {
assert.strictEqual( v1, "other deferred", "chaining in done handler" );
assert.strictEqual( v2, "other deferred", "chaining in fail handler" );
assert.strictEqual( s1, "std deferred", "chaining thenable in done handler" );
assert.strictEqual( s2, "std deferred", "chaining thenable in fail handler" );
QUnit.start();
} );
defer.resolve( "other deferred" );
} );
|