1
1
import { ElementRef } from '@angular/core' ;
2
2
import { TestBed , inject } from '@angular/core/testing' ;
3
- import { ConnectedPositionStrategy } from './connected-position-strategy' ;
4
- import { ViewportRuler , VIEWPORT_RULER_PROVIDER } from '@angular/cdk/scrolling' ;
5
3
import { OverlayPositionBuilder } from './overlay-position-builder' ;
6
- import { ConnectedOverlayPositionChange } from './connected-position' ;
7
4
import { CdkScrollable } from '@angular/cdk/scrolling' ;
8
5
import { Subscription } from 'rxjs/Subscription' ;
9
- import { ScrollDispatchModule } from '@angular/cdk/scrolling' ;
10
- import { OverlayRef } from '../overlay-ref' ;
6
+ import {
7
+ OverlayModule ,
8
+ Overlay ,
9
+ OverlayRef ,
10
+ ConnectedPositionStrategy ,
11
+ ConnectedOverlayPositionChange ,
12
+ } from '../index' ;
11
13
12
14
13
15
// Default width and height of the overlay and origin panels throughout these tests.
@@ -19,17 +21,15 @@ const DEFAULT_WIDTH = 60;
19
21
// for tests on CI (both SauceLabs and Browserstack).
20
22
21
23
describe ( 'ConnectedPositionStrategy' , ( ) => {
24
+ let positionBuilder : OverlayPositionBuilder ;
22
25
23
- let viewportRuler : ViewportRuler ;
26
+ beforeEach ( ( ) => {
27
+ TestBed . configureTestingModule ( { imports : [ OverlayModule ] } ) ;
24
28
25
- beforeEach ( ( ) => TestBed . configureTestingModule ( {
26
- imports : [ ScrollDispatchModule ] ,
27
- providers : [ VIEWPORT_RULER_PROVIDER ]
28
- } ) ) ;
29
-
30
- beforeEach ( inject ( [ ViewportRuler ] , ( _ruler : ViewportRuler ) => {
31
- viewportRuler = _ruler ;
32
- } ) ) ;
29
+ inject ( [ Overlay ] , ( overlay : Overlay ) => {
30
+ positionBuilder = overlay . position ( ) ;
31
+ } ) ( ) ;
32
+ } ) ;
33
33
34
34
describe ( 'with origin on document body' , ( ) => {
35
35
const ORIGIN_HEIGHT = DEFAULT_HEIGHT ;
@@ -42,7 +42,6 @@ describe('ConnectedPositionStrategy', () => {
42
42
let overlayContainerElement : HTMLElement ;
43
43
let strategy : ConnectedPositionStrategy ;
44
44
let fakeElementRef : ElementRef ;
45
- let positionBuilder : OverlayPositionBuilder ;
46
45
47
46
let originRect : ClientRect | null ;
48
47
let originCenterX : number | null ;
@@ -56,9 +55,7 @@ describe('ConnectedPositionStrategy', () => {
56
55
document . body . appendChild ( originElement ) ;
57
56
document . body . appendChild ( overlayContainerElement ) ;
58
57
overlayContainerElement . appendChild ( overlayElement ) ;
59
-
60
- fakeElementRef = new FakeElementRef ( originElement ) ;
61
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
58
+ fakeElementRef = new ElementRef ( originElement ) ;
62
59
} ) ;
63
60
64
61
afterEach ( ( ) => {
@@ -177,8 +174,6 @@ describe('ConnectedPositionStrategy', () => {
177
174
} ) ;
178
175
179
176
it ( 'should reposition the overlay if it would go off the bottom of the screen' , ( ) => {
180
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
181
-
182
177
originElement . style . bottom = '25px' ;
183
178
originElement . style . left = '200px' ;
184
179
originRect = originElement . getBoundingClientRect ( ) ;
@@ -200,8 +195,6 @@ describe('ConnectedPositionStrategy', () => {
200
195
} ) ;
201
196
202
197
it ( 'should reposition the overlay if it would go off the right of the screen' , ( ) => {
203
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
204
-
205
198
originElement . style . top = '200px' ;
206
199
originElement . style . right = '25px' ;
207
200
originRect = originElement . getBoundingClientRect ( ) ;
@@ -224,8 +217,6 @@ describe('ConnectedPositionStrategy', () => {
224
217
} ) ;
225
218
226
219
it ( 'should recalculate and set the last position with recalculateLastPosition()' , ( ) => {
227
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
228
-
229
220
// Push the trigger down so the overlay doesn't have room to open on the bottom.
230
221
originElement . style . bottom = '25px' ;
231
222
originRect = originElement . getBoundingClientRect ( ) ;
@@ -254,8 +245,6 @@ describe('ConnectedPositionStrategy', () => {
254
245
} ) ;
255
246
256
247
it ( 'should default to the initial position, if no positions fit in the viewport' , ( ) => {
257
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
258
-
259
248
// Make the origin element taller than the viewport.
260
249
originElement . style . height = '1000px' ;
261
250
originElement . style . top = '0' ;
@@ -353,7 +342,6 @@ describe('ConnectedPositionStrategy', () => {
353
342
} ) ;
354
343
355
344
it ( 'should emit onPositionChange event when position changes' , ( ) => {
356
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
357
345
originElement . style . top = '200px' ;
358
346
originElement . style . right = '25px' ;
359
347
@@ -390,7 +378,6 @@ describe('ConnectedPositionStrategy', () => {
390
378
} ) ;
391
379
392
380
it ( 'should emit the onPositionChange event even if none of the positions fit' , ( ) => {
393
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
394
381
originElement . style . bottom = '25px' ;
395
382
originElement . style . right = '25px' ;
396
383
@@ -414,8 +401,6 @@ describe('ConnectedPositionStrategy', () => {
414
401
} ) ;
415
402
416
403
it ( 'should pick the fallback position that shows the largest area of the element' , ( ) => {
417
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
418
-
419
404
originElement . style . top = '200px' ;
420
405
originElement . style . right = '25px' ;
421
406
originRect = originElement . getBoundingClientRect ( ) ;
@@ -441,7 +426,6 @@ describe('ConnectedPositionStrategy', () => {
441
426
} ) ;
442
427
443
428
it ( 'should re-use the preferred position when re-applying while locked in' , ( ) => {
444
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
445
429
strategy = positionBuilder . connectedTo (
446
430
fakeElementRef ,
447
431
{ originX : 'end' , originY : 'center' } ,
@@ -584,15 +568,14 @@ describe('ConnectedPositionStrategy', () => {
584
568
scrollable . appendChild ( originElement ) ;
585
569
586
570
// Create a strategy with knowledge of the scrollable container
587
- let positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
588
- let fakeElementRef = new FakeElementRef ( originElement ) ;
571
+ let fakeElementRef = new ElementRef ( originElement ) ;
589
572
strategy = positionBuilder . connectedTo (
590
573
fakeElementRef ,
591
574
{ originX : 'start' , originY : 'bottom' } ,
592
575
{ overlayX : 'start' , overlayY : 'top' } ) ;
593
576
594
577
strategy . withScrollableContainers ( [
595
- new CdkScrollable ( new FakeElementRef ( scrollable ) , null ! , null ! ) ] ) ;
578
+ new CdkScrollable ( new ElementRef ( scrollable ) , null ! , null ! ) ] ) ;
596
579
strategy . attach ( fakeOverlayRef ( overlayElement ) ) ;
597
580
positionChangeHandler = jasmine . createSpy ( 'positionChangeHandler' ) ;
598
581
onPositionChangeSubscription = strategy . onPositionChange . subscribe ( positionChangeHandler ) ;
@@ -666,7 +649,6 @@ describe('ConnectedPositionStrategy', () => {
666
649
let overlayContainerElement : HTMLElement ;
667
650
let strategy : ConnectedPositionStrategy ;
668
651
let fakeElementRef : ElementRef ;
669
- let positionBuilder : OverlayPositionBuilder ;
670
652
671
653
beforeEach ( ( ) => {
672
654
// The origin and overlay elements need to be in the document body in order to have geometry.
@@ -676,9 +658,7 @@ describe('ConnectedPositionStrategy', () => {
676
658
document . body . appendChild ( originElement ) ;
677
659
document . body . appendChild ( overlayContainerElement ) ;
678
660
overlayContainerElement . appendChild ( overlayElement ) ;
679
-
680
- fakeElementRef = new FakeElementRef ( originElement ) ;
681
- positionBuilder = new OverlayPositionBuilder ( viewportRuler , document ) ;
661
+ fakeElementRef = new ElementRef ( originElement ) ;
682
662
} ) ;
683
663
684
664
afterEach ( ( ) => {
@@ -809,12 +789,6 @@ function createOverflowContainerElement() {
809
789
return element ;
810
790
}
811
791
812
-
813
- /** Fake implementation of ElementRef that is just a simple container for nativeElement. */
814
- class FakeElementRef implements ElementRef {
815
- constructor ( public nativeElement : HTMLElement ) { }
816
- }
817
-
818
792
function fakeOverlayRef ( overlayElement : HTMLElement ) {
819
793
return { overlayElement} as OverlayRef ;
820
794
}
0 commit comments