@@ -870,6 +870,154 @@ describe('form', function() {
870
870
expect ( scope . form . $submitted ) . toBe ( false ) ;
871
871
} ) ;
872
872
} ) ;
873
+
874
+ describe ( 'ngFormOptions attributes' , function ( ) {
875
+ it ( 'should allow define a form as root' , function ( ) {
876
+ doc = jqLite (
877
+ '<ng:form name="parent">' +
878
+ '<ng:form name="child" ng-form-options="{root:true}">' +
879
+ '<input ng:model="modelA" name="inputA">' +
880
+ '<input ng:model="modelB" name="inputB">' +
881
+ '</ng:form>' +
882
+ '</ng:form>' ) ;
883
+ $compile ( doc ) ( scope ) ;
884
+
885
+ var parent = scope . parent ,
886
+ child = scope . child ,
887
+ inputA = child . inputA ,
888
+ inputB = child . inputB ;
889
+
890
+ inputA . $setValidity ( 'MyError' , false ) ;
891
+ inputB . $setValidity ( 'MyError' , false ) ;
892
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
893
+ expect ( child . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
894
+
895
+ inputA . $setValidity ( 'MyError' , true ) ;
896
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
897
+ expect ( child . $error . MyError ) . toEqual ( [ inputB ] ) ;
898
+
899
+ inputB . $setValidity ( 'MyError' , true ) ;
900
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
901
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
902
+
903
+ child . $setDirty ( ) ;
904
+ expect ( parent . $dirty ) . toBeFalsy ( ) ;
905
+
906
+ child . $setSubmitted ( ) ;
907
+ expect ( parent . $submitted ) . toBeFalsy ( ) ;
908
+ } ) ;
909
+
910
+ it ( 'should chain nested forms as default behaviour' , function ( ) {
911
+ doc = jqLite (
912
+ '<ng:form name="parent">' +
913
+ '<ng:form name="child" ng-form-options="{}">' +
914
+ '<input ng:model="modelA" name="inputA">' +
915
+ '<input ng:model="modelB" name="inputB">' +
916
+ '</ng:form>' +
917
+ '</ng:form>' ) ;
918
+ $compile ( doc ) ( scope ) ;
919
+
920
+ var parent = scope . parent ,
921
+ child = scope . child ,
922
+ inputA = child . inputA ,
923
+ inputB = child . inputB ;
924
+
925
+ inputA . $setValidity ( 'MyError' , false ) ;
926
+ inputB . $setValidity ( 'MyError' , false ) ;
927
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
928
+ expect ( child . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
929
+
930
+ inputA . $setValidity ( 'MyError' , true ) ;
931
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
932
+ expect ( child . $error . MyError ) . toEqual ( [ inputB ] ) ;
933
+
934
+ inputB . $setValidity ( 'MyError' , true ) ;
935
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
936
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
937
+
938
+ child . $setDirty ( ) ;
939
+ expect ( parent . $dirty ) . toBeTruthy ( ) ;
940
+
941
+ child . $setSubmitted ( ) ;
942
+ expect ( parent . $submitted ) . toBeTruthy ( ) ;
943
+ } ) ;
944
+
945
+ it ( 'should chain nested forms when "root" is false' , function ( ) {
946
+ doc = jqLite (
947
+ '<ng:form name="parent">' +
948
+ '<ng:form name="child" ng-form-options="{root:false}">' +
949
+ '<input ng:model="modelA" name="inputA">' +
950
+ '<input ng:model="modelB" name="inputB">' +
951
+ '</ng:form>' +
952
+ '</ng:form>' ) ;
953
+ $compile ( doc ) ( scope ) ;
954
+
955
+ var parent = scope . parent ,
956
+ child = scope . child ,
957
+ inputA = child . inputA ,
958
+ inputB = child . inputB ;
959
+
960
+ inputA . $setValidity ( 'MyError' , false ) ;
961
+ inputB . $setValidity ( 'MyError' , false ) ;
962
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
963
+ expect ( child . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
964
+
965
+ inputA . $setValidity ( 'MyError' , true ) ;
966
+ expect ( parent . $error . MyError ) . toEqual ( [ child ] ) ;
967
+ expect ( child . $error . MyError ) . toEqual ( [ inputB ] ) ;
968
+
969
+ inputB . $setValidity ( 'MyError' , true ) ;
970
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
971
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
972
+
973
+ child . $setDirty ( ) ;
974
+ expect ( parent . $dirty ) . toBeTruthy ( ) ;
975
+
976
+ child . $setSubmitted ( ) ;
977
+ expect ( parent . $submitted ) . toBeTruthy ( ) ;
978
+ } ) ;
979
+
980
+ it ( 'should maintain the default behavior for children of a root form' , function ( ) {
981
+ doc = jqLite (
982
+ '<ng:form name="parent">' +
983
+ '<ng:form name="child" ng-form-options="{root:true}">' +
984
+ '<ng:form name="grandchild">' +
985
+ '<input ng:model="modelA" name="inputA">' +
986
+ '<input ng:model="modelB" name="inputB">' +
987
+ '</ng:form>' +
988
+ '</ng:form>' +
989
+ '</ng:form>' ) ;
990
+ $compile ( doc ) ( scope ) ;
991
+
992
+ var parent = scope . parent ,
993
+ child = scope . child ,
994
+ grandchild = scope . grandchild ,
995
+ inputA = grandchild . inputA ,
996
+ inputB = grandchild . inputB ;
997
+
998
+ inputA . $setValidity ( 'MyError' , false ) ;
999
+ inputB . $setValidity ( 'MyError' , false ) ;
1000
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1001
+ expect ( child . $error . MyError ) . toEqual ( [ grandchild ] ) ;
1002
+ expect ( grandchild . $error . MyError ) . toEqual ( [ inputA , inputB ] ) ;
1003
+
1004
+ inputA . $setValidity ( 'MyError' , true ) ;
1005
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1006
+ expect ( child . $error . MyError ) . toEqual ( [ grandchild ] ) ;
1007
+ expect ( grandchild . $error . MyError ) . toEqual ( [ inputB ] ) ;
1008
+
1009
+ inputB . $setValidity ( 'MyError' , true ) ;
1010
+ expect ( parent . $error . MyError ) . toBeFalsy ( ) ;
1011
+ expect ( child . $error . MyError ) . toBeFalsy ( ) ;
1012
+ expect ( grandchild . $error . MyError ) . toBeFalsy ( ) ;
1013
+
1014
+ child . $setDirty ( ) ;
1015
+ expect ( parent . $dirty ) . toBeFalsy ( ) ;
1016
+
1017
+ child . $setSubmitted ( ) ;
1018
+ expect ( parent . $submitted ) . toBeFalsy ( ) ;
1019
+ } ) ;
1020
+ } ) ;
873
1021
} ) ;
874
1022
875
1023
describe ( 'form animations' , function ( ) {
@@ -947,4 +1095,5 @@ describe('form animations', function() {
947
1095
assertValidAnimation ( $animate . queue [ 2 ] , 'addClass' , 'ng-valid-custom-error' ) ;
948
1096
assertValidAnimation ( $animate . queue [ 3 ] , 'removeClass' , 'ng-invalid-custom-error' ) ;
949
1097
} ) ) ;
1098
+
950
1099
} ) ;
0 commit comments