11'use strict' ;
22
3- describe ( 'ngOptions' , function ( ) {
3+ ddescribe ( 'ngOptions' , function ( ) {
44
55 var scope , formElement , element , $compile , linkLog ;
66
@@ -1553,16 +1553,19 @@ describe('ngOptions', function() {
15531553
15541554 expect ( element ) . toEqualSelectValue ( scope . selected ) ;
15551555
1556- var first = jqLite ( element . find ( 'optgroup' ) [ 0 ] ) ;
1557- var b = jqLite ( first . find ( 'option' ) [ 0 ] ) ;
1558- var d = jqLite ( first . find ( 'option' ) [ 1 ] ) ;
1556+ var optgroups = element . find ( 'optgroup' ) ;
1557+ expect ( optgroups . length ) . toBe ( 2 ) ;
1558+
1559+ var first = optgroups . eq ( 0 ) ;
1560+ var b = first . find ( 'option' ) . eq ( 0 ) ;
1561+ var d = first . find ( 'option' ) . eq ( 1 ) ;
15591562 expect ( first . attr ( 'label' ) ) . toEqual ( 'first' ) ;
15601563 expect ( b . text ( ) ) . toEqual ( 'B' ) ;
15611564 expect ( d . text ( ) ) . toEqual ( 'D' ) ;
15621565
1563- var second = jqLite ( element . find ( 'optgroup' ) [ 1 ] ) ;
1564- var c = jqLite ( second . find ( 'option' ) [ 0 ] ) ;
1565- var e = jqLite ( second . find ( 'option' ) [ 1 ] ) ;
1566+ var second = optgroups . eq ( 1 ) ;
1567+ var c = second . find ( 'option' ) . eq ( 0 ) ;
1568+ var e = second . find ( 'option' ) . eq ( 1 ) ;
15661569 expect ( second . attr ( 'label' ) ) . toEqual ( 'second' ) ;
15671570 expect ( c . text ( ) ) . toEqual ( 'C' ) ;
15681571 expect ( e . text ( ) ) . toEqual ( 'E' ) ;
@@ -1575,6 +1578,118 @@ describe('ngOptions', function() {
15751578 } ) ;
15761579
15771580
1581+ it ( 'should group when the options are available on compile time' , function ( ) {
1582+ scope . values = [ { name : 'C' , group : 'first' } ,
1583+ { name : 'D' , group : 'second' } ,
1584+ { name : 'F' , group : 'first' } ,
1585+ { name : 'G' , group : 'second' } ] ;
1586+ scope . selected = scope . values [ 3 ] ;
1587+
1588+ createSelect ( {
1589+ 'ng-model' : 'selected' ,
1590+ 'ng-options' : 'item as item.name group by item.group for item in values'
1591+ } ) ;
1592+
1593+ expect ( element ) . toEqualSelectValue ( scope . selected ) ;
1594+
1595+ var optgroups = element . find ( 'optgroup' ) ;
1596+ expect ( optgroups . length ) . toBe ( 2 ) ;
1597+
1598+ var first = optgroups . eq ( 0 ) ;
1599+ var c = first . find ( 'option' ) . eq ( 0 ) ;
1600+ var f = first . find ( 'option' ) . eq ( 1 ) ;
1601+ expect ( first . attr ( 'label' ) ) . toEqual ( 'first' ) ;
1602+ expect ( c . text ( ) ) . toEqual ( 'C' ) ;
1603+ expect ( f . text ( ) ) . toEqual ( 'F' ) ;
1604+
1605+ var second = optgroups . eq ( 1 ) ;
1606+ var d = second . find ( 'option' ) . eq ( 0 ) ;
1607+ var g = second . find ( 'option' ) . eq ( 1 ) ;
1608+ expect ( second . attr ( 'label' ) ) . toEqual ( 'second' ) ;
1609+ expect ( d . text ( ) ) . toEqual ( 'D' ) ;
1610+ expect ( g . text ( ) ) . toEqual ( 'G' ) ;
1611+
1612+ scope . $apply ( function ( ) {
1613+ scope . selected = scope . values [ 0 ] ;
1614+ } ) ;
1615+
1616+ expect ( element ) . toEqualSelectValue ( scope . selected ) ;
1617+ } ) ;
1618+
1619+
1620+ it ( 'should group when the options are updated' , function ( ) {
1621+ var optgroups , one , two , three , alpha , beta , gamma , delta , epsilon ;
1622+
1623+ createSelect ( {
1624+ 'ng-model' : 'selected' ,
1625+ 'ng-options' : 'i.name group by i.cls for i in list'
1626+ } ) ;
1627+
1628+ scope . list = [
1629+ { cls : 'one' , name : 'Alpha' } ,
1630+ { cls : 'one' , name : 'Beta' } ,
1631+ { cls : 'two' , name : 'Gamma' }
1632+ ] ;
1633+ scope . $digest ( ) ;
1634+
1635+ optgroups = element . find ( 'optgroup' ) ;
1636+ expect ( optgroups . length ) . toBe ( 2 ) ;
1637+
1638+ one = optgroups . eq ( 0 ) ;
1639+ expect ( one . children ( 'option' ) . length ) . toBe ( 2 ) ;
1640+
1641+ alpha = one . find ( 'option' ) . eq ( 0 ) ;
1642+ beta = one . find ( 'option' ) . eq ( 1 ) ;
1643+ expect ( one . attr ( 'label' ) ) . toEqual ( 'one' ) ;
1644+ expect ( alpha . text ( ) ) . toEqual ( 'Alpha' ) ;
1645+ expect ( beta . text ( ) ) . toEqual ( 'Beta' ) ;
1646+
1647+ two = optgroups . eq ( 1 ) ;
1648+ expect ( two . children ( 'option' ) . length ) . toBe ( 1 ) ;
1649+
1650+ gamma = two . find ( 'option' ) . eq ( 0 ) ;
1651+ expect ( two . attr ( 'label' ) ) . toEqual ( 'two' ) ;
1652+ expect ( gamma . text ( ) ) . toEqual ( 'Gamma' ) ;
1653+
1654+ // Remove item from first group, add item to second group, add new group
1655+ scope . list . shift ( ) ;
1656+ scope . list . push (
1657+ { cls : 'two' , name : 'Delta' } ,
1658+ { cls : 'three' , name : 'Epsilon' }
1659+ ) ;
1660+ scope . $digest ( ) ;
1661+
1662+ optgroups = element . find ( 'optgroup' ) ;
1663+ expect ( optgroups . length ) . toBe ( 3 ) ;
1664+
1665+ // Group with removed item
1666+ one = optgroups . eq ( 0 ) ;
1667+ expect ( one . children ( 'option' ) . length ) . toBe ( 1 ) ;
1668+
1669+ beta = one . find ( 'option' ) . eq ( 0 ) ;
1670+ expect ( one . attr ( 'label' ) ) . toEqual ( 'one' ) ;
1671+ expect ( beta . text ( ) ) . toEqual ( 'Beta' ) ;
1672+
1673+ // Group with new item
1674+ two = optgroups . eq ( 1 ) ;
1675+ expect ( two . children ( 'option' ) . length ) . toBe ( 2 ) ;
1676+
1677+ gamma = two . find ( 'option' ) . eq ( 0 ) ;
1678+ expect ( two . attr ( 'label' ) ) . toEqual ( 'two' ) ;
1679+ expect ( gamma . text ( ) ) . toEqual ( 'Gamma' ) ;
1680+ delta = two . find ( 'option' ) . eq ( 1 ) ;
1681+ expect ( two . attr ( 'label' ) ) . toEqual ( 'two' ) ;
1682+ expect ( delta . text ( ) ) . toEqual ( 'Delta' ) ;
1683+
1684+ // New group
1685+ three = optgroups . eq ( 2 ) ;
1686+ expect ( three . children ( 'option' ) . length ) . toBe ( 1 ) ;
1687+
1688+ epsilon = three . find ( 'option' ) . eq ( 0 ) ;
1689+ expect ( three . attr ( 'label' ) ) . toEqual ( 'three' ) ;
1690+ expect ( epsilon . text ( ) ) . toEqual ( 'Epsilon' ) ;
1691+ } ) ;
1692+
15781693 it ( 'should place non-grouped items in the list where they appear' , function ( ) {
15791694 createSelect ( {
15801695 'ng-model' : 'selected' ,
0 commit comments