Skip to content

Commit 539e44e

Browse files
crisbetommalerba
authored andcommitted
fix(tooltip): throw a better error when an invalid position is passed (#1986)
* Throws a more informative error message when an invalid position is passed to the tooltip. Previously it would throw something along the lines of `Cannot read property 'originX' of undefined`. * Moves the various tooltip switch statements to use an enum for easier validation. Referencing #1959.
1 parent a1acf99 commit 539e44e

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/lib/tooltip/tooltip-errors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {MdError} from '../core';
2+
3+
/** Exception thrown when a tooltip has an invalid position. */
4+
export class MdTooltipInvalidPositionError extends MdError {
5+
constructor(position: string) {
6+
super(`Tooltip position "${position}" is invalid.`);
7+
}
8+
}

src/lib/tooltip/tooltip.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,20 +253,28 @@ describe('MdTooltip', () => {
253253
tooltipDirective.show();
254254
expect(tooltipDirective._tooltipInstance._transformOrigin).toBe('right');
255255
});
256+
257+
it('should throw when trying to assign an invalid position', () => {
258+
expect(() => {
259+
fixture.componentInstance.position = 'everywhere';
260+
fixture.detectChanges();
261+
tooltipDirective.show();
262+
}).toThrowError('Tooltip position "everywhere" is invalid.');
263+
});
256264
});
257265
});
258266

259267
@Component({
260268
selector: 'app',
261269
template: `
262270
<button *ngIf="showButton"
263-
[md-tooltip]="message"
271+
[md-tooltip]="message"
264272
[tooltip-position]="position">
265273
Button
266274
</button>`
267275
})
268276
class BasicTooltipDemo {
269-
position: TooltipPosition = 'below';
277+
position: string = 'below';
270278
message: string = initialTooltipMessage;
271279
showButton: boolean = true;
272280
}

src/lib/tooltip/tooltip.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
OVERLAY_PROVIDERS,
2727
DefaultStyleCompatibilityModeModule,
2828
} from '../core';
29+
import {MdTooltipInvalidPositionError} from './tooltip-errors';
2930
import {Observable} from 'rxjs/Observable';
3031
import {Subject} from 'rxjs/Subject';
3132
import {Dir} from '../core/rtl/dir';
@@ -178,6 +179,8 @@ export class MdTooltip {
178179
this.position == 'before' && !isDirectionLtr) {
179180
return {originX: 'end', originY: 'center'};
180181
}
182+
183+
throw new MdTooltipInvalidPositionError(this.position);
181184
}
182185

183186
/** Returns the overlay position based on the user's preference */
@@ -202,6 +205,8 @@ export class MdTooltip {
202205
this.position == 'before' && !isLtr) {
203206
return {overlayX: 'start', overlayY: 'center'};
204207
}
208+
209+
throw new MdTooltipInvalidPositionError(this.position);
205210
}
206211

207212
/** Updates the tooltip message and repositions the overlay according to the new message length */
@@ -302,6 +307,7 @@ export class TooltipComponent {
302307
case 'right': this._transformOrigin = 'left'; break;
303308
case 'above': this._transformOrigin = 'bottom'; break;
304309
case 'below': this._transformOrigin = 'top'; break;
310+
default: throw new MdTooltipInvalidPositionError(value);
305311
}
306312
}
307313

0 commit comments

Comments
 (0)