1
- import {
2
- inject ,
3
- fakeAsync ,
4
- flushMicrotasks ,
5
- TestComponentBuilder ,
6
- TestBed ,
7
- async ,
8
- } from '@angular/core/testing' ;
9
- import { Component , ViewChild , ViewContainerRef } from '@angular/core' ;
1
+ import { inject , fakeAsync , flushMicrotasks , TestBed , async } from '@angular/core/testing' ;
2
+ import { NgModule , Component , ViewChild , ViewContainerRef } from '@angular/core' ;
10
3
import { TemplatePortalDirective , PortalModule } from '../portal/portal-directives' ;
11
4
import { TemplatePortal , ComponentPortal } from '../portal/portal' ;
12
5
import { Overlay } from './overlay' ;
@@ -18,36 +11,32 @@ import {OverlayModule} from './overlay-directives';
18
11
19
12
20
13
describe ( 'Overlay' , ( ) => {
21
- let builder : TestComponentBuilder ;
22
14
let overlay : Overlay ;
23
15
let componentPortal : ComponentPortal < PizzaMsg > ;
24
16
let templatePortal : TemplatePortal ;
25
17
let overlayContainerElement : HTMLElement ;
26
18
27
19
beforeEach ( async ( ( ) => {
28
20
TestBed . configureTestingModule ( {
29
- imports : [ OverlayModule , PortalModule ] ,
30
- declarations : [ TestComponentWithTemplatePortals , PizzaMsg ] ,
21
+ imports : [ OverlayModule , PortalModule , OverlayTestModule ] ,
31
22
providers : [
32
23
{ provide : OverlayContainer , useFactory : ( ) => {
33
24
overlayContainerElement = document . createElement ( 'div' ) ;
34
25
return { getContainerElement : ( ) => overlayContainerElement } ;
35
26
} }
36
27
]
37
28
} ) ;
38
- } ) ) ;
39
29
30
+ TestBed . compileComponents ( ) ;
31
+ } ) ) ;
40
32
41
- let deps = [ TestComponentBuilder , Overlay ] ;
42
- beforeEach ( fakeAsync ( inject ( deps , ( tcb : TestComponentBuilder , o : Overlay ) => {
43
- builder = tcb ;
33
+ beforeEach ( fakeAsync ( inject ( [ Overlay ] , ( o : Overlay ) => {
44
34
overlay = o ;
45
35
46
- builder . createAsync ( TestComponentWithTemplatePortals ) . then ( fixture => {
47
- fixture . detectChanges ( ) ;
48
- templatePortal = fixture . componentInstance . templatePortal ;
49
- componentPortal = new ComponentPortal ( PizzaMsg , fixture . componentInstance . viewContainerRef ) ;
50
- } ) ;
36
+ let fixture = TestBed . createComponent ( TestComponentWithTemplatePortals ) ;
37
+ fixture . detectChanges ( ) ;
38
+ templatePortal = fixture . componentInstance . templatePortal ;
39
+ componentPortal = new ComponentPortal ( PizzaMsg , fixture . componentInstance . viewContainerRef ) ;
51
40
52
41
flushMicrotasks ( ) ;
53
42
} ) ) ) ;
@@ -140,24 +129,29 @@ describe('Overlay', () => {
140
129
141
130
142
131
/** Simple component for testing ComponentPortal. */
143
- @Component ( {
144
- selector : 'pizza-msg' ,
145
- template : '<p>Pizza</p>' ,
146
- } )
132
+ @Component ( { template : '<p>Pizza</p>' } )
147
133
class PizzaMsg { }
148
134
149
135
150
136
/** Test-bed component that contains a TempatePortal and an ElementRef. */
151
- @Component ( {
152
- selector : 'portal-test' ,
153
- template : `<template portal>Cake</template>` ,
154
- } )
137
+ @Component ( { template : `<template portal>Cake</template>` } )
155
138
class TestComponentWithTemplatePortals {
156
139
@ViewChild ( TemplatePortalDirective ) templatePortal : TemplatePortalDirective ;
157
140
158
141
constructor ( public viewContainerRef : ViewContainerRef ) { }
159
142
}
160
143
144
+ // Create a real (non-test) NgModule as a workaround for
145
+ // https://github.com/angular/angular/issues/10760
146
+ const TEST_COMPONENTS = [ PizzaMsg , TestComponentWithTemplatePortals ] ;
147
+ @NgModule ( {
148
+ imports : [ OverlayModule , PortalModule ] ,
149
+ exports : TEST_COMPONENTS ,
150
+ declarations : TEST_COMPONENTS ,
151
+ entryComponents : TEST_COMPONENTS ,
152
+ } )
153
+ class OverlayTestModule { }
154
+
161
155
class FakePositionStrategy implements PositionStrategy {
162
156
apply ( element : Element ) : Promise < void > {
163
157
element . classList . add ( 'fake-positioned' ) ;
0 commit comments