@@ -17,7 +17,7 @@ import {
1717
1818import { TestBed , ViewProxy } from 'angular2/src/test_lib/test_bed' ;
1919import { Injector } from 'angular2/di' ;
20- import { Component , View } from 'angular2/annotations' ;
20+ import { Component , View , onDestroy } from 'angular2/annotations' ;
2121import * as viewAnn from 'angular2/src/core/annotations_impl/view' ;
2222import { DynamicComponentLoader } from 'angular2/src/core/compiler/dynamic_component_loader' ;
2323import { ElementRef } from 'angular2/src/core/compiler/element_ref' ;
@@ -68,6 +68,28 @@ export function main() {
6868 } ) ;
6969 } ) ) ;
7070
71+ it ( 'should allow destroying dynamically-loaded components' ,
72+ inject ( [ TestBed , AsyncTestCompleter ] , ( tb , async ) => {
73+ tb . overrideView ( MyComp , new viewAnn . View ( {
74+ template : '<dynamic-comp #dynamic></dynamic-comp>' ,
75+ directives : [ DynamicComp ]
76+ } ) ) ;
77+
78+ tb . createView ( MyComp ) . then ( ( view ) => {
79+ var dynamicComponent = view . rawView . locals . get ( "dynamic" ) ;
80+ dynamicComponent . done . then ( ( ref ) => {
81+ view . detectChanges ( ) ;
82+ expect ( view . rootNodes ) . toHaveText ( "hello" ) ;
83+
84+ ref . dispose ( ) ;
85+
86+ expect ( ref . instance . destroyed ) . toBe ( true ) ;
87+ expect ( view . rootNodes ) . toHaveText ( "" ) ;
88+ async . done ( ) ;
89+ } ) ;
90+ } ) ;
91+ } ) ) ;
92+
7193 it ( 'should allow to destroy and create them via viewcontainer directives' ,
7294 ijTestBed ( ( tb : TestBed , async ) => {
7395 tb . overrideView ( MyComp , new viewAnn . View ( {
@@ -287,16 +309,23 @@ class DynamicComp {
287309 }
288310}
289311
290- @Component ( { selector : 'hello-cmp' , appInjector : [ DynamicallyCreatedComponentService ] } )
312+ @Component ( {
313+ selector : 'hello-cmp' ,
314+ appInjector : [ DynamicallyCreatedComponentService ] ,
315+ lifecycle : [ onDestroy ]
316+ } )
291317@View ( { template : "{{greeting}}" } )
292318class DynamicallyCreatedCmp {
293319 greeting : string ;
294320 dynamicallyCreatedComponentService : DynamicallyCreatedComponentService ;
321+ destroyed : boolean = false ;
295322
296323 constructor ( a : DynamicallyCreatedComponentService ) {
297324 this . greeting = "hello" ;
298325 this . dynamicallyCreatedComponentService = a ;
299326 }
327+
328+ onDestroy ( ) { this . destroyed = true ; }
300329}
301330
302331@Component ( { selector : 'dummy' } )
0 commit comments