@@ -1550,6 +1550,130 @@ describe('select', function() {
1550
1550
expect ( scope . value ) . toBe ( false ) ;
1551
1551
} ) ;
1552
1552
} ) ;
1553
+
1554
+ describe ( 'ngModelCtrl' , function ( ) {
1555
+ it ( 'should prefix the model value with the word "the" using $parsers' , function ( ) {
1556
+ createSelect ( {
1557
+ 'name' : 'select' ,
1558
+ 'ng-model' : 'value' ,
1559
+ 'ng-options' : 'item for item in [\'first\', \'second\', \'third\', \'fourth\']' ,
1560
+ } ) ;
1561
+
1562
+ scope . form . select . $parsers . push ( function ( value ) {
1563
+ return 'the ' + value ;
1564
+ } ) ;
1565
+
1566
+ element . val ( '2' ) ;
1567
+ browserTrigger ( element , 'change' ) ;
1568
+ expect ( scope . value ) . toBe ( 'the third' ) ;
1569
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1570
+ } ) ;
1571
+
1572
+ it ( 'should prefix the view value with the word "the" using $formatters' , function ( ) {
1573
+ createSelect ( {
1574
+ 'name' : 'select' ,
1575
+ 'ng-model' : 'value' ,
1576
+ 'ng-options' : 'item for item in [\'the first\', \'the second\', \'the third\', \'the fourth\']' ,
1577
+ } ) ;
1578
+
1579
+ scope . form . select . $formatters . push ( function ( value ) {
1580
+ return 'the ' + value ;
1581
+ } ) ;
1582
+
1583
+ scope . $apply ( function ( ) {
1584
+ scope . value = 'third' ;
1585
+ } ) ;
1586
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1587
+ } ) ;
1588
+
1589
+ it ( 'should fail validation when $validators fail' , function ( ) {
1590
+ createSelect ( {
1591
+ 'name' : 'select' ,
1592
+ 'ng-model' : 'value' ,
1593
+ 'ng-options' : 'item for item in [\'first\', \'second\', \'third\', \'fourth\']' ,
1594
+ } ) ;
1595
+
1596
+ scope . form . select . $validators . fail = function ( ) {
1597
+ return false ;
1598
+ } ;
1599
+
1600
+ element . val ( '2' ) ;
1601
+ browserTrigger ( element , 'change' ) ;
1602
+ expect ( element ) . toBeInvalid ( ) ;
1603
+ expect ( scope . value ) . toBeUndefined ( ) ;
1604
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1605
+ } ) ;
1606
+
1607
+ it ( 'should pass validation when $validators pass' , function ( ) {
1608
+ createSelect ( {
1609
+ 'name' : 'select' ,
1610
+ 'ng-model' : 'value' ,
1611
+ 'ng-options' : 'item for item in [\'first\', \'second\', \'third\', \'fourth\']' ,
1612
+ } ) ;
1613
+
1614
+ scope . form . select . $validators . pass = function ( ) {
1615
+ return true ;
1616
+ } ;
1617
+
1618
+ element . val ( '2' ) ;
1619
+ browserTrigger ( element , 'change' ) ;
1620
+ expect ( element ) . toBeValid ( ) ;
1621
+ expect ( scope . value ) . toBe ( 'third' ) ;
1622
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1623
+ } ) ;
1624
+
1625
+ it ( 'should fail validation when $asyncValidators fail' , inject ( function ( $q , $rootScope ) {
1626
+ var defer ;
1627
+ createSelect ( {
1628
+ 'name' : 'select' ,
1629
+ 'ng-model' : 'value' ,
1630
+ 'ng-options' : 'item for item in [\'first\', \'second\', \'third\', \'fourth\']' ,
1631
+ } ) ;
1632
+
1633
+ scope . form . select . $asyncValidators . async = function ( ) {
1634
+ defer = $q . defer ( ) ;
1635
+ return defer . promise ;
1636
+ } ;
1637
+
1638
+ element . val ( '2' ) ;
1639
+ browserTrigger ( element , 'change' ) ;
1640
+ expect ( scope . form . select . $pending ) . toBeDefined ( ) ;
1641
+ expect ( scope . value ) . toBeUndefined ( ) ;
1642
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1643
+
1644
+ defer . reject ( ) ;
1645
+ $rootScope . $digest ( ) ;
1646
+ expect ( scope . form . select . $pending ) . toBeUndefined ( ) ;
1647
+ expect ( scope . value ) . toBeUndefined ( ) ;
1648
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1649
+ } ) ) ;
1650
+
1651
+ it ( 'should pass validation when $asyncValidators pass' , inject ( function ( $q , $rootScope ) {
1652
+ var defer ;
1653
+ createSelect ( {
1654
+ 'name' : 'select' ,
1655
+ 'ng-model' : 'value' ,
1656
+ 'ng-options' : 'item for item in [\'first\', \'second\', \'third\', \'fourth\']' ,
1657
+ } ) ;
1658
+
1659
+ scope . form . select . $asyncValidators . async = function ( ) {
1660
+ defer = $q . defer ( ) ;
1661
+ return defer . promise ;
1662
+ } ;
1663
+
1664
+ element . val ( '2' ) ;
1665
+ browserTrigger ( element , 'change' ) ;
1666
+ expect ( scope . form . select . $pending ) . toBeDefined ( ) ;
1667
+ expect ( scope . value ) . toBeUndefined ( ) ;
1668
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1669
+
1670
+ defer . resolve ( ) ;
1671
+ $rootScope . $digest ( ) ;
1672
+ expect ( scope . form . select . $pending ) . toBeUndefined ( ) ;
1673
+ expect ( scope . value ) . toBe ( 'third' ) ;
1674
+ expect ( element . val ( ) ) . toBe ( '2' ) ;
1675
+ } ) ) ;
1676
+ } ) ;
1553
1677
} ) ;
1554
1678
1555
1679
0 commit comments