@@ -2,6 +2,7 @@ import {inject, ComponentFixture, TestBed, async} from '@angular/core/testing';
2
2
import {
3
3
NgModule ,
4
4
Component ,
5
+ ViewChild ,
5
6
ViewChildren ,
6
7
QueryList ,
7
8
ViewContainerRef ,
@@ -10,7 +11,7 @@ import {
10
11
Injector ,
11
12
ApplicationRef ,
12
13
} from '@angular/core' ;
13
- import { TemplatePortalDirective , PortalModule } from './portal-directives' ;
14
+ import { TemplatePortalDirective , PortalHostDirective , PortalModule } from './portal-directives' ;
14
15
import { Portal , ComponentPortal } from './portal' ;
15
16
import { DomPortalHost } from './dom-portal-host' ;
16
17
@@ -141,6 +142,52 @@ describe('Portals', () => {
141
142
142
143
expect ( hostContainer . textContent ) . toContain ( 'Pizza' ) ;
143
144
} ) ;
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
+ } ) ;
144
191
} ) ;
145
192
146
193
describe ( 'DomPortalHost' , ( ) => {
@@ -331,6 +378,7 @@ class ArbitraryViewContainerRefComponent {
331
378
} )
332
379
class PortalTestApp {
333
380
@ViewChildren ( TemplatePortalDirective ) portals : QueryList < TemplatePortalDirective > ;
381
+ @ViewChild ( PortalHostDirective ) portalHost : PortalHostDirective ;
334
382
selectedPortal : Portal < any > ;
335
383
fruit : string = 'Banana' ;
336
384
0 commit comments