@@ -1771,6 +1771,28 @@ describe('ViewContainerRef', () => {
1771
1771
1772
1772
describe ( 'view engine compatibility' , ( ) => {
1773
1773
1774
+ @Component ( { selector : 'app' , template : '' } )
1775
+ class AppCmpt {
1776
+ static ngComponentDef = defineComponent ( {
1777
+ type : AppCmpt ,
1778
+ selectors : [ [ 'app' ] ] ,
1779
+ factory : ( ) => new AppCmpt (
1780
+ directiveInject ( ViewContainerRef as any ) , injectComponentFactoryResolver ( ) ) ,
1781
+ consts : 0 ,
1782
+ vars : 0 ,
1783
+ template : ( rf : RenderFlags , cmp : AppCmpt ) => { }
1784
+ } ) ;
1785
+
1786
+ constructor ( private _vcRef : ViewContainerRef , private _cfResolver : ComponentFactoryResolver ) {
1787
+ }
1788
+
1789
+ insert ( comp : any ) {
1790
+ this . _vcRef . createComponent ( this . _cfResolver . resolveComponentFactory ( comp ) ) ;
1791
+ }
1792
+
1793
+ clear ( ) { this . _vcRef . clear ( ) ; }
1794
+ }
1795
+
1774
1796
// https://stackblitz.com/edit/angular-xxpffd?file=src%2Findex.html
1775
1797
it ( 'should allow injecting VCRef into the root (bootstrapped) component' , ( ) => {
1776
1798
@@ -1781,39 +1803,58 @@ describe('ViewContainerRef', () => {
1781
1803
}
1782
1804
} , 1 , 0 ) ;
1783
1805
1784
- @Component ( { selector : 'app' , template : '' } )
1785
- class AppCmpt {
1786
- static ngComponentDef = defineComponent ( {
1787
- type : AppCmpt ,
1788
- selectors : [ [ 'app' ] ] ,
1789
- factory : ( ) => new AppCmpt (
1790
- directiveInject ( ViewContainerRef as any ) , injectComponentFactoryResolver ( ) ) ,
1791
- consts : 0 ,
1792
- vars : 0 ,
1793
- template : ( rf : RenderFlags , cmp : AppCmpt ) => { }
1794
- } ) ;
1795
1806
1796
- constructor (
1797
- private _vcRef : ViewContainerRef , private _cfResolver : ComponentFactoryResolver ) { }
1807
+ const fixture = new ComponentFixture ( AppCmpt ) ;
1808
+ expect ( fixture . outerHtml ) . toBe ( '<div host="mark"></div>' ) ;
1798
1809
1799
- insert ( ) {
1800
- this . _vcRef . createComponent ( this . _cfResolver . resolveComponentFactory ( DynamicComponent ) ) ;
1801
- }
1810
+ fixture . component . insert ( DynamicComponent ) ;
1811
+ fixture . update ( ) ;
1812
+ expect ( fixture . outerHtml )
1813
+ . toBe ( '<div host="mark"></div><dynamic-cmpt>inserted dynamically</dynamic-cmpt>' ) ;
1814
+
1815
+ fixture . component . clear ( ) ;
1816
+ fixture . update ( ) ;
1817
+ expect ( fixture . outerHtml ) . toBe ( '<div host="mark"></div>' ) ;
1818
+ } ) ;
1802
1819
1803
- clear ( ) { this . _vcRef . clear ( ) ; }
1820
+ it ( 'should check bindings for components dynamically created by root component' , ( ) => {
1821
+ class DynamicCompWithBindings {
1822
+ checkCount = 0 ;
1823
+
1824
+ ngDoCheck ( ) { this . checkCount ++ ; }
1825
+
1826
+ /** check count: {{ checkCount }} */
1827
+ static ngComponentDef = defineComponent ( {
1828
+ type : DynamicCompWithBindings ,
1829
+ selectors : [ [ 'dynamic-cmpt-with-bindings' ] ] ,
1830
+ factory : ( ) => new DynamicCompWithBindings ( ) ,
1831
+ consts : 1 ,
1832
+ vars : 1 ,
1833
+ template : ( rf : RenderFlags , ctx : DynamicCompWithBindings ) => {
1834
+ if ( rf & RenderFlags . Create ) {
1835
+ text ( 0 ) ;
1836
+ }
1837
+ if ( rf & RenderFlags . Update ) {
1838
+ textBinding ( 0 , interpolation1 ( 'check count: ' , ctx . checkCount , '' ) ) ;
1839
+ }
1840
+ }
1841
+ } ) ;
1804
1842
}
1805
1843
1806
1844
const fixture = new ComponentFixture ( AppCmpt ) ;
1807
1845
expect ( fixture . outerHtml ) . toBe ( '<div host="mark"></div>' ) ;
1808
1846
1809
- fixture . component . insert ( ) ;
1847
+ fixture . component . insert ( DynamicCompWithBindings ) ;
1810
1848
fixture . update ( ) ;
1811
1849
expect ( fixture . outerHtml )
1812
- . toBe ( '<div host="mark"></div><dynamic-cmpt>inserted dynamically</dynamic-cmpt>' ) ;
1850
+ . toBe (
1851
+ '<div host="mark"></div><dynamic-cmpt-with-bindings>check count: 1</dynamic-cmpt-with-bindings>' ) ;
1813
1852
1814
- fixture . component . clear ( ) ;
1815
1853
fixture . update ( ) ;
1816
- expect ( fixture . outerHtml ) . toBe ( '<div host="mark"></div>' ) ;
1854
+ expect ( fixture . outerHtml )
1855
+ . toBe (
1856
+ '<div host="mark"></div><dynamic-cmpt-with-bindings>check count: 2</dynamic-cmpt-with-bindings>' ) ;
1817
1857
} ) ;
1858
+
1818
1859
} ) ;
1819
1860
} ) ;
0 commit comments