Skip to content

Commit

Permalink
fix(tooltip): throw a better error when an invalid position is passed
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
crisbeto committed Dec 6, 2016
1 parent a1acf99 commit e4626de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/lib/tooltip/tooltip-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {MdError} from '../core';

/** Exception thrown when a tooltip has an invalid position. */
export class MdTooltipInvalidPositionError extends MdError {
constructor(position: string) {
super(`Tooltip position "${position}" is invalid.`);
}
}
12 changes: 10 additions & 2 deletions src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,28 @@ describe('MdTooltip', () => {
tooltipDirective.show();
expect(tooltipDirective._tooltipInstance._transformOrigin).toBe('right');
});

it('should throw when trying to assign an invalid position', () => {
expect(() => {
fixture.componentInstance.position = 'everywhere';
fixture.detectChanges();
tooltipDirective.show();
}).toThrowError('Tooltip position "everywhere" is invalid.');
});
});
});

@Component({
selector: 'app',
template: `
<button *ngIf="showButton"
[md-tooltip]="message"
[md-tooltip]="message"
[tooltip-position]="position">
Button
</button>`
})
class BasicTooltipDemo {
position: TooltipPosition = 'below';
position: string = 'below';
message: string = initialTooltipMessage;
showButton: boolean = true;
}
6 changes: 6 additions & 0 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
OVERLAY_PROVIDERS,
DefaultStyleCompatibilityModeModule,
} from '../core';
import {MdTooltipInvalidPositionError} from './tooltip-errors';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {Dir} from '../core/rtl/dir';
Expand Down Expand Up @@ -178,6 +179,8 @@ export class MdTooltip {
this.position == 'before' && !isDirectionLtr) {
return {originX: 'end', originY: 'center'};
}

throw new MdTooltipInvalidPositionError(this.position);
}

/** Returns the overlay position based on the user's preference */
Expand All @@ -202,6 +205,8 @@ export class MdTooltip {
this.position == 'before' && !isLtr) {
return {overlayX: 'start', overlayY: 'center'};
}

throw new MdTooltipInvalidPositionError(this.position);
}

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

Expand Down

0 comments on commit e4626de

Please sign in to comment.