@@ -683,7 +683,7 @@ describe('q', function() {
683
683
} ) ;
684
684
685
685
686
- describe ( 'all' , function ( ) {
686
+ describe ( 'all (array) ' , function ( ) {
687
687
it ( 'should resolve all of nothing' , function ( ) {
688
688
var result ;
689
689
q . all ( [ ] ) . then ( function ( r ) { result = r ; } ) ;
@@ -742,6 +742,72 @@ describe('q', function() {
742
742
} ) ;
743
743
} ) ;
744
744
745
+ describe ( 'all (hash)' , function ( ) {
746
+ it ( 'should resolve all of nothing' , function ( ) {
747
+ var result ;
748
+ q . all ( { } ) . then ( function ( r ) { result = r ; } ) ;
749
+ mockNextTick . flush ( ) ;
750
+ expect ( result ) . toEqual ( { } ) ;
751
+ } ) ;
752
+
753
+
754
+ it ( 'should take a hash of promises and return a promise for a hash of results' , function ( ) {
755
+ var deferred1 = defer ( ) ,
756
+ deferred2 = defer ( ) ;
757
+
758
+ q . all ( { en : promise , fr : deferred1 . promise , es : deferred2 . promise } ) . then ( success ( ) , error ( ) ) ;
759
+ expect ( logStr ( ) ) . toBe ( '' ) ;
760
+ syncResolve ( deferred , 'hi' ) ;
761
+ expect ( logStr ( ) ) . toBe ( '' ) ;
762
+ syncResolve ( deferred2 , 'hola' ) ;
763
+ expect ( logStr ( ) ) . toBe ( '' ) ;
764
+ syncResolve ( deferred1 , 'salut' ) ;
765
+ expect ( logStr ( ) ) . toBe ( 'success({en:hi,es:hola,fr:salut})' ) ;
766
+ } ) ;
767
+
768
+
769
+ it ( 'should reject the derived promise if at least one of the promises in the hash is rejected' ,
770
+ function ( ) {
771
+ var deferred1 = defer ( ) ,
772
+ deferred2 = defer ( ) ;
773
+
774
+ q . all ( { en : promise , fr : deferred1 . promise , es : deferred2 . promise } ) . then ( success ( ) , error ( ) ) ;
775
+ expect ( logStr ( ) ) . toBe ( '' ) ;
776
+ syncResolve ( deferred2 , 'hola' ) ;
777
+ expect ( logStr ( ) ) . toBe ( '' ) ;
778
+ syncReject ( deferred1 , 'oops' ) ;
779
+ expect ( logStr ( ) ) . toBe ( 'error(oops)' ) ;
780
+ } ) ;
781
+
782
+
783
+ it ( 'should ignore multiple resolutions of an (evil) hash promise' , function ( ) {
784
+ var evilPromise = {
785
+ then : function ( success , error ) {
786
+ evilPromise . success = success ;
787
+ evilPromise . error = error ;
788
+ }
789
+ }
790
+
791
+ q . all ( { good : promise , evil : evilPromise } ) . then ( success ( ) , error ( ) ) ;
792
+ expect ( logStr ( ) ) . toBe ( '' ) ;
793
+
794
+ evilPromise . success ( 'first' ) ;
795
+ evilPromise . success ( 'muhaha' ) ;
796
+ evilPromise . error ( 'arghhh' ) ;
797
+ expect ( logStr ( ) ) . toBe ( '' ) ;
798
+
799
+ syncResolve ( deferred , 'done' ) ;
800
+ expect ( logStr ( ) ) . toBe ( 'success({evil:first,good:done})' ) ;
801
+ } ) ;
802
+
803
+ it ( 'should handle correctly situation when given the same promise several times' , function ( ) {
804
+ q . all ( { first : promise , second : promise , third : promise } ) . then ( success ( ) , error ( ) ) ;
805
+ expect ( logStr ( ) ) . toBe ( '' ) ;
806
+
807
+ syncResolve ( deferred , 'done' ) ;
808
+ expect ( logStr ( ) ) . toBe ( 'success({first:done,second:done,third:done})' ) ;
809
+ } ) ;
810
+ } ) ;
745
811
746
812
describe ( 'exception logging' , function ( ) {
747
813
var mockExceptionLogger = {
0 commit comments