@@ -706,29 +706,44 @@ describe('view destruction', () => {
706
706
} ) ;
707
707
708
708
it ( 'should invoke onDestroy only once when a component is registered as a provider' , ( ) => {
709
- const testToken = new InjectionToken < ComponentWithOnDestroy > ( 'testToken' ) ;
709
+ const testToken = new InjectionToken < ParentWithOnDestroy > ( 'testToken' ) ;
710
710
let destroyCalls = 0 ;
711
711
712
- class ComponentWithOnDestroy {
712
+ class ParentWithOnDestroy {
713
713
static ngComponentDef = defineComponent ( {
714
714
selectors : [ [ 'comp-with-on-destroy' ] ] ,
715
- type : ComponentWithOnDestroy ,
715
+ type : ParentWithOnDestroy ,
716
716
consts : 0 ,
717
717
vars : 0 ,
718
- factory : ( ) => new ComponentWithOnDestroy ( ) ,
718
+ factory : ( ) => new ParentWithOnDestroy ( ) ,
719
719
template : ( rf : any , ctx : any ) => { } ,
720
- features : [ ProvidersFeature ( [ { provide : testToken , useExisting : ComponentWithOnDestroy } ] ) ]
720
+ features : [ ProvidersFeature ( [ { provide : testToken , useExisting : ParentWithOnDestroy } ] ) ]
721
721
} ) ;
722
722
723
723
ngOnDestroy ( ) { destroyCalls ++ ; }
724
724
}
725
725
726
+ class ChildComponent {
727
+ static ngComponentDef = defineComponent ( {
728
+ selectors : [ [ 'child' ] ] ,
729
+ type : ChildComponent ,
730
+ consts : 0 ,
731
+ vars : 0 ,
732
+ factory : ( ) => new ChildComponent ( directiv
B33E
eInject ( testToken ) ) ,
733
+ template : ( rf : any , ctx : any ) => { } ,
734
+ } ) ;
735
+
736
+ // We need to inject the parent so the provider is instantiated.
737
+ constructor ( _parent : ParentWithOnDestroy ) { }
738
+ }
739
+
726
740
const FixtureComponent = createComponent ( 'test-app' , ( rf : RenderFlags , ctx : any ) => {
727
741
if ( rf & RenderFlags . Create ) {
728
- element ( 0 , 'comp-with-on-destroy' ) ;
729
- directiveInject ( testToken ) ; // Inject the token so the provider gets instantiated.
742
+ elementStart ( 0 , 'comp-with-on-destroy' ) ;
743
+ element ( 1 , 'child' ) ;
744
+ elementEnd ( ) ;
730
745
}
731
- } , 1 , 0 , [ ComponentWithOnDestroy ] , [ ] , null , [ ] , [ ] ) ;
746
+ } , 2 , 0 , [ ParentWithOnDestroy , ChildComponent ] , [ ] , null , [ ] , [ ] ) ;
732
747
733
748
const fixture = new ComponentFixture ( FixtureComponent ) ;
734
749
fixture . update ( ) ;
0 commit comments