@@ -790,6 +790,7 @@ describe('animations', function() {
790
790
expect ( element ) . toHaveClass ( 'red' ) ;
791
791
} ) ) ;
792
792
793
+
793
794
it ( 'removeClass() should issue a removeClass animation with the correct DOM operation' , inject ( function ( $animate , $rootScope ) {
794
795
parent . append ( element ) ;
795
796
element . addClass ( 'blue' ) ;
@@ -934,6 +935,195 @@ describe('animations', function() {
934
935
} ) ) ;
935
936
} ) ;
936
937
938
+
939
+ describe ( '$animate.cancel()' , function ( ) {
940
+
941
+ it ( 'should cancel enter()' , inject ( function ( $animate , $rootScope ) {
942
+ expect ( parent . children ( ) . length ) . toBe ( 0 ) ;
943
+
944
+ options . foo = 'bar' ;
945
+ var spy = jasmine . createSpy ( 'cancelCatch' ) ;
946
+
947
+ var runner = $animate . enter ( element , parent , null , options ) ;
948
+
949
+ runner . catch ( spy ) ;
950
+
951
+ expect ( parent . children ( ) . length ) . toBe ( 1 ) ;
952
+
953
+ $rootScope . $digest ( ) ;
954
+
955
+ expect ( capturedAnimation [ 0 ] ) . toBe ( element ) ;
956
+ expect ( capturedAnimation [ 1 ] ) . toBe ( 'enter' ) ;
957
+ expect ( capturedAnimation [ 2 ] . foo ) . toEqual ( options . foo ) ;
958
+
959
+ $animate . cancel ( runner ) ;
960
+ // Since enter() immediately adds the element, we can only check if the
961
+ // element is still at the position
962
+ expect ( parent . children ( ) . length ) . toBe ( 1 ) ;
963
+
964
+ $rootScope . $digest ( ) ;
965
+
966
+ // Catch handler is called after digest
967
+ expect ( spy ) . toHaveBeenCalled ( ) ;
968
+ } ) ) ;
969
+
970
+
971
+ it ( 'should cancel move()' , inject ( function ( $animate , $rootScope ) {
972
+ parent . append ( element ) ;
973
+
974
+ expect ( parent . children ( ) . length ) . toBe ( 1 ) ;
975
+ expect ( parent2 . children ( ) . length ) . toBe ( 0 ) ;
976
+
977
+ options . foo = 'bar' ;
978
+ var spy = jasmine . createSpy ( 'cancelCatch' ) ;
979
+
980
+ var runner = $animate . move ( element , parent2 , null , options ) ;
981
+ runner . catch ( spy ) ;
982
+
983
+ expect ( parent . children ( ) . length ) . toBe ( 0 ) ;
984
+ expect ( parent2 . children ( ) . length ) . toBe ( 1 ) ;
985
+
986
+ $rootScope . $digest ( ) ;
987
+
988
+ expect ( capturedAnimation [ 0 ] ) . toBe ( element ) ;
989
+ expect ( capturedAnimation [ 1 ] ) . toBe ( 'move' ) ;
990
+ expect ( capturedAnimation [ 2 ] . foo ) . toEqual ( options . foo ) ;
991
+
992
+ $animate . cancel ( runner ) ;
993
+ // Since moves() immediately moves the element, we can only check if the
994
+ // element is still at the correct position
995
+ expect ( parent . children ( ) . length ) . toBe ( 0 ) ;
996
+ expect ( parent2 . children ( ) . length ) . toBe ( 1 ) ;
997
+
998
+ $rootScope . $digest ( ) ;
999
+
1000
+ // Catch handler is called after digest
1001
+ expect ( spy ) . toHaveBeenCalled ( ) ;
1002
+ } ) ) ;
1003
+
1004
+
1005
+ it ( 'cancel leave()' , inject ( function ( $animate , $rootScope ) {
1006
+ parent . append ( element ) ;
1007
+ options . foo = 'bar' ;
1008
+ var spy = jasmine . createSpy ( 'cancelCatch' ) ;
1009
+
1010
+ var runner = $animate . leave ( element , options ) ;
1011
+
1012
+ runner . catch ( spy ) ;
1013
+ $rootScope . $digest ( ) ;
1014
+
1015
+ expect ( capturedAnimation [ 0 ] ) . toBe ( element ) ;
1016
+ expect ( capturedAnimation [ 1 ] ) . toBe ( 'leave' ) ;
1017
+ expect ( capturedAnimation [ 2 ] . foo ) . toEqual ( options . foo ) ;
1018
+
1019
+ expect ( element . parent ( ) . length ) . toBe ( 1 ) ;
1020
+
1021
+ $animate . cancel ( runner ) ;
1022
+ // Animation concludes immediately
1023
+ expect ( element . parent ( ) . length ) . toBe ( 0 ) ;
1024
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
1025
+
1026
+ $rootScope . $digest ( ) ;
1027
+ // Catch handler is called after digest
1028
+ expect ( spy ) . toHaveBeenCalled ( ) ;
1029
+ } ) ) ;
1030
+
1031
+ it ( 'should cancel addClass()' , inject ( function ( $animate , $rootScope ) {
1032
+ parent . append ( element ) ;
1033
+ options . foo = 'bar' ;
1034
+ var runner = $animate . addClass ( element , 'red' , options ) ;
1035
+ var spy = jasmine . createSpy ( 'cancelCatch' ) ;
1036
+
1037
+ runner . catch ( spy ) ;
1038
+ $rootScope . $digest ( ) ;
1039
+
1040
+ expect ( capturedAnimation [ 0 ] ) . toBe ( element ) ;
1041
+ expect ( capturedAnimation [ 1 ] ) . toBe ( 'addClass' ) ;
1042
+ expect ( capturedAnimation [ 2 ] . foo ) . toEqual ( options . foo ) ;
1043
+
1044
+ $animate . cancel ( runner ) ;
1045
+ expect ( element ) . toHaveClass ( 'red' ) ;
1046
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
1047
+
1048
+ $rootScope . $digest ( ) ;
1049
+ expect ( spy ) . toHaveBeenCalled ( ) ;
1050
+ } ) ) ;
1051
+
1052
+
1053
+ it ( 'should cancel setClass()' , inject ( function ( $animate , $rootScope ) {
1054
+ parent . append ( element ) ;
1055
+ element . addClass ( 'red' ) ;
1056
+ options . foo = 'bar' ;
1057
+
1058
+ var runner = $animate . setClass ( element , 'blue' , 'red' , options ) ;
1059
+ var spy = jasmine . createSpy ( 'cancelCatch' ) ;
1060
+
1061
+ runner . catch ( spy ) ;
1062
+ $rootScope . $digest ( ) ;
1063
+
1064
+ expect ( capturedAnimation [ 0 ] ) . toBe ( element ) ;
1065
+ expect ( capturedAnimation [ 1 ] ) . toBe ( 'setClass' ) ;
1066
+ expect ( capturedAnimation [ 2 ] . foo ) . toEqual ( options . foo ) ;
1067
+
1068
+ $animate . cancel ( runner ) ;
1069
+ expect ( element ) . toHaveClass ( 'blue' ) ;
1070
+ expect ( element ) . not . toHaveClass ( 'red' ) ;
1071
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
1072
+
1073
+ $rootScope . $digest ( ) ;
1074
+ expect ( spy ) . toHaveBeenCalled ( ) ;
1075
+ } ) ) ;
1076
+
1077
+
1078
+ it ( 'should cancel removeClass()' , inject ( function ( $animate , $rootScope ) {
1079
+ parent . append ( element ) ;
1080
+ element . addClass ( 'red blue' ) ;
1081
+
1082
+ options . foo = 'bar' ;
1083
+ var runner = $animate . removeClass ( element , 'red' , options ) ;
1084
+ var spy = jasmine . createSpy ( 'cancelCatch' ) ;
1085
+
1086
+ runner . catch ( spy ) ;
1087
+ $rootScope . $digest ( ) ;
1088
+
1089
+ expect ( capturedAnimation [ 0 ] ) . toBe ( element ) ;
1090
+ expect ( capturedAnimation [ 1 ] ) . toBe ( 'removeClass' ) ;
1091
+ expect ( capturedAnimation [ 2 ] . foo ) . toEqual ( options . foo ) ;
1092
+
1093
+ $animate . cancel ( runner ) ;
1094
+ expect ( element ) . not . toHaveClass ( 'red' ) ;
1095
+ expect ( element ) . toHaveClass ( 'blue' ) ;
1096
+
1097
+ $rootScope . $digest ( ) ;
1098
+ expect ( spy ) . toHaveBeenCalled ( ) ;
1099
+ } ) ) ;
1100
+
1101
+
1102
+ it ( 'should cancel animate()' ,
1103
+ inject ( function ( $animate , $rootScope ) {
1104
+
1105
+ parent . append ( element ) ;
1106
+
1107
+ var fromStyle = { color : 'blue' } ;
1108
+ var options = { addClass : 'red' } ;
1109
+
1110
+ var runner = $animate . animate ( element , fromStyle , null , null , options ) ;
1111
+ var spy = jasmine . createSpy ( 'cancelCatch' ) ;
1112
+
1113
+ runner . catch ( spy ) ;
1114
+ $rootScope . $digest ( ) ;
1115
+
1116
+ expect ( capturedAnimation ) . toBeTruthy ( ) ;
1117
+
1118
+ $animate . cancel ( runner ) ;
1119
+ expect ( element ) . toHaveClass ( 'red' ) ;
1120
+
1121
+ $rootScope . $digest ( ) ;
1122
+ expect ( spy ) . toHaveBeenCalled ( ) ;
1123
+ } ) ) ;
1124
+ } ) ;
1125
+
1126
+
937
1127
describe ( 'parent animations' , function ( ) {
938
1128
they ( 'should not cancel a pre-digest parent class-based animation if a child $prop animation is set to run' ,
939
1129
[ 'structural' , 'class-based' ] , function ( animationType ) {
0 commit comments