@@ -294,27 +294,13 @@ describe('NgModelController', function() {
294
294
} ;
295
295
296
296
ctrl . $modelValue = 'test' ;
297
+ ctrl . $$invalidModelValue = undefined ;
297
298
ctrl . $validate ( ) ;
298
299
299
300
expect ( ctrl . $valid ) . toBe ( false ) ;
300
301
301
302
ctrl . $modelValue = 'TEST' ;
302
- ctrl . $validate ( ) ;
303
-
304
- expect ( ctrl . $valid ) . toBe ( true ) ;
305
- } ) ;
306
-
307
- it ( 'should perform validations when $validate() is called' , function ( ) {
308
- ctrl . $validators . uppercase = function ( value ) {
309
- return ( / ^ [ A - Z ] + $ / ) . test ( value ) ;
310
- } ;
311
-
312
- ctrl . $modelValue = 'test' ;
313
- ctrl . $validate ( ) ;
314
-
315
- expect ( ctrl . $valid ) . toBe ( false ) ;
316
-
317
- ctrl . $modelValue = 'TEST' ;
303
+ ctrl . $$invalidModelValue = undefined ;
318
304
ctrl . $validate ( ) ;
319
305
320
306
expect ( ctrl . $valid ) . toBe ( true ) ;
@@ -403,6 +389,7 @@ describe('NgModelController', function() {
403
389
} ;
404
390
} ;
405
391
392
+ ctrl . $modelValue = undefined ;
406
393
ctrl . $validators . a = curry ( true ) ;
407
394
ctrl . $validators . b = curry ( true ) ;
408
395
ctrl . $validators . c = curry ( false ) ;
@@ -423,6 +410,7 @@ describe('NgModelController', function() {
423
410
} ;
424
411
} ;
425
412
413
+ ctrl . $modelValue = undefined ;
426
414
ctrl . $validators . unique = curry ( false ) ;
427
415
ctrl . $validators . tooLong = curry ( false ) ;
428
416
ctrl . $validators . notNumeric = curry ( true ) ;
@@ -1489,6 +1477,80 @@ describe('input', function() {
1489
1477
expect ( inputElm ) . toBeValid ( ) ;
1490
1478
expect ( scope . form . input . $error . maxlength ) . not . toBe ( true ) ;
1491
1479
} ) ;
1480
+
1481
+ it ( 'should assign the correct model after an observed validator became valid' , function ( ) {
1482
+ compileInput ( '<input type="text" name="input" ng-model="value" maxlength="{{ max }}" />' ) ;
1483
+
1484
+ scope . $apply ( function ( ) {
1485
+ scope . max = 1 ;
1486
+ } ) ;
1487
+ changeInputValueTo ( '12345' ) ;
1488
+ expect ( scope . value ) . toBeUndefined ( ) ;
1489
+
1490
+ scope . $apply ( function ( ) {
1491
+ scope . max = 6 ;
1492
+ } ) ;
1493
+ expect ( scope . value ) . toBe ( '12345' ) ;
1494
+ } ) ;
1495
+
1496
+ it ( 'should assign the correct model after an observed validator became invalid' , function ( ) {
1497
+ compileInput ( '<input type="text" name="input" ng-model="value" maxlength="{{ max }}" />' ) ;
1498
+
1499
+ scope . $apply ( function ( ) {
1500
+ scope . max = 6 ;
1501
+ } ) ;
1502
+ changeInputValueTo ( '12345' ) ;
1503
+ expect ( scope . value ) . toBe ( '12345' ) ;
1504
+
1505
+ scope . $apply ( function ( ) {
1506
+ scope . max = 1 ;
1507
+ } ) ;
1508
+ expect ( scope . value ) . toBeUndefined ( ) ;
1509
+ } ) ;
1510
+
1511
+ it ( 'should leave the value as invalid if observed maxlength changed, but is still invalid' , function ( ) {
1512
+ compileInput ( '<input type="text" name="input" ng-model="value" maxlength="{{ max }}" />' ) ;
1513
+ scope . $apply ( function ( ) {
1514
+ scope . max = 1 ;
1515
+ } ) ;
1516
+
1517
+ changeInputValueTo ( '12345' ) ;
1518
+ expect ( inputElm ) . toBeInvalid ( ) ;
1519
+ expect ( scope . form . input . $error . maxlength ) . toBe ( true ) ;
1520
+ expect ( scope . value ) . toBeUndefined ( ) ;
1521
+
1522
+ scope . $apply ( function ( ) {
1523
+ scope . max = 3 ;
1524
+ } ) ;
1525
+
1526
+ expect ( inputElm ) . toBeInvalid ( ) ;
1527
+ expect ( scope . form . input . $error . maxlength ) . toBe ( true ) ;
1528
+ expect ( scope . value ) . toBeUndefined ( ) ;
1529
+ } ) ;
1530
+
1531
+ it ( 'should not notify if observed maxlength changed, but is still invalid' , function ( ) {
1532
+ compileInput ( '<input type="text" name="input" ng-model="value" ng-change="ngChangeSpy()" ' +
1533
+ 'maxlength="{{ max }}" />' ) ;
1534
+
1535
+ scope . $apply ( function ( ) {
1536
+ scope . max = 1 ;
1537
+ } ) ;
1538
+ changeInputValueTo ( '12345' ) ;
1539
+
1540
+ scope . ngChangeSpy = jasmine . createSpy ( ) ;
1541
+ scope . $apply ( function ( ) {
1542
+ scope . max = 3 ;
1543
+ } ) ;
1544
+
1545
+ expect ( scope . ngChangeSpy ) . not . toHaveBeenCalled ( ) ;
1546
+ } ) ;
1547
+
1548
+ it ( 'should leave the model untouched when validating before model initialization' , function ( ) {
1549
+ scope . value = '12345' ;
1550
+ compileInput ( '<input type="text" name="input" ng-model="value" minlength="3" />' ) ;
1551
+ expect ( scope . value ) . toBe ( '12345' ) ;
1552
+ } ) ;
1553
+
1492
1554
} ) ;
1493
1555
1494
1556
0 commit comments