Skip to content

Commit c437e71

Browse files
committed
refactor(overlay): move connected position assertions into FlexibleConnectedPositionStrategy
Since the `ConnectedPositionStrategy` proxies everything to `FlexibleConnectedPositionStrategy`, these changes move the assertions that verify that the config looks correctly into the `FlexibleConnectedPositionStrategy`.
1 parent 1616f2f commit c437e71

File tree

4 files changed

+68
-78
lines changed

4 files changed

+68
-78
lines changed

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

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const DEFAULT_WIDTH = 60;
2626
describe('ConnectedPositionStrategy', () => {
2727
let overlay: Overlay;
2828
let overlayContainer: OverlayContainer;
29-
let overlayContainerElement: HTMLElement;
3029
let zone: MockNgZone;
3130
let overlayRef: OverlayRef;
3231

@@ -39,7 +38,6 @@ describe('ConnectedPositionStrategy', () => {
3938
inject([Overlay, OverlayContainer], (o: Overlay, oc: OverlayContainer) => {
4039
overlay = o;
4140
overlayContainer = oc;
42-
overlayContainerElement = oc.getContainerElement();
4341
})();
4442
});
4543

@@ -775,61 +773,6 @@ describe('ConnectedPositionStrategy', () => {
775773

776774
});
777775

778-
describe('validations', () => {
779-
let overlayElement: HTMLElement;
780-
let originElement: HTMLElement;
781-
let positionStrategy: ConnectedPositionStrategy;
782-
783-
beforeEach(() => {
784-
overlayElement = createPositionedBlockElement();
785-
overlayContainerElement.appendChild(overlayElement);
786-
originElement = createBlockElement();
787-
788-
positionStrategy = overlay.position().connectedTo(
789-
new ElementRef(originElement),
790-
{originX: 'start', originY: 'bottom'},
791-
{overlayX: 'start', overlayY: 'top'});
792-
793-
attachOverlay(positionStrategy);
794-
});
795-
796-
afterEach(() => {
797-
positionStrategy.dispose();
798-
});
799-
800-
it('should throw when attaching without any positions', () => {
801-
positionStrategy.withPositions([]);
802-
expect(() => positionStrategy.apply()).toThrow();
803-
});
804-
805-
it('should throw when passing in something that is missing a connection point', () => {
806-
positionStrategy.withPositions([{originY: 'top', overlayX: 'start', overlayY: 'top'} as any]);
807-
expect(() => positionStrategy.apply()).toThrow();
808-
});
809-
810-
it('should throw when passing in something that has an invalid X position', () => {
811-
positionStrategy.withPositions([{
812-
originX: 'left',
813-
originY: 'top',
814-
overlayX: 'left',
815-
overlayY: 'top'
816-
} as any]);
817-
818-
expect(() => positionStrategy.apply()).toThrow();
819-
});
820-
821-
it('should throw when passing in something that has an invalid Y position', () => {
822-
positionStrategy.withPositions([{
823-
originX: 'start',
824-
originY: 'middle',
825-
overlayX: 'start',
826-
overlayY: 'middle'
827-
} as any]);
828-
829-
expect(() => positionStrategy.apply()).toThrow();
830-
});
831-
});
832-
833776
});
834777

