@@ -13,6 +13,7 @@ import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
13
13
import { downgradeComponent , UpgradeComponent , UpgradeModule } from '@angular/upgrade/static' ;
14
14
15
15
import * as angular from '../../../src/common/src/angular1' ;
16
+ import { $ROOT_SCOPE } from '../../../src/common/src/constants' ;
16
17
import { html , multiTrim , withEachNg1Version } from '../../../src/common/test/helpers/common_test_helpers' ;
17
18
18
19
import { $apply , bootstrap } from './static_test_helpers' ;
@@ -648,6 +649,66 @@ withEachNg1Version(() => {
648
649
} ) ;
649
650
} ) ) ;
650
651
652
+ it ( 'should destroy the AngularJS app when `PlatformRef` is destroyed' , waitForAsync ( ( ) => {
653
+ @Component ( { selector : 'ng2' , template : '<span>NG2</span>' } )
654
+ class Ng2Component {
655
+ }
656
+
657
+ @NgModule ( {
658
+ declarations : [ Ng2Component ] ,
659
+ entryComponents : [ Ng2Component ] ,
660
+ imports : [ BrowserModule , UpgradeModule ] ,
661
+ } )
662
+ class Ng2Module {
663
+ ngDoBootstrap ( ) { }
664
+ }
665
+
666
+ const ng1Module = angular . module_ ( 'ng1' , [ ] )
667
+ . component ( 'ng1' , { template : '<ng2></ng2>' } )
668
+ . directive ( 'ng2' , downgradeComponent ( { component : Ng2Component } ) ) ;
669
+
670
+ const element = html ( '<div><ng1></ng1></div>' ) ;
671
+ const platformRef = platformBrowserDynamic ( ) ;
672
+
673
+ platformRef . bootstrapModule ( Ng2Module ) . then ( ref => {
674
+ const upgrade = ref . injector . get ( UpgradeModule ) ;
675
+ upgrade . bootstrap ( element , [ ng1Module . name ] ) ;
676
+
677
+ const $rootScope : angular . IRootScopeService = upgrade . $injector . get ( $ROOT_SCOPE ) ;
678
+ const rootScopeDestroySpy = spyOn ( $rootScope , '$destroy' ) ;
679
+
680
+ const appElem = angular . element ( element ) ;
681
+ const ng1Elem = angular . element ( element . querySelector ( 'ng1' ) as Element ) ;
682
+ const ng2Elem = angular . element ( element . querySelector ( 'ng2' ) as Element ) ;
683
+ const ng2ChildElem = angular . element ( element . querySelector ( 'ng2 span' ) as Element ) ;
684
+
685
+ // Attach data to all elements.
686
+ appElem . data ! ( 'testData' , 1 ) ;
687
+ ng1Elem . data ! ( 'testData' , 2 ) ;
688
+ ng2Elem . data ! ( 'testData' , 3 ) ;
689
+ ng2ChildElem . data ! ( 'testData' , 4 ) ;
690
+
691
+ // Verify data can be retrieved.
692
+ expect ( appElem . data ! ( 'testData' ) ) . toBe ( 1 ) ;
693
+ expect ( ng1Elem . data ! ( 'testData' ) ) . toBe ( 2 ) ;
694
+ expect ( ng2Elem . data ! ( 'testData' ) ) . toBe ( 3 ) ;
695
+ expect ( ng2ChildElem . data ! ( 'testData' ) ) . toBe ( 4 ) ;
696
+
697
+ expect ( rootScopeDestroySpy ) . not . toHaveBeenCalled ( ) ;
698
+
699
+ // Destroy `PlatformRef`.
700
+ platformRef . destroy ( ) ;
701
+
702
+ // Verify `$rootScope` has been destroyed and data has been cleaned up.
703
+ expect ( rootScopeDestroySpy ) . toHaveBeenCalled ( ) ;
704
+
705
+ expect ( appElem . data ! ( 'testData' ) ) . toBeUndefined ( ) ;
706
+ expect ( ng1Elem . data ! ( 'testData' ) ) . toBeUndefined ( ) ;
707
+ expect ( ng2Elem . data ! ( 'testData' ) ) . toBeUndefined ( ) ;
708
+ expect ( ng2ChildElem . data ! ( 'testData' ) ) . toBeUndefined ( ) ;
709
+ } ) ;
710
+ } ) ) ;
711
+
651
712
it ( 'should work when compiled outside the dom (by fallback to the root ng2.injector)' ,
652
713
waitForAsync ( ( ) => {
653
714
@Component ( { selector : 'ng2' , template : 'test' } )
0 commit comments