|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import {ChangeDetectorRef, Compiler, Component, ComponentFactoryResolver, EventEmitter, Injector, Input, NgModule, NgModuleRef, OnChanges, OnDestroy, SimpleChanges, destroyPlatform} from '@angular/core'; |
| 9 | +import {ChangeDetectorRef, Compiler, Component, ComponentFactoryResolver, Directive, ElementRef, EventEmitter, Injector, Input, NgModule, NgModuleRef, OnChanges, OnDestroy, SimpleChanges, destroyPlatform} from '@angular/core'; |
10 | 10 | import {async, fakeAsync, tick} from '@angular/core/testing';
|
11 | 11 | import {BrowserModule} from '@angular/platform-browser';
|
12 | 12 | import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
13 |
| -import {UpgradeModule, downgradeComponent} from '@angular/upgrade/static'; |
| 13 | +import {UpgradeComponent, UpgradeModule, downgradeComponent} from '@angular/upgrade/static'; |
14 | 14 | import * as angular from '@angular/upgrade/static/src/common/angular1';
|
15 | 15 |
|
16 | 16 | import {$apply, bootstrap, html, multiTrim, withEachNg1Version} from '../test_helpers';
|
@@ -461,6 +461,57 @@ withEachNg1Version(() => {
|
461 | 461 | });
|
462 | 462 | }));
|
463 | 463 |
|
| 464 | + it('should properly run cleanup with multiple levels of nesting', async(() => { |
| 465 | + let destroyed = false; |
| 466 | + |
| 467 | + @Component({ |
| 468 | + selector: 'ng2-outer', |
| 469 | + template: '<div *ngIf="!destroyIt"><ng1></ng1></div>', |
| 470 | + }) |
| 471 | + class Ng2OuterComponent { |
| 472 | + @Input() destroyIt = false; |
| 473 | + } |
| 474 | + |
| 475 | + @Component({selector: 'ng2-inner', template: 'test'}) |
| 476 | + class Ng2InnerComponent implements OnDestroy { |
| 477 | + ngOnDestroy() { destroyed = true; } |
| 478 | + } |
| 479 | + |
| 480 | + @Directive({selector: 'ng1'}) |
| 481 | + class Ng1ComponentFacade extends UpgradeComponent { |
| 482 | + constructor(elementRef: ElementRef, injector: Injector) { |
| 483 | + super('ng1', elementRef, injector); |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + @NgModule({ |
| 488 | + imports: [BrowserModule, UpgradeModule], |
| 489 | + declarations: [Ng1ComponentFacade, Ng2InnerComponent, Ng2OuterComponent], |
| 490 | + entryComponents: [Ng2InnerComponent, Ng2OuterComponent], |
| 491 | + }) |
| 492 | + class Ng2Module { |
| 493 | + ngDoBootstrap() {} |
| 494 | + } |
| 495 | + |
| 496 | + const ng1Module = |
| 497 | + angular.module('ng1', []) |
| 498 | + .directive('ng1', () => ({template: '<ng2-inner></ng2-inner>'})) |
| 499 | + .directive('ng2Inner', downgradeComponent({component: Ng2InnerComponent})) |
| 500 | + .directive('ng2Outer', downgradeComponent({component: Ng2OuterComponent})); |
| 501 | + |
| 502 | + const element = html('<ng2-outer [destroy-it]="destroyIt"></ng2-outer>'); |
| 503 | + |
| 504 | + bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then(upgrade => { |
| 505 | + expect(element.textContent).toBe('test'); |
| 506 | + expect(destroyed).toBe(false); |
| 507 | + |
| 508 | + $apply(upgrade, 'destroyIt = true'); |
| 509 | + |
| 510 | + expect(element.textContent).toBe(''); |
| 511 | + expect(destroyed).toBe(true); |
| 512 | + }); |
| 513 | + })); |
| 514 | + |
464 | 515 | it('should work when compiled outside the dom (by fallback to the root ng2.injector)',
|
465 | 516 | async(() => {
|
466 | 517 |
|
|
0 commit comments