@@ -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,50 @@ 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
+ // If no javascript animation is found, animator is undefined
801
+ if ( animator ) {
802
+ runners . push ( animator ) ;
803
+ }
804
+ return animator ;
805
+ } ;
806
+
807
+ animateJsConstructor . $closeAndFlush = function ( ) {
808
+ runners . forEach ( function ( runner ) {
809
+ runner . end ( ) ;
810
+ } ) ;
811
+ runners = [ ] ;
812
+ } ;
813
+
814
+ return animateJsConstructor ;
815
+ } ] ) ;
816
+
817
+ $provide . decorator ( '$animateCss' , [ '$delegate' , function ( $delegate ) {
818
+ var runners = [ ] ;
819
+
820
+ var animateCssConstructor = function ( element , options ) {
821
+ var animator = $delegate ( element , options ) ;
822
+ runners . push ( animator ) ;
823
+ return animator ;
824
+ } ;
825
+
826
+ animateCssConstructor . $closeAndFlush = function ( ) {
827
+ runners . forEach ( function ( runner ) {
828
+ runner . end ( ) ;
829
+ } ) ;
830
+ runners = [ ] ;
831
+ } ;
832
+
833
+ return animateCssConstructor ;
834
+ } ] ) ;
835
+
836
+ $provide . decorator ( '$animate' , [ '$delegate' , '$timeout' , '$browser' , '$$rAF' , '$animateCss' , '$$animateJs' ,
787
837
'$$forceReflow' , '$$animateAsyncRun' , '$rootScope' ,
788
- function ( $delegate , $timeout , $browser , $$rAF ,
838
+ function ( $delegate , $timeout , $browser , $$rAF , $animateCss , $$animateJs ,
789
839
$$forceReflow , $$animateAsyncRun , $rootScope ) {
790
840
var animate = {
791
841
queue : [ ] ,
@@ -797,7 +847,35 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
797
847
return $$forceReflow . totalReflows ;
798
848
} ,
799
849
enabled : $delegate . enabled ,
800
- flush : function ( ) {
850
+ /**
851
+ * @ngdoc method
852
+ * @name $animate#closeAndFlush
853
+ * @description
854
+ *
855
+ * This method will close all pending animations (both {@link ngAnimate#javascript-based-animations Javascript}
856
+ * and {@link ngAnimate.$animateCss CSS}) and it will also flush any remaining animation frames and/or callbacks.
857
+ */
858
+ closeAndFlush : function ( ) {
859
+ // we allow the flush command to swallow the errors
860
+ // because depending on whether CSS or JS animations are
861
+ // used, there may not be a RAF flush. The primary flush
862
+ // at the end of this function must throw an exception
863
+ // because it will track if there were pending animations
864
+ this . flush ( true ) ;
865
+ $animateCss . $closeAndFlush ( ) ;
866
+ $$animateJs . $closeAndFlush ( ) ;
867
+ this . flush ( ) ;
868
+ } ,
869
+ /**
870
+ * @ngdoc method
871
+ * @name $animate#flush
872
+ * @description
873
+ *
874
+ * This method is used to flush the pending callbacks and animation frames to either start
875
+ * an animation or conclude an animation. Note that this will not actually close an
876
+ * actively running animation (see {@link ngMock.$animate#closeAndFlush `closeAndFlush()`} for that).
877
+ */
878
+ flush : function ( hideErrors ) {
801
879
$rootScope . $digest ( ) ;
802
880
803
881
var doNextRun , somethingFlushed = false ;
@@ -814,7 +892,7 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
814
892
}
815
893
} while ( doNextRun ) ;
816
894
817
- if ( ! somethingFlushed ) {
895
+ if ( ! somethingFlushed && ! hideErrors ) {
818
896
throw new Error ( 'No pending animations ready to be closed or flushed' ) ;
819
897
}
820
898
0 commit comments