@@ -51,6 +51,8 @@ describe('NgModelController', function() {
51
51
52
52
53
53
it ( 'should init the properties' , function ( ) {
54
+ expect ( ctrl . $untouched ) . toBe ( true ) ;
55
+ expect ( ctrl . $touched ) . toBe ( false ) ;
54
56
expect ( ctrl . $dirty ) . toBe ( false ) ;
55
57
expect ( ctrl . $pristine ) . toBe ( true ) ;
56
58
expect ( ctrl . $valid ) . toBe ( true ) ;
@@ -133,6 +135,28 @@ describe('NgModelController', function() {
133
135
} ) ;
134
136
} ) ;
135
137
138
+ describe ( 'setUntouched' , function ( ) {
139
+
140
+ it ( 'should set control to its untouched state' , function ( ) {
141
+ ctrl . $setTouched ( ) ;
142
+
143
+ ctrl . $setUntouched ( ) ;
144
+ expect ( ctrl . $touched ) . toBe ( false ) ;
145
+ expect ( ctrl . $untouched ) . toBe ( true ) ;
146
+ } ) ;
147
+ } ) ;
148
+
149
+ describe ( 'setTouched' , function ( ) {
150
+
151
+ it ( 'should set control to its touched state' , function ( ) {
152
+ ctrl . $setUntouched ( ) ;
153
+
154
+ ctrl . $setTouched ( ) ;
155
+ expect ( ctrl . $touched ) . toBe ( true ) ;
156
+ expect ( ctrl . $untouched ) . toBe ( false ) ;
157
+ } ) ;
158
+ } ) ;
159
+
136
160
describe ( 'view -> model' , function ( ) {
137
161
138
162
it ( 'should set the value to $viewValue' , function ( ) {
@@ -265,13 +289,14 @@ describe('NgModelController', function() {
265
289
266
290
describe ( 'ngModel' , function ( ) {
267
291
268
- it ( 'should set css classes (ng-valid, ng-invalid, ng-pristine, ng-dirty)' ,
292
+ it ( 'should set css classes (ng-valid, ng-invalid, ng-pristine, ng-dirty, ng-untouched, ng-touched )' ,
269
293
inject ( function ( $compile , $rootScope , $sniffer ) {
270
294
var element = $compile ( '<input type="email" ng-model="value" />' ) ( $rootScope ) ;
271
295
272
296
$rootScope . $digest ( ) ;
273
297
expect ( element ) . toBeValid ( ) ;
274
298
expect ( element ) . toBePristine ( ) ;
299
+ expect ( element ) . toBeUntouched ( ) ;
275
300
expect ( element . hasClass ( 'ng-valid-email' ) ) . toBe ( true ) ;
276
301
expect ( element . hasClass ( 'ng-invalid-email' ) ) . toBe ( false ) ;
277
302
@@ -297,6 +322,9 @@ describe('ngModel', function() {
297
322
expect ( element . hasClass ( 'ng-valid-email' ) ) . toBe ( true ) ;
298
323
expect ( element . hasClass ( 'ng-invalid-email' ) ) . toBe ( false ) ;
299
324
325
+ browserTrigger ( element , 'blur' ) ;
326
+ expect ( element ) . toBeTouched ( ) ;
327
+
300
328
dealoc ( element ) ;
301
329
} ) ) ;
302
330
@@ -309,6 +337,23 @@ describe('ngModel', function() {
309
337
expect ( element ) . toHaveClass ( 'ng-invalid-required' ) ;
310
338
} ) ) ;
311
339
340
+ it ( 'should set the control touched state on "blur" event' , inject ( function ( $compile , $rootScope ) {
341
+ var element = $compile ( '<form name="myForm">' +
342
+ '<input name="myControl" ng-model="value" >' +
343
+ '</form>' ) ( $rootScope ) ;
344
+ var inputElm = element . find ( 'input' ) ;
345
+ var control = $rootScope . myForm . myControl ;
346
+
347
+ expect ( control . $touched ) . toBe ( false ) ;
348
+ expect ( control . $untouched ) . toBe ( true ) ;
349
+
350
+ browserTrigger ( inputElm , 'blur' ) ;
351
+ expect ( control . $touched ) . toBe ( true ) ;
352
+ expect ( control . $untouched ) . toBe ( false ) ;
353
+
354
+ dealoc ( element ) ;
355
+ } ) ) ;
356
+
312
357
313
358
it ( 'should register/deregister a nested ngModel with parent form when entering or leaving DOM' ,
314
359
inject ( function ( $compile , $rootScope ) {
@@ -2687,6 +2732,22 @@ describe('NgModel animations', function() {
2687
2732
assertValidAnimation ( animations [ 1 ] , 'addClass' , 'ng-pristine' ) ;
2688
2733
} ) ) ;
2689
2734
2735
+ it ( 'should trigger an animation when untouched' , inject ( function ( $animate ) {
2736
+ model . $setUntouched ( ) ;
2737
+
2738
+ var animations = findElementAnimations ( input , $animate . queue ) ;
2739
+ assertValidAnimation ( animations [ 0 ] , 'setClass' , 'ng-untouched' ) ;
2740
+ expect ( animations [ 0 ] . args [ 2 ] ) . toBe ( 'ng-touched' ) ;
2741
+ } ) ) ;
2742
+
2743
+ it ( 'should trigger an animation when touched' , inject ( function ( $animate ) {
2744
+ model . $setTouched ( ) ;
2745
+
2746
+ var animations = findElementAnimations ( input , $animate . queue ) ;
2747
+ assertValidAnimation ( animations [ 0 ] , 'setClass' , 'ng-touched' , 'ng-untouched' ) ;
2748
+ expect ( animations [ 0 ] . args [ 2 ] ) . toBe ( 'ng-untouched' ) ;
2749
+ } ) ) ;
2750
+
2690
2751
it ( 'should trigger custom errors as addClass/removeClass when invalid/valid' , inject ( function ( $animate ) {
2691
2752
model . $setValidity ( 'custom-error' , false ) ;
2692
2753
0 commit comments