@@ -1631,6 +1631,179 @@ describe('query logic', () => {
1631
1631
expect ( groups [ 2 ] ) . toBeUndefined ( ) ;
1632
1632
} ) ;
1633
1633
} ) ;
1634
+
1635
+ describe ( 'querying for string token providers' , ( ) => {
1636
+ @Directive ( {
1637
+ selector : '[text-token]' ,
1638
+ providers : [ { provide : 'Token' , useExisting : TextTokenDirective } ] ,
1639
+ } )
1640
+ class TextTokenDirective {
1641
+ }
1642
+
1643
+ it ( 'should match string injection token in a ViewChild query' , ( ) => {
1644
+ @Component ( { template : '<div text-token></div>' } )
1645
+ class App {
1646
+ @ViewChild ( 'Token' ) token : any ;
1647
+ }
1648
+
1649
+ TestBed . configureTestingModule ( { declarations : [ App , TextTokenDirective ] } ) ;
1650
+ const fixture = TestBed . createComponent ( App ) ;
1651
+ fixture . detectChanges ( ) ;
1652
+ expect ( fixture . componentInstance . token ) . toBeAnInstanceOf ( TextTokenDirective ) ;
1653
+ } ) ;
1654
+
1655
+ it ( 'should give precedence to local reference if both a reference and a string injection token provider match a ViewChild query' ,
1656
+ ( ) => {
1657
+ @Component ( { template : '<div text-token #Token></div>' } )
1658
+ class App {
1659
+ @ViewChild ( 'Token' ) token : any ;
1660
+ }
1661
+
1662
+ TestBed . configureTestingModule ( { declarations : [ App , TextTokenDirective ] } ) ;
1663
+ const fixture = TestBed . createComponent ( App ) ;
1664
+ fixture . detectChanges ( ) ;
1665
+ expect ( fixture . componentInstance . token ) . toBeAnInstanceOf ( ElementRef ) ;
1666
+ } ) ;
1667
+
1668
+ it ( 'should match string injection token in a ViewChildren query' , ( ) => {
1669
+ @Component ( { template : '<div text-token></div>' } )
1670
+ class App {
1671
+ @ViewChildren ( 'Token' ) tokens ! : QueryList < any > ;
1672
+ }
1673
+
1674
+ TestBed . configureTestingModule ( { declarations : [ App , TextTokenDirective ] } ) ;
1675
+ const fixture = TestBed . createComponent ( App ) ;
1676
+ fixture . detectChanges ( ) ;
1677
+
1678
+ const tokens = fixture . componentInstance . tokens ;
1679
+ expect ( tokens . length ) . toBe ( 1 ) ;
1680
+ expect ( tokens . first ) . toBeAnInstanceOf ( TextTokenDirective ) ;
1681
+ } ) ;
1682
+
1683
+ it ( 'should match both string injection token and local reference inside a ViewChildren query' ,
1684
+ ( ) => {
1685
+ @Component ( { template : '<div text-token #Token></div>' } )
1686
+ class App {
1687
+ @ViewChildren ( 'Token' ) tokens ! : QueryList < any > ;
1688
+ }
1689
+
1690
+ TestBed . configureTestingModule ( { declarations : [ App , TextTokenDirective ] } ) ;
1691
+ const fixture = TestBed . createComponent ( App ) ;
1692
+ fixture . detectChanges ( ) ;
1693
+
1694
+ expect ( fixture . componentInstance . tokens . toArray ( ) ) . toEqual ( [
1695
+ jasmine . any ( ElementRef ) , jasmine . any ( TextTokenDirective )
1696
+ ] ) ;
1697
+ } ) ;
1698
+
1699
+ it ( 'should match string injection token in a ContentChild query' , ( ) => {
1700
+ @Component ( { selector : 'has-query' , template : '<ng-content></ng-content>' } )
1701
+ class HasQuery {
1702
+ @ContentChild ( 'Token' ) token : any ;
1703
+ }
1704
+
1705
+ @Component ( { template : '<has-query><div text-token></div></has-query>' } )
1706
+ class App {
1707
+ @ViewChild ( HasQuery ) queryComp ! : HasQuery ;
1708
+ }
1709
+
1710
+ TestBed . configureTestingModule ( { declarations : [ App , HasQuery , TextTokenDirective ] } ) ;
1711
+ const fixture = TestBed . createComponent ( App ) ;
1712
+ fixture . detectChanges ( ) ;
1713
+
1714
+ expect ( fixture . componentInstance . queryComp . token ) . toBeAnInstanceOf ( TextTokenDirective ) ;
1715
+ } ) ;
1716
+
1717
+ it ( 'should give precedence to local reference if both a reference and a string injection token provider match a ContentChild query' ,
1718
+ ( ) => {
1719
+ @Component ( { selector : 'has-query' , template : '<ng-content></ng-content>' } )
1720
+ class HasQuery {
1721
+ @ContentChild ( 'Token' ) token : any ;
1722
+ }
1723
+
1724
+ @Component ( { template : '<has-query><div text-token #Token></div></has-query>' } )
1725
+ class App {
1726
+ @ViewChild ( HasQuery ) queryComp ! : HasQuery ;
1727
+ }
1728
+
1729
+ TestBed . configureTestingModule ( { declarations : [ App , HasQuery , TextTokenDirective ] } ) ;
1730
+ const fixture = TestBed . createComponent ( App ) ;
1731
+ fixture . detectChanges ( ) ;
1732
+
1733
+ expect ( fixture . componentInstance . queryComp . token ) . toBeAnInstanceOf ( ElementRef ) ;
1734
+ } ) ;
1735
+
1736
+ it ( 'should match string injection token in a ContentChildren query' , ( ) => {
1737
+ @Component ( { selector : 'has-query' , template : '<ng-content></ng-content>' } )
1738
+ class HasQuery {
1739
+ @ContentChildren ( 'Token' ) tokens ! : QueryList < any > ;
1740
+ }
1741
+
1742
+ @Component ( { template : '<has-query><div text-token></div></has-query>' } )
1743
+ class App {
1744
+ @ViewChild ( HasQuery ) queryComp ! : HasQuery ;
1745
+ }
1746
+
1747
+ TestBed . configureTestingModule ( { declarations : [ App , HasQuery , TextTokenDirective ] } ) ;
1748
+ const fixture = TestBed . createComponent ( App ) ;
1749
+ fixture . detectChanges ( ) ;
1750
+
1751
+ const tokens = fixture . componentInstance . queryComp . tokens ;
1752
+ expect ( tokens . length ) . toBe ( 1 ) ;
1753
+ expect ( tokens . first ) . toBeAnInstanceOf ( TextTokenDirective ) ;
1754
+ } ) ;
1755
+
1756
+ it ( 'should match both string injection token and local reference inside a ContentChildren query' ,
1757
+ ( ) => {
1758
+ @Component ( { selector : 'has-query' , template : '<ng-content></ng-content>' } )
1759
+ class HasQuery {
1760
+ @ContentChildren ( 'Token' ) tokens ! : QueryList < any > ;
1761
+ }
1762
+
1763
+ @Component ( { template : '<has-query><div text-token #Token></div></has-query>' } )
1764
+ class App {
1765
+ @ViewChild ( HasQuery ) queryComp ! : HasQuery ;
1766
+ }
1767
+
1768
+ TestBed . configureTestingModule ( { declarations : [ App , HasQuery , TextTokenDirective ] } ) ;
1769
+ const fixture = TestBed . createComponent ( App ) ;
1770
+ fixture . detectChanges ( ) ;
1771
+
1772
+ expect ( fixture . componentInstance . queryComp . tokens . toArray ( ) ) . toEqual ( [
1773
+ jasmine . any ( ElementRef ) , jasmine . any ( TextTokenDirective )
1774
+ ] ) ;
1775
+ } ) ;
1776
+
1777
+ it ( 'should match string token specified through the `read` option of a view query' , ( ) => {
1778
+ @Component ( { template : '<div text-token #Token></div>' } )
1779
+ class App {
1780
+ @ViewChild ( 'Token' , { read : 'Token' } ) token : any ;
1781
+ }
1782
+
1783
+ TestBed . configureTestingModule ( { declarations : [ App , TextTokenDirective ] } ) ;
1784
+ const fixture = TestBed . createComponent ( App ) ;
1785
+ fixture . detectChanges ( ) ;
1786
+ expect ( fixture . componentInstance . token ) . toBeAnInstanceOf ( TextTokenDirective ) ;
1787
+ } ) ;
1788
+
1789
+ it ( 'should match string token specified through the `read` option of a content query' , ( ) => {
1790
+ @Component ( { selector : 'has-query' , template : '<ng-content></ng-content>' } )
1791
+ class HasQuery {
1792
+ @ContentChild ( 'Token' , { read : 'Token' } ) token : any ;
1793
+ }
1794
+
1795
+ @Component ( { template : '<has-query><div text-token #Token></div></has-query>' } )
1796
+ class App {
1797
+ @ViewChild ( HasQuery ) queryComp ! : HasQuery ;
1798
+ }
1799
+
1800
+ TestBed . configureTestingModule ( { declarations : [ App , HasQuery , TextTokenDirective ] } ) ;
1801
+ const fixture = TestBed . createComponent ( App ) ;
1802
+ fixture . detectChanges ( ) ;
1803
+
1804
+ expect ( fixture . componentInstance . queryComp . token ) . toBeAnInstanceOf ( TextTokenDirective ) ;
1805
+ } ) ;
1806
+ } ) ;
1634
1807
} ) ;
1635
1808
1636
1809
function initWithTemplate ( compType : Type < any > , template : string ) {
0 commit comments