835778
/** Creates an absolutely positioned, display: block element with a default size. */

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import {
1616
ConnectionPositionPair,
1717
OriginConnectionPosition,
1818
OverlayConnectionPosition,
19-
validateHorizontalPosition,
20-
validateVerticalPosition,
2119
} from './connected-position';
2220
import {FlexibleConnectedPositionStrategy} from './flexible-connected-position-strategy';
2321
import {PositionStrategy} from './position-strategy';
@@ -110,7 +108,6 @@ export class ConnectedPositionStrategy implements PositionStrategy {
110108
* @docs-private
111109
*/
112110
apply(): void {
113-
this._validatePositions();
114111
this._positionStrategy.apply();
115112
}
116113

@@ -120,7 +117,6 @@ export class ConnectedPositionStrategy implements PositionStrategy {
120117
* allows one to re-align the panel without changing the orientation of the panel.
121118
*/
122119
recalculateLastPosition(): void {
123-
this._validatePositions();
124120
this._positionStrategy.reapplyLastPosition();
125121
}
126122

@@ -214,21 +210,4 @@ export class ConnectedPositionStrategy implements PositionStrategy {
214210
this._positionStrategy.setOrigin(origin);
215211
return this;
216212
}
217-
218-
/** Validates that the current position match the expected values. */
219-
private _validatePositions(): void {
220-
if (!this._preferredPositions.length) {
221-
throw Error('ConnectedPositionStrategy: At least one position is required.');
222-
}
223-
224-
// TODO(crisbeto): remove these once Angular's template type
225-
// checking is advanced enough to catch these cases.
226-
// TODO(crisbeto): port these checks into the flexible positioning.
227-
this._preferredPositions.forEach(pair => {
228-
validateHorizontalPosition('originX', pair.originX);
229-
validateVerticalPosition('originY', pair.originY);
230-
validateHorizontalPosition('overlayX', pair.overlayX);
231-
validateVerticalPosition('overlayY', pair.overlayY);
232-
});
233-
}
234213
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,53 @@ describe('FlexibleConnectedPositionStrategy', () => {
15881588

15891589
});
15901590

1591+
describe('validations', () => {
1592+
let originElement: HTMLElement;
1593+
let positionStrategy: FlexibleConnectedPositionStrategy;
1594+
1595+
beforeEach(() => {
1596+
originElement = createPositionedBlockElement();
1597+
document.body.appendChild(originElement);
1598+
positionStrategy = overlay.position().flexibleConnectedTo(new ElementRef(originElement));
1599+
});
1600+
1601+
afterEach(() => {
1602+
positionStrategy.dispose();
1603+
});
1604+
1605+
it('should throw when attaching without any positions', () => {
1606+
positionStrategy.withPositions([]);
1607+
expect(() => attachOverlay({positionStrategy})).toThrow();
1608+
});
1609+
1610+
it('should throw when passing in something that is missing a connection point', () => {
1611+
positionStrategy.withPositions([{originY: 'top', overlayX: 'start', overlayY: 'top'} as any]);
1612+
expect(() => attachOverlay({positionStrategy})).toThrow();
1613+
});
1614+
1615+
it('should throw when passing in something that has an invalid X position', () => {
1616+
positionStrategy.withPositions([{
1617+
originX: 'left',
1618+
originY: 'top',
1619+
overlayX: 'left',
1620+
overlayY: 'top'
1621+
} as any]);
1622+
1623+
expect(() => attachOverlay({positionStrategy})).toThrow();
1624+
});
1625+
1626+
it('should throw when passing in something that has an invalid Y position', () => {
1627+
positionStrategy.withPositions([{
1628+
originX: 'start',
1629+
originY: 'middle',
1630+
overlayX: 'start',
1631+
overlayY: 'middle'
1632+
} as any]);
1633+
1634+
expect(() => attachOverlay({positionStrategy})).toThrow();
1635+
});
1636+
});
1637+
15911638
});
15921639

15931640
/** Creates an absolutely positioned, display: block element with a default size. */

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
ConnectedOverlayPositionChange,
1414
ConnectionPositionPair,
1515
ScrollingVisibility,
16+
validateHorizontalPosition,
17+
validateVerticalPosition,
1618
} from './connected-position';
1719
import {Observable, Subscription, Subject} from 'rxjs';
1820
import {OverlayRef} from '../overlay-ref';
@@ -159,6 +161,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
159161
return;
160162
}
161163

164+
this._validatePositions();
165+
162166
// If the position has been applied already (e.g. when the overlay was opened) and the
163167
// consumer opted into locking in the position, re-use the old position, in order to
164168
// prevent the overlay from jumping around.
@@ -284,6 +288,7 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
284288
*/
285289
reapplyLastPosition(): void {
286290
if (!this._isDisposed) {
291+
this._validatePositions();
287292
this._originRect = this._origin.getBoundingClientRect();
288293
this._overlayRect = this._pane.getBoundingClientRect();
289294
this._viewportRect = this._getNarrowedViewportRect();
@@ -912,6 +917,22 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
912917

913918
return position.offsetY == null ? this._offsetY : position.offsetY;
914919
}
920+
921+
/** Validates that the current position match the expected values. */
922+
private _validatePositions(): void {
923+
if (!this._preferredPositions.length) {
924+
throw Error('FlexibleConnectedPositionStrategy: At least one position is required.');
925+
}
926+
927+
// TODO(crisbeto): remove these once Angular's template type
928+
// checking is advanced enough to catch these cases.
929+
this._preferredPositions.forEach(pair => {
930+
validateHorizontalPosition('originX', pair.originX);
931+
validateVerticalPosition('originY', pair.originY);
932+
validateHorizontalPosition('overlayX', pair.overlayX);
933+
validateVerticalPosition('overlayY', pair.overlayY);
934+
});
935+
}
915936
}
916937

917938
/** A simple (x, y) coordinate. */

0 commit comments

Comments
 (0)