@@ -714,10 +714,7 @@ describe('form', function() {
714
714
expect ( form . $error . maxlength [ 0 ] . $name ) . toBe ( 'childform' ) ;
715
715
716
716
inputController . $setPristine ( ) ;
717
- // this assertion prevents to propagate prestine to the parent form
718
- // expect(form.$dirty).toBe(true);
719
-
720
- form . $setPristine ( ) ;
717
+ expect ( form . $dirty ) . toBe ( false ) ;
721
718
722
719
// remove child form
723
720
form . $removeControl ( childformController ) ;
@@ -1057,7 +1054,7 @@ describe('form', function() {
1057
1054
1058
1055
childFormCtrl . $setDirty ( ) ;
1059
1056
scope . $apply ( ) ;
1060
- expect ( parentForm ) . not . toBePristine ( ) ;
1057
+ expect ( parentForm ) . toBeDirty ( ) ;
1061
1058
1062
1059
childFormCtrl . $setPristine ( ) ;
1063
1060
scope . $apply ( ) ;
@@ -1068,55 +1065,138 @@ describe('form', function() {
1068
1065
it ( 'should be pristine if all the nested controls are pristine' , function ( ) {
1069
1066
doc = $compile (
1070
1067
'<form name="form">' +
1071
- '<input ng-model="inputModel1" name="input1">' +
1072
- '<input ng-model="inputModel2" name="input2">' +
1068
+ '<div ng-form name="childForm">' +
1069
+ '<input ng-model="inputModel1" name="input1">' +
1070
+ '<input ng-model="inputModel2" name="input2">' +
1071
+ '</div>' +
1073
1072
'</form>' ) ( scope ) ;
1074
1073
1075
1074
var form = doc ,
1076
- formCtrl = scope . form ,
1075
+ childForm = form . find ( 'div' ) . eq ( 0 ) ,
1077
1076
input1 = form . find ( 'input' ) . eq ( 0 ) ,
1078
1077
input2 = form . find ( 'input' ) . eq ( 1 ) ,
1079
1078
inputCtrl1 = input1 . controller ( 'ngModel' ) ,
1080
1079
inputCtrl2 = input2 . controller ( 'ngModel' ) ;
1081
1080
1082
1081
inputCtrl1 . $setDirty ( ) ;
1082
+ inputCtrl1 . $setDirty ( ) ;
1083
+ scope . $apply ( ) ;
1084
+ expect ( form ) . toBeDirty ( ) ;
1085
+ expect ( childForm ) . toBeDirty ( ) ;
1086
+
1087
+ inputCtrl2 . $setDirty ( ) ;
1083
1088
inputCtrl2 . $setDirty ( ) ;
1084
1089
scope . $apply ( ) ;
1085
- expect ( form ) . not . toBePristine ( ) ;
1090
+ expect ( form ) . toBeDirty ( ) ;
1091
+ expect ( childForm ) . toBeDirty ( ) ;
1086
1092
1087
1093
inputCtrl1 . $setPristine ( ) ;
1088
1094
scope . $apply ( ) ;
1089
- expect ( form ) . not . toBePristine ( ) ;
1095
+ expect ( form ) . toBeDirty ( ) ;
1096
+ expect ( childForm ) . toBeDirty ( ) ;
1090
1097
1091
1098
inputCtrl2 . $setPristine ( ) ;
1092
1099
scope . $apply ( ) ;
1093
1100
expect ( form ) . toBePristine ( ) ;
1101
+ expect ( childForm ) . toBePristine ( ) ;
1094
1102
} ) ;
1095
1103
1096
1104
it ( 'should be pristine if all the nested forms are pristine' , function ( ) {
1097
1105
doc = $compile (
1098
- '<form name="form">' +
1099
- '<div ng-form name="childForm1"></div>' +
1100
- '<div ng-form name="childForm2"></div>' +
1106
+ '<form name="outerForm1">' +
1107
+ '<div ng-form name="outerForm2">' +
1108
+ '<div ng-form name="childForm1"></div>' +
1109
+ '<div ng-form name="childForm2"></div>' +
1110
+ '</div>' +
1101
1111
'</form>' ) ( scope ) ;
1102
1112
1103
- var form = doc ,
1104
- formCtrl = scope . form ,
1113
+ var outerForm1 = doc ,
1114
+ outerForm2 = doc . find ( 'div' ) . eq ( 0 ) ,
1105
1115
childFormCtrl1 = scope . childForm1 ,
1106
1116
childFormCtrl2 = scope . childForm2 ;
1107
1117
1108
1118
childFormCtrl1 . $setDirty ( ) ;
1119
+ scope . $apply ( ) ;
1120
+ expect ( outerForm1 ) . toBeDirty ( ) ;
1121
+ expect ( outerForm2 ) . toBeDirty ( ) ;
1109
1122
childFormCtrl2 . $setDirty ( ) ;
1110
1123
scope . $apply ( ) ;
1111
- expect ( form ) . not . toBePristine ( ) ;
1124
+ expect ( outerForm1 ) . toBeDirty ( ) ;
1125
+ expect ( outerForm2 ) . toBeDirty ( ) ;
1112
1126
1113
1127
childFormCtrl1 . $setPristine ( ) ;
1114
1128
scope . $apply ( ) ;
1115
- expect ( form ) . not . toBePristine ( ) ;
1129
+ expect ( outerForm1 ) . toBeDirty ( ) ;
1130
+ expect ( outerForm2 ) . toBeDirty ( ) ;
1116
1131
1117
1132
childFormCtrl2 . $setPristine ( ) ;
1118
1133
scope . $apply ( ) ;
1119
- expect ( form ) . toBePristine ( ) ;
1134
+ expect ( outerForm1 ) . toBePristine ( ) ;
1135
+ expect ( outerForm2 ) . toBePristine ( ) ;
1136
+ } ) ;
1137
+
1138
+ it ( 'should properly handle added/removed controls' , function ( ) {
1139
+
1140
+ var test = function ( input , inputCtrl ) {
1141
+ doc = $compile (
1142
+ '<form name="outerForm">' +
1143
+ '<div ng-form name="innerForm"></div>' +
1144
+ '</form>' ) ( scope ) ;
1145
+
1146
+ var outerForm = doc ,
1147
+ innerForm = doc . find ( 'div' ) . eq ( 0 ) ,
1148
+ innerFormCtrl = innerForm . controller ( 'form' ) ;
1149
+
1150
+ inputCtrl . $setDirty ( ) ;
1151
+
1152
+ // just add control does not change form pristine-ness
1153
+ innerFormCtrl . $addControl ( inputCtrl ) ;
1154
+ scope . $apply ( ) ;
1155
+ expect ( innerForm ) . toBePristine ( ) ;
1156
+
1157
+ // change after adding
1158
+ inputCtrl . $setDirty ( ) ;
1159
+ scope . $apply ( ) ;
1160
+ expect ( innerForm ) . toBeDirty ( ) ;
1161
+
1162
+ innerFormCtrl . $removeControl ( inputCtrl ) ;
1163
+
1164
+ // removed control does not affect
1165
+ inputCtrl . $setPristine ( ) ;
1166
+ scope . $apply ( ) ;
1167
+ expect ( innerForm ) . toBeDirty ( ) ;
1168
+
1169
+ innerFormCtrl . $addControl ( inputCtrl ) ;
1170
+ scope . $apply ( ) ;
1171
+ expect ( innerForm ) . toBeDirty ( ) ;
1172
+
1173
+ inputCtrl . $setPristine ( ) ;
1174
+ scope . $apply ( ) ;
1175
+ expect ( innerForm ) . toBePristine ( ) ;
1176
+
1177
+ innerFormCtrl . $removeControl ( inputCtrl ) ;
1178
+ inputCtrl . $setPristine ( ) ;
1179
+ innerFormCtrl . $addControl ( inputCtrl ) ;
1180
+ scope . $apply ( ) ;
1181
+ expect ( innerForm ) . toBePristine ( ) ;
1182
+
1183
+ inputCtrl . $setDirty ( ) ;
1184
+ scope . $apply ( ) ;
1185
+ expect ( outerForm ) . toBeDirty ( ) ;
1186
+ } ;
1187
+
1188
+ var input1 = $compile ( '<input ng-model="inputModel" name="input">' ) ( scope ) ,
1189
+ inputCtrl1 = input1 . controller ( 'ngModel' ) ,
1190
+
1191
+ input2 = $compile ( '<div ng-form name="input"></div>' ) ( scope ) ,
1192
+ inputCtrl2 = input2 . controller ( 'form' ) ;
1193
+
1194
+ // test for input
1195
+ test ( input1 , inputCtrl1 ) ;
1196
+ dealoc ( doc ) ;
1197
+
1198
+ // test for ng-form
1199
+ test ( input2 , inputCtrl2 ) ;
1120
1200
} ) ;
1121
1201
} ) ;
1122
1202
0 commit comments