@@ -751,6 +751,15 @@ angular.mock.TzDate = function(offset, timestamp) {
751
751
angular . mock . TzDate . prototype = Date . prototype ;
752
752
/* jshint +W101 */
753
753
754
+
755
+ /**
756
+ * @ngdoc service
757
+ * @name $animate
758
+ *
759
+ * @description
760
+ * Mock implementation of the {@link ng.$animate `$animate`} service. Exposes two additional methods
761
+ * for testing animations.
762
+ */
754
763
angular . mock . animate = angular . module ( 'ngAnimateMock' , [ 'ng' ] )
755
764
756
765
. config ( [ '$provide' , function ( $provide ) {
@@ -783,9 +792,47 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
783
792
return queueFn ;
784
793
} ) ;
785
794
786
- $provide . decorator ( '$animate' , [ '$delegate' , '$timeout' , '$browser' , '$$rAF' ,
795
+ $provide . decorator ( '$$animateJs' , [ '$delegate' , function ( $delegate ) {
796
+ var runners = [ ] ;
797
+
798
+ var animateJsConstructor = function ( ) {
799
+ var animator = $delegate . apply ( $delegate , arguments ) ;
800
+ runners . push ( animator ) ;
801
+ return animator ;
802
+ } ;
803
+
804
+ animateJsConstructor . $closeAndFlush = function ( ) {
805
+ runners . forEach ( function ( runner ) {
806
+ runner . end ( ) ;
807
+ } ) ;
808
+ runners = [ ] ;
809
+ } ;
810
+
811
+ return animateJsConstructor ;
812
+ } ] ) ;
813
+
814
+ $provide . decorator ( '$animateCss' , [ '$delegate' , function ( $delegate ) {
815
+ var runners = [ ] ;
816
+
817
+ var animateCssConstructor = function ( element , options ) {
818
+ var animator = $delegate ( element , options ) ;
819
+ runners . push ( animator ) ;
820
+ return animator ;
821
+ } ;
822
+
823
+ animateCssConstructor . $closeAndFlush = function ( ) {
824
+ runners . forEach ( function ( runner ) {
825
+ runner . end ( ) ;
826
+ } ) ;
827
+ runners = [ ] ;
828
+ } ;
829
+
830
+ return animateCssConstructor ;
831
+ } ] ) ;
832
+
833
+ $provide . decorator ( '$animate' , [ '$delegate' , '$timeout' , '$browser' , '$$rAF' , '$animateCss' , '$$animateJs' ,
787
834
'$$forceReflow' , '$$animateAsyncRun' , '$rootScope' ,
788
- function ( $delegate , $timeout , $browser , $$rAF ,
835
+ function ( $delegate , $timeout , $browser , $$rAF , $animateCss , $$animateJs ,
789
836
$$forceReflow , $$animateAsyncRun , $rootScope ) {
790
837
var animate = {
791
838
queue : [ ] ,
@@ -797,7 +844,35 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
797
844
return $$forceReflow . totalReflows ;
798
845
} ,
799
846
enabled : $delegate . enabled ,
800
- flush : function ( ) {
847
+ /**
848
+ * @ngdoc method
849
+ * @name $animate#closeAndFlush
850
+ * @description
851
+ *
852
+ * This method will close all pending animations (both {@link ngAnimate#javascript-based-animations Javascript}
853
+ * and {@link ngAnimate.$animateCss CSS}) and it will also flush any remaining animation frames and/or callbacks.
854
+ */
855
+ closeAndFlush : function ( ) {
856
+ // we allow the flush command to swallow the errors
857
+ // because depending on whether CSS or JS animations are
858
+ // used, there may not be a RAF flush. The primary flush
859
+ // at the end of this function must throw an exception
860
+ // because it will track if there were pending animations
861
+ this . flush ( true ) ;
862
+ $animateCss . $closeAndFlush ( ) ;
863
+ $$animateJs . $closeAndFlush ( ) ;
864
+ this . flush ( ) ;
865
+ } ,
866
+ /**
867
+ * @ngdoc method
868
+ * @name $animate#flush
869
+ * @description
870
+ *
871
+ * This method is used to flush the pending callbacks and animation frames to either start
872
+ * an animation or conclude an animation. Note that this will not actually close an
873
+ * actively running animation (see {@link ngMock.$animate#closeAndFlush `closeAndFlush()`} for that).
874
+ */
875
+ flush : function ( hideErrors ) {
801
876
$rootScope . $digest ( ) ;
802
877
803
878
var doNextRun , somethingFlushed = false ;
@@ -814,7 +889,7 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
814
889
}
815
890
} while ( doNextRun ) ;
816
891
817
- if ( ! somethingFlushed ) {
892
+ if ( ! somethingFlushed && ! hideErrors ) {
818
893
throw new Error ( 'No pending animations ready to be closed or flushed' ) ;
819
894
}
820
895
0 commit comments