Skip to content

Commit 1a383ce

Browse files
crisbetotinayuangao
authored andcommitted
refactor(overlay): more robust testing setup (#8638)
Reworks the testing setup for the `ConnectedPositionStrategy` and `GlobalPositionStrategy` to avoid constructing the services ourselves and to prevent failures like #8559.
1 parent 8009f1b commit 1a383ce

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

src/cdk/overlay/position/connected-position-strategy.spec.ts

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import {ElementRef} from '@angular/core';
22
import {TestBed, inject} from '@angular/core/testing';
3-
import {ConnectedPositionStrategy} from './connected-position-strategy';
4-
import {ViewportRuler, VIEWPORT_RULER_PROVIDER} from '@angular/cdk/scrolling';
53
import {OverlayPositionBuilder} from './overlay-position-builder';
6-
import {ConnectedOverlayPositionChange} from './connected-position';
74
import {CdkScrollable} from '@angular/cdk/scrolling';
85
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';
1113

1214

1315
// Default width and height of the overlay and origin panels throughout these tests.
@@ -19,17 +21,15 @@ const DEFAULT_WIDTH = 60;
1921
// for tests on CI (both SauceLabs and Browserstack).
2022

2123
describe('ConnectedPositionStrategy', () => {
24+
let positionBuilder: OverlayPositionBuilder;
2225

23-
let viewportRuler: ViewportRuler;
26+
beforeEach(() => {
27+
TestBed.configureTestingModule({imports: [OverlayModule]});
2428

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+
});
3333

3434
describe('with origin on document body', () => {
3535
const ORIGIN_HEIGHT = DEFAULT_HEIGHT;
@@ -42,7 +42,6 @@ describe('ConnectedPositionStrategy', () => {
4242
let overlayContainerElement: HTMLElement;
4343
let strategy: ConnectedPositionStrategy;
4444
let fakeElementRef: ElementRef;
45-
let positionBuilder: OverlayPositionBuilder;
4645

4746
let originRect: ClientRect | null;
4847
let originCenterX: number | null;
@@ -56,9 +55,7 @@ describe('ConnectedPositionStrategy', () => {
5655
document.body.appendChild(originElement);
5756
document.body.appendChild(overlayContainerElement);
5857
overlayContainerElement.appendChild(overlayElement);
59-
60-
fakeElementRef = new FakeElementRef(originElement);
61-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
58+
fakeElementRef = new ElementRef(originElement);
6259
});
6360

6461
afterEach(() => {
@@ -177,8 +174,6 @@ describe('ConnectedPositionStrategy', () => {
177174
});
178175

179176
it('should reposition the overlay if it would go off the bottom of the screen', () => {
180-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
181-
182177
originElement.style.bottom = '25px';
183178
originElement.style.left = '200px';
184179
originRect = originElement.getBoundingClientRect();
@@ -200,8 +195,6 @@ describe('ConnectedPositionStrategy', () => {
200195
});
201196

202197
it('should reposition the overlay if it would go off the right of the screen', () => {
203-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
204-
205198
originElement.style.top = '200px';
206199
originElement.style.right = '25px';
207200
originRect = originElement.getBoundingClientRect();
@@ -224,8 +217,6 @@ describe('ConnectedPositionStrategy', () => {
224217
});
225218

226219
it('should recalculate and set the last position with recalculateLastPosition()', () => {
227-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
228-
229220
// Push the trigger down so the overlay doesn't have room to open on the bottom.
230221
originElement.style.bottom = '25px';
231222
originRect = originElement.getBoundingClientRect();
@@ -254,8 +245,6 @@ describe('ConnectedPositionStrategy', () => {
254245
});
255246

256247
it('should default to the initial position, if no positions fit in the viewport', () => {
257-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
258-
259248
// Make the origin element taller than the viewport.
260249
originElement.style.height = '1000px';
261250
originElement.style.top = '0';
@@ -353,7 +342,6 @@ describe('ConnectedPositionStrategy', () => {
353342
});
354343

355344
it('should emit onPositionChange event when position changes', () => {
356-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
357345
originElement.style.top = '200px';
358346
originElement.style.right = '25px';
359347

@@ -390,7 +378,6 @@ describe('ConnectedPositionStrategy', () => {
390378
});
391379

392380
it('should emit the onPositionChange event even if none of the positions fit', () => {
393-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
394381
originElement.style.bottom = '25px';
395382
originElement.style.right = '25px';
396383

@@ -414,8 +401,6 @@ describe('ConnectedPositionStrategy', () => {
414401
});
415402

416403
it('should pick the fallback position that shows the largest area of the element', () => {
417-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
418-
419404
originElement.style.top = '200px';
420405
originElement.style.right = '25px';
421406
originRect = originElement.getBoundingClientRect();
@@ -441,7 +426,6 @@ describe('ConnectedPositionStrategy', () => {
441426
});
442427

443428
it('should re-use the preferred position when re-applying while locked in', () => {
444-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
445429
strategy = positionBuilder.connectedTo(
446430
fakeElementRef,
447431
{originX: 'end', originY: 'center'},
@@ -584,15 +568,14 @@ describe('ConnectedPositionStrategy', () => {
584568
scrollable.appendChild(originElement);
585569

586570
// 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);
589572
strategy = positionBuilder.connectedTo(
590573
fakeElementRef,
591574
{originX: 'start', originY: 'bottom'},
592575
{overlayX: 'start', overlayY: 'top'});
593576

594577
strategy.withScrollableContainers([
595-
new CdkScrollable(new FakeElementRef(scrollable), null!, null!)]);
578+
new CdkScrollable(new ElementRef(scrollable), null!, null!)]);
596579
strategy.attach(fakeOverlayRef(overlayElement));
597580
positionChangeHandler = jasmine.createSpy('positionChangeHandler');
598581
onPositionChangeSubscription = strategy.onPositionChange.subscribe(positionChangeHandler);
@@ -666,7 +649,6 @@ describe('ConnectedPositionStrategy', () => {
666649
let overlayContainerElement: HTMLElement;
667650
let strategy: ConnectedPositionStrategy;
668651
let fakeElementRef: ElementRef;
669-
let positionBuilder: OverlayPositionBuilder;
670652

671653
beforeEach(() => {
672654
// The origin and overlay elements need to be in the document body in order to have geometry.
@@ -676,9 +658,7 @@ describe('ConnectedPositionStrategy', () => {
676658
document.body.appendChild(originElement);
677659
document.body.appendChild(overlayContainerElement);
678660
overlayContainerElement.appendChild(overlayElement);
679-
680-
fakeElementRef = new FakeElementRef(originElement);
681-
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);
661+
fakeElementRef = new ElementRef(originElement);
682662
});
683663

684664
afterEach(() => {
@@ -809,12 +789,6 @@ function createOverflowContainerElement() {
809789
return element;
810790
}
811791

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-
818792
function fakeOverlayRef(overlayElement: HTMLElement) {
819793
return {overlayElement} as OverlayRef;
820794
}

src/cdk/overlay/position/global-position-strategy.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import {GlobalPositionStrategy} from './global-position-strategy';
2-
import {OverlayRef} from '../overlay-ref';
1+
import {TestBed, inject} from '@angular/core/testing';
2+
import {OverlayModule, Overlay, OverlayRef, GlobalPositionStrategy} from '../index';
33

44

55
describe('GlobalPositonStrategy', () => {
66
let element: HTMLElement;
77
let strategy: GlobalPositionStrategy;
88

99
beforeEach(() => {
10+
TestBed.configureTestingModule({imports: [OverlayModule]});
11+
12+
inject([Overlay], (overlay: Overlay) => {
13+
strategy = overlay.position().global();
14+
})();
15+
1016
element = document.createElement('div');
11-
strategy = new GlobalPositionStrategy(document);
1217
document.body.appendChild(element);
1318
strategy.attach({overlayElement: element} as OverlayRef);
1419
});

0 commit comments

Comments
 (0)