@@ -2,6 +2,7 @@ import {inject, ComponentFixture, TestBed, async} from '@angular/core/testing';
22import {
33 NgModule ,
44 Component ,
5+ ViewChild ,
56 ViewChildren ,
67 QueryList ,
78 ViewContainerRef ,
@@ -10,7 +11,7 @@ import {
1011 Injector ,
1112 ApplicationRef ,
1213} from '@angular/core' ;
13- import { TemplatePortalDirective , PortalModule } from './portal-directives' ;
14+ import { TemplatePortalDirective , PortalHostDirective , PortalModule } from './portal-directives' ;
1415import { Portal , ComponentPortal } from './portal' ;
1516import { DomPortalHost } from './dom-portal-host' ;
1617
@@ -141,6 +142,52 @@ describe('Portals', () => {
141142
142143 expect ( hostContainer . textContent ) . toContain ( 'Pizza' ) ;
143144 } ) ;
145+
146+ it ( 'should detach the portal when it is set to null' , ( ) => {
147+ let testAppComponent = fixture . debugElement . componentInstance ;
148+ testAppComponent . selectedPortal = new ComponentPortal ( PizzaMsg ) ;
149+
150+ fixture . detectChanges ( ) ;
151+ expect ( testAppComponent . portalHost . hasAttached ( ) ) . toBe ( true ) ;
152+ expect ( testAppComponent . portalHost . portal ) . toBe ( testAppComponent . selectedPortal ) ;
153+
154+ testAppComponent . selectedPortal = null ;
155+ fixture . detectChanges ( ) ;
156+
157+ expect ( testAppComponent . portalHost . hasAttached ( ) ) . toBe ( false ) ;
158+ expect ( testAppComponent . portalHost . portal ) . toBeNull ( ) ;
159+ } ) ;
160+
161+ it ( 'should set the `portal` when attaching a component portal programmatically' , ( ) => {
162+ let testAppComponent = fixture . debugElement . componentInstance ;
163+ let portal = new ComponentPortal ( PizzaMsg ) ;
164+
165+ testAppComponent . portalHost . attachComponentPortal ( portal ) ;
166+
167+ expect ( testAppComponent . portalHost . portal ) . toBe ( portal ) ;
168+ } ) ;
169+
170+ it ( 'should set the `portal` when attaching a template portal programmatically' , ( ) => {
171+ let testAppComponent = fixture . debugElement . componentInstance ;
172+ fixture . detectChanges ( ) ;
173+
174+ testAppComponent . portalHost . attachTemplatePortal ( testAppComponent . cakePortal ) ;
175+
176+ expect ( testAppComponent . portalHost . portal ) . toBe ( testAppComponent . cakePortal ) ;
177+ } ) ;
178+
179+ it ( 'should clear the portal reference on destroy' , ( ) => {
180+ let testAppComponent = fixture . debugElement . componentInstance ;
181+
182+ testAppComponent . selectedPortal = new ComponentPortal ( PizzaMsg ) ;
183+ fixture . detectChanges ( ) ;
184+
185+ expect ( testAppComponent . portalHost . portal ) . toBeTruthy ( ) ;
186+
187+ fixture . destroy ( ) ;
188+
189+ expect ( testAppComponent . portalHost . portal ) . toBeNull ( ) ;
190+ } ) ;
144191 } ) ;
145192
146193 describe ( 'DomPortalHost' , ( ) => {
@@ -331,6 +378,7 @@ class ArbitraryViewContainerRefComponent {
331378} )
332379class PortalTestApp {
333380 @ViewChildren ( TemplatePortalDirective ) portals : QueryList < TemplatePortalDirective > ;
381+ @ViewChild ( PortalHostDirective ) portalHost : PortalHostDirective ;
334382 selectedPortal : Portal < any > ;
335383 fruit : string = 'Banana' ;
336384
0 commit comments