@@ -345,6 +345,10 @@ describe('q', function() {
345
345
it ( 'should have a then method' , function ( ) {
346
346
expect ( typeof promise . then ) . toBe ( 'function' ) ;
347
347
} ) ;
348
+
349
+ it ( 'should have a always method' , function ( ) {
350
+ expect ( typeof promise . always ) . toBe ( 'function' ) ;
351
+ } ) ;
348
352
349
353
350
354
describe ( 'then' , function ( ) {
@@ -461,6 +465,134 @@ describe('q', function() {
461
465
expect ( log ) . toEqual ( [ 'error(oops!)' ] ) ;
462
466
} ) ;
463
467
} ) ;
468
+
469
+
470
+ describe ( 'always' , function ( ) {
471
+
472
+ it ( 'should not take an argument' ,
473
+ function ( ) {
474
+ promise . always ( success ( 1 ) )
475
+ syncResolve ( deferred , 'foo' ) ;
476
+ expect ( logStr ( ) ) . toBe ( 'success1()' ) ;
477
+ } ) ;
478
+
479
+ describe ( "when the promise is fulfilled" , function ( ) {
480
+
481
+ it ( 'should call the callback' ,
482
+ function ( ) {
483
+ promise . then ( success ( 1 ) )
484
+ . always ( success ( 2 ) )
485
+ syncResolve ( deferred , 'foo' ) ;
486
+ expect ( logStr ( ) ) . toBe ( 'success1(foo); success2()' ) ;
487
+ } ) ;
488
+
489
+ it ( 'should fulfill with the original value' ,
490
+ function ( ) {
491
+ promise . always ( success ( 1 ) )
492
+ . then ( success ( 2 ) , error ( 2 ) )
493
+ syncResolve ( deferred , 'foo' ) ;
494
+ expect ( logStr ( ) ) . toBe ( 'success1(); success2(foo)' ) ;
495
+ } ) ;
496
+
497
+ describe ( "when the callback returns a promise" , function ( ) {
498
+
499
+ describe ( "that is fulfilled" , function ( ) {
500
+ it ( "should fulfill with the original reason after that promise resolves" ,
501
+ function ( ) {
502
+ var returnedDef = defer ( )
503
+ returnedDef . resolve ( 'bar' ) ;
504
+ promise . always ( success ( 1 , returnedDef . promise ) )
505
+ . then ( success ( 2 ) )
506
+ syncResolve ( deferred , 'foo' ) ;
507
+ expect ( logStr ( ) ) . toBe ( 'success1(); success2(foo)' ) ;
508
+ } ) ;
509
+ } ) ;
510
+
511
+ describe ( "that is rejected" , function ( ) {
512
+ it ( "should reject with this new rejection reason" ,
513
+ function ( ) {
514
+ var returnedDef = defer ( )
515
+ returnedDef . reject ( 'bar' ) ;
516
+ promise . always ( success ( 1 , returnedDef . promise ) )
517
+ . then ( success ( 2 ) , error ( 1 ) )
518
+ syncResolve ( deferred , 'foo' ) ;
519
+ expect ( logStr ( ) ) . toBe ( 'success1(); error1(bar)' ) ;
520
+ } ) ;
521
+ } ) ;
522
+
523
+ } ) ;
524
+
525
+ describe ( "when the callback throws an exception" , function ( ) {
526
+ it ( "should reject with this new exception" , function ( ) {
527
+ promise . always ( error ( 1 , "exception" , true ) )
528
+ . then ( success ( 1 ) , error ( 2 ) )
529
+ syncResolve ( deferred , 'foo' ) ;
530
+ expect ( logStr ( ) ) . toBe ( 'error1(); error2(exception)' ) ;
531
+ } ) ;
532
+ } ) ;
533
+
534
+ } ) ;
535
+
536
+
537
+ describe ( "when the promise is rejected" , function ( ) {
538
+
539
+ it ( "should call the callback" , function ( ) {
540
+ promise . always ( success ( 1 ) )
541
+ . then ( success ( 2 ) , error ( 1 ) )
542
+ syncReject ( deferred , 'foo' ) ;
543
+ expect ( logStr ( ) ) . toBe ( 'success1(); error1(foo)' ) ;
544
+ } ) ;
545
+
546
+ it ( 'should reject with the original reason' , function ( ) {
547
+ promise . always ( success ( 1 ) , "hello" )
548
+ . then ( success ( 2 ) , error ( 2 ) )
549
+ syncReject ( deferred , 'original' ) ;
550
+ expect ( logStr ( ) ) . toBe ( 'success1(); error2(original)' ) ;
551
+ } ) ;
552
+
553
+ describe ( "when the callback returns a promise" , function ( ) {
554
+
555
+ describe ( "that is fulfilled" , function ( ) {
556
+
557
+ it ( "should reject with the original reason after that promise resolves" , function ( ) {
558
+ var returnedDef = defer ( )
559
+ returnedDef . resolve ( 'bar' ) ;
560
+ promise . always ( success ( 1 , returnedDef . promise ) )
561
+ . then ( success ( 2 ) , error ( 2 ) )
562
+ syncReject ( deferred , 'original' ) ;
563
+ expect ( logStr ( ) ) . toBe ( 'success1(); error2(original)' ) ;
564
+ } ) ;
565
+
566
+ } ) ;
567
+
568
+ describe ( "that is rejected" , function ( ) {
569
+
570
+ it ( "should reject with the new reason" , function ( ) {
571
+ var returnedDef = defer ( )
572
+ returnedDef . reject ( 'bar' ) ;
573
+ promise . always ( success ( 1 , returnedDef . promise ) )
574
+ . then ( success ( 2 ) , error ( 1 ) )
575
+ syncResolve ( deferred , 'foo' ) ;
576
+ expect ( logStr ( ) ) . toBe ( 'success1(); error1(bar)' ) ;
577
+ } ) ;
578
+
579
+ } ) ;
580
+
581
+ } ) ;
582
+
583
+ describe ( "when the callback throws an exception" , function ( ) {
584
+
585
+ it ( "should reject with this new exception" , function ( ) {
586
+ promise . always ( error ( 1 , "exception" , true ) )
587
+ . then ( success ( 1 ) , error ( 2 ) )
588
+ syncResolve ( deferred , 'foo' ) ;
589
+ expect ( logStr ( ) ) . toBe ( 'error1(); error2(exception)' ) ;
590
+ } ) ;
591
+
592
+ } ) ;
593
+
594
+ } ) ;
595
+ } ) ;
464
596
} ) ;
465
597
} ) ;
466
598
0 commit comments