@@ -751,6 +751,15 @@ angular.mock.TzDate = function(offset, timestamp) {
751751angular . mock . TzDate . prototype = Date . prototype ;
752752/* jshint +W101 */
753753
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+ */
754763angular . mock . animate = angular . module ( 'ngAnimateMock' , [ 'ng' ] )
755764
756765 . config ( [ '$provide' , function ( $provide ) {
@@ -783,9 +792,50 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
783792 return queueFn ;
784793 } ) ;
785794
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' ,
787837 '$$forceReflow' , '$$animateAsyncRun' , '$rootScope' ,
788- function ( $delegate , $timeout , $browser , $$rAF ,
838+ function ( $delegate , $timeout , $browser , $$rAF , $animateCss , $$animateJs ,
789839 $$forceReflow , $$animateAsyncRun , $rootScope ) {
790840 var animate = {
791841 queue : [ ] ,
@@ -797,7 +847,35 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
797847 return $$forceReflow . totalReflows ;
798848 } ,
799849 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 ) {
801879 $rootScope . $digest ( ) ;
802880
803881 var doNextRun , somethingFlushed = false ;
@@ -814,7 +892,7 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
814892 }
815893 } while ( doNextRun ) ;
816894
817- if ( ! somethingFlushed ) {
895+ if ( ! somethingFlushed && ! hideErrors ) {
818896 throw new Error ( 'No pending animations ready to be closed or flushed' ) ;
819897 }
820898
0 commit comments