@@ -1555,23 +1555,65 @@ describe('ngOptions', function() {
15551555
15561556 expect ( element ) . toEqualSelectValue ( scope . selected ) ;
15571557
1558- var zero = jqLite ( element . find ( 'optgroup' ) [ 0 ] ) ;
1559- var b = jqLite ( zero . find ( 'option' ) [ 0 ] ) ;
1560- var e = jqLite ( zero . find ( 'option' ) [ 1 ] ) ;
1558+ var optgroups = element . find ( 'optgroup' ) ;
1559+ expect ( optgroups . length ) . toBe ( 3 ) ;
1560+
1561+ var zero = optgroups . eq ( 0 ) ;
1562+ var b = zero . find ( 'option' ) . eq ( 0 ) ;
1563+ var e = zero . find ( 'option' ) . eq ( 1 ) ;
15611564 expect ( zero . attr ( 'label' ) ) . toEqual ( '0' ) ;
15621565 expect ( b . text ( ) ) . toEqual ( 'B' ) ;
15631566 expect ( e . text ( ) ) . toEqual ( 'E' ) ;
15641567
1565- var first = jqLite ( element . find ( 'optgroup' ) [ 1 ] ) ;
1566- var c = jqLite ( first . find ( 'option' ) [ 0 ] ) ;
1567- var f = jqLite ( first . find ( 'option' ) [ 1 ] ) ;
1568+ var first = optgroups . eq ( 1 ) ;
1569+ var c = first . find ( 'option' ) . eq ( 0 ) ;
1570+ var f = first . find ( 'option' ) . eq ( 1 ) ;
1571+ expect ( first . attr ( 'label' ) ) . toEqual ( 'first' ) ;
1572+ expect ( c . text ( ) ) . toEqual ( 'C' ) ;
1573+ expect ( f . text ( ) ) . toEqual ( 'F' ) ;
1574+
1575+ var second = optgroups . eq ( 2 ) ;
1576+ var d = second . find ( 'option' ) . eq ( 0 ) ;
1577+ var g = second . find ( 'option' ) . eq ( 1 ) ;
1578+ expect ( second . attr ( 'label' ) ) . toEqual ( 'second' ) ;
1579+ expect ( d . text ( ) ) . toEqual ( 'D' ) ;
1580+ expect ( g . text ( ) ) . toEqual ( 'G' ) ;
1581+
1582+ scope . $apply ( function ( ) {
1583+ scope . selected = scope . values [ 0 ] ;
1584+ } ) ;
1585+
1586+ expect ( element ) . toEqualSelectValue ( scope . selected ) ;
1587+ } ) ;
1588+
1589+
1590+ it ( 'should group when the options are available on compile time' , function ( ) {
1591+ scope . values = [ { name : 'C' , group : 'first' } ,
1592+ { name : 'D' , group : 'second' } ,
1593+ { name : 'F' , group : 'first' } ,
1594+ { name : 'G' , group : 'second' } ] ;
1595+ scope . selected = scope . values [ 3 ] ;
1596+
1597+ createSelect ( {
1598+ 'ng-model' : 'selected' ,
1599+ 'ng-options' : 'item as item.name group by item.group for item in values'
1600+ } ) ;
1601+
1602+ expect ( element ) . toEqualSelectValue ( scope . selected ) ;
1603+
1604+ var optgroups = element . find ( 'optgroup' ) ;
1605+ expect ( optgroups . length ) . toBe ( 2 ) ;
1606+
1607+ var first = optgroups . eq ( 0 ) ;
1608+ var c = first . find ( 'option' ) . eq ( 0 ) ;
1609+ var f = first . find ( 'option' ) . eq ( 1 ) ;
15681610 expect ( first . attr ( 'label' ) ) . toEqual ( 'first' ) ;
15691611 expect ( c . text ( ) ) . toEqual ( 'C' ) ;
15701612 expect ( f . text ( ) ) . toEqual ( 'F' ) ;
15711613
1572- var second = jqLite ( element . find ( 'optgroup' ) [ 2 ] ) ;
1573- var d = jqLite ( second . find ( 'option' ) [ 0 ] ) ;
1574- var g = jqLite ( second . find ( 'option' ) [ 1 ] ) ;
1614+ var second = optgroups . eq ( 1 ) ;
1615+ var d = second . find ( 'option' ) . eq ( 0 ) ;
1616+ var g = second . find ( 'option' ) . eq ( 1 ) ;
15751617 expect ( second . attr ( 'label' ) ) . toEqual ( 'second' ) ;
15761618 expect ( d . text ( ) ) . toEqual ( 'D' ) ;
15771619 expect ( g . text ( ) ) . toEqual ( 'G' ) ;
@@ -1584,6 +1626,79 @@ describe('ngOptions', function() {
15841626 } ) ;
15851627
15861628
1629+ it ( 'should group when the options are updated' , function ( ) {
1630+ var optgroups , one , two , three , alpha , beta , gamma , delta , epsilon ;
1631+
1632+ createSelect ( {
1633+ 'ng-model' : 'selected' ,
1634+ 'ng-options' : 'i.name group by i.cls for i in list'
1635+ } ) ;
1636+
1637+ scope . list = [
1638+ { cls : 'one' , name : 'Alpha' } ,
1639+ { cls : 'one' , name : 'Beta' } ,
1640+ { cls : 'two' , name : 'Gamma' }
1641+ ] ;
1642+ scope . $digest ( ) ;
1643+
1644+ optgroups = element . find ( 'optgroup' ) ;
1645+ expect ( optgroups . length ) . toBe ( 2 ) ;
1646+
1647+ one = optgroups . eq ( 0 ) ;
1648+ expect ( one . children ( 'option' ) . length ) . toBe ( 2 ) ;
1649+
1650+ alpha = one . find ( 'option' ) . eq ( 0 ) ;
1651+ beta = one . find ( 'option' ) . eq ( 1 ) ;
1652+ expect ( one . attr ( 'label' ) ) . toEqual ( 'one' ) ;
1653+ expect ( alpha . text ( ) ) . toEqual ( 'Alpha' ) ;
1654+ expect ( beta . text ( ) ) . toEqual ( 'Beta' ) ;
1655+
1656+ two = optgroups . eq ( 1 ) ;
1657+ expect ( two . children ( 'option' ) . length ) . toBe ( 1 ) ;
1658+
1659+ gamma = two . find ( 'option' ) . eq ( 0 ) ;
1660+ expect ( two . attr ( 'label' ) ) . toEqual ( 'two' ) ;
1661+ expect ( gamma . text ( ) ) . toEqual ( 'Gamma' ) ;
1662+
1663+ // Remove item from first group, add item to second group, add new group
1664+ scope . list . shift ( ) ;
1665+ scope . list . push (
1666+ { cls : 'two' , name : 'Delta' } ,
1667+ { cls : 'three' , name : 'Epsilon' }
1668+ ) ;
1669+ scope . $digest ( ) ;
1670+
1671+ optgroups = element . find ( 'optgroup' ) ;
1672+ expect ( optgroups . length ) . toBe ( 3 ) ;
1673+
1674+ // Group with removed item
1675+ one = optgroups . eq ( 0 ) ;
1676+ expect ( one . children ( 'option' ) . length ) . toBe ( 1 ) ;
1677+
1678+ beta = one . find ( 'option' ) . eq ( 0 ) ;
1679+ expect ( one . attr ( 'label' ) ) . toEqual ( 'one' ) ;
1680+ expect ( beta . text ( ) ) . toEqual ( 'Beta' ) ;
1681+
1682+ // Group with new item
1683+ two = optgroups . eq ( 1 ) ;
1684+ expect ( two . children ( 'option' ) . length ) . toBe ( 2 ) ;
1685+
1686+ gamma = two . find ( 'option' ) . eq ( 0 ) ;
1687+ expect ( two . attr ( 'label' ) ) . toEqual ( 'two' ) ;
1688+ expect ( gamma . text ( ) ) . toEqual ( 'Gamma' ) ;
1689+ delta = two . find ( 'option' ) . eq ( 1 ) ;
1690+ expect ( two . attr ( 'label' ) ) . toEqual ( 'two' ) ;
1691+ expect ( delta . text ( ) ) . toEqual ( 'Delta' ) ;
1692+
1693+ // New group
1694+ three = optgroups . eq ( 2 ) ;
1695+ expect ( three . children ( 'option' ) . length ) . toBe ( 1 ) ;
1696+
1697+ epsilon = three . find ( 'option' ) . eq ( 0 ) ;
1698+ expect ( three . attr ( 'label' ) ) . toEqual ( 'three' ) ;
1699+ expect ( epsilon . text ( ) ) . toEqual ( 'Epsilon' ) ;
1700+ } ) ;
1701+
15871702 it ( 'should place non-grouped items in the list where they appear' , function ( ) {
15881703 createSelect ( {
15891704 'ng-model' : 'selected' ,
0 commit comments