@@ -1507,6 +1507,174 @@ describe('ngMock', function() {
1507
1507
expect ( $rootElement . text ( ) ) . toEqual ( '' ) ;
1508
1508
} ) ) ;
1509
1509
} ) ;
1510
+
1511
+
1512
+ describe ( '$rootScopeDecorator' , function ( ) {
1513
+
1514
+ describe ( '$countChildScopes' , function ( ) {
1515
+
1516
+ it ( 'should return 0 when no child scopes' , inject ( function ( $rootScope ) {
1517
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 0 ) ;
1518
+
1519
+ var childScope = $rootScope . $new ( ) ;
1520
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 1 ) ;
1521
+ expect ( childScope . $countChildScopes ( ) ) . toBe ( 0 ) ;
1522
+
1523
+ var grandChildScope = childScope . $new ( ) ;
1524
+ expect ( childScope . $countChildScopes ( ) ) . toBe ( 1 ) ;
1525
+ expect ( grandChildScope . $countChildScopes ( ) ) . toBe ( 0 ) ;
1526
+ } ) ) ;
1527
+
1528
+
1529
+ it ( 'should correctly navigate complex scope tree' , inject ( function ( $rootScope ) {
1530
+ var child ;
1531
+
1532
+ $rootScope . $new ( ) ;
1533
+ $rootScope . $new ( ) . $new ( ) . $new ( ) ;
1534
+ child = $rootScope . $new ( ) . $new ( ) ;
1535
+ child . $new ( ) ;
1536
+ child . $new ( ) ;
1537
+ child . $new ( ) . $new ( ) . $new ( ) ;
1538
+
1539
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 11 ) ;
1540
+ } ) ) ;
1541
+
1542
+
1543
+ it ( 'should provide the current count even after child destructions' , inject ( function ( $rootScope ) {
1544
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 0 ) ;
1545
+
1546
+ var childScope1 = $rootScope . $new ( ) ;
1547
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 1 ) ;
1548
+
1549
+ var childScope2 = $rootScope . $new ( ) ;
1550
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 2 ) ;
1551
+
1552
+ childScope1 . $destroy ( ) ;
1553
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 1 ) ;
1554
+
1555
+ childScope2 . $destroy ( ) ;
1556
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 0 ) ;
1557
+ } ) ) ;
1558
+
1559
+
1560
+ it ( 'should work with isolate scopes' , inject ( function ( $rootScope ) {
1561
+ /*
1562
+ RS
1563
+ |
1564
+ CIS
1565
+ / \
1566
+ GCS GCIS
1567
+ */
1568
+
1569
+ var childIsolateScope = $rootScope . $new ( true ) ;
1570
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 1 ) ;
1571
+
1572
+ var grandChildScope = childIsolateScope . $new ( ) ;
1573
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 2 ) ;
1574
+ expect ( childIsolateScope . $countChildScopes ( ) ) . toBe ( 1 ) ;
1575
+
1576
+ var grandChildIsolateScope = childIsolateScope . $new ( true ) ;
1577
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 3 ) ;
1578
+ expect ( childIsolateScope . $countChildScopes ( ) ) . toBe ( 2 ) ;
1579
+
1580
+ childIsolateScope . $destroy ( ) ;
1581
+ expect ( $rootScope . $countChildScopes ( ) ) . toBe ( 0 ) ;
1582
+ } ) ) ;
1583
+ } ) ;
1584
+
1585
+
1586
+ describe ( '$countWatchers' , function ( ) {
1587
+
1588
+ it ( 'should return the sum of watchers for the current scope and all of its children' , inject (
1589
+ function ( $rootScope ) {
1590
+
1591
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 0 ) ;
1592
+
1593
+ var childScope = $rootScope . $new ( ) ;
1594
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 0 ) ;
1595
+
1596
+ childScope . $watch ( 'foo' ) ;
1597
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1598
+ expect ( childScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1599
+
1600
+ $rootScope . $watch ( 'bar' ) ;
1601
+ childScope . $watch ( 'baz' ) ;
1602
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 3 ) ;
1603
+ expect ( childScope . $countWatchers ( ) ) . toBe ( 2 ) ;
1604
+ } ) ) ;
1605
+
1606
+
1607
+ it ( 'should correctly navigate complex scope tree' , inject ( function ( $rootScope ) {
1608
+ var child ;
1609
+
1610
+ $rootScope . $watch ( 'foo1' ) ;
1611
+
1612
+ $rootScope . $new ( ) ;
1613
+ $rootScope . $new ( ) . $new ( ) . $new ( ) ;
1614
+
1615
+ child = $rootScope . $new ( ) . $new ( ) ;
1616
+ child . $watch ( 'foo2' ) ;
1617
+ child . $new ( ) ;
1618
+ child . $new ( ) ;
1619
+ child = child . $new ( ) . $new ( ) . $new ( ) ;
1620
+ child . $watch ( 'foo3' ) ;
1621
+ child . $watch ( 'foo4' ) ;
1622
+
1623
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 4 ) ;
1624
+ } ) ) ;
1625
+
1626
+
1627
+ it ( 'should provide the current count even after child destruction and watch deregistration' ,
1628
+ inject ( function ( $rootScope ) {
1629
+
1630
+ var deregisterWatch1 = $rootScope . $watch ( 'exp1' ) ;
1631
+
1632
+ var childScope = $rootScope . $new ( ) ;
1633
+ childScope . $watch ( 'exp2' ) ;
1634
+
1635
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 2 ) ;
1636
+
1637
+ childScope . $destroy ( ) ;
1638
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1639
+
1640
+ deregisterWatch1 ( ) ;
1641
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 0 ) ;
1642
+ } ) ) ;
1643
+
1644
+
1645
+ it ( 'should work with isolate scopes' , inject ( function ( $rootScope ) {
1646
+ /*
1647
+ RS=1
1648
+ |
1649
+ CIS=1
1650
+ / \
1651
+ GCS=1 GCIS=1
1652
+ */
1653
+
1654
+ $rootScope . $watch ( 'exp1' ) ;
1655
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1656
+
1657
+ var childIsolateScope = $rootScope . $new ( true ) ;
1658
+ childIsolateScope . $watch ( 'exp2' ) ;
1659
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 2 ) ;
1660
+ expect ( childIsolateScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1661
+
1662
+ var grandChildScope = childIsolateScope . $new ( ) ;
1663
+ grandChildScope . $watch ( 'exp3' ) ;
1664
+
1665
+ var grandChildIsolateScope = childIsolateScope . $new ( true ) ;
1666
+ grandChildIsolateScope . $watch ( 'exp4' ) ;
1667
+
1668
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 4 ) ;
1669
+ expect ( childIsolateScope . $countWatchers ( ) ) . toBe ( 3 ) ;
1670
+ expect ( grandChildScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1671
+ expect ( grandChildIsolateScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1672
+
1673
+ childIsolateScope . $destroy ( ) ;
1674
+ expect ( $rootScope . $countWatchers ( ) ) . toBe ( 1 ) ;
1675
+ } ) ) ;
1676
+ } ) ;
1677
+ } ) ;
1510
1678
} ) ;
1511
1679
1512
1680
0 commit comments