fix(menu): throw better error when trying to open undefined menu (#12688)

Throws a better error than "Unable to read property templateRef of undefined" when clicking on a trigger with an undefined menu. We have a check for this already in one of the lifecycle hooks, but people can skip it when unit testing.

Fixes #12649.
This commit is contained in:
Kristiyan Kostadinov
2018-08-21 20:44:33 +02:00
committed by Jeremy Elbourn
parent 2364b7cc3a
commit c90dcfb5ac
2 changed files with 15 additions and 0 deletions
+2
View File
@@ -195,6 +195,8 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
return;
}
this._checkMenu();
const overlayRef = this._createOverlay();
this._setPosition(overlayRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy);
overlayRef.attach(this._portal);
+13
View File
@@ -427,6 +427,19 @@ describe('MatMenu', () => {
expect(triggerEl.hasAttribute('aria-expanded')).toBe(false);
});
it('should throw the correct error if the menu is not defined after init', () => {
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
fixture.detectChanges();
fixture.componentInstance.trigger.menu = null!;
fixture.detectChanges();
expect(() => {
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();
}).toThrowError(/must pass in an mat-menu instance/);
});
describe('lazy rendering', () => {
it('should be able to render the menu content lazily', fakeAsync(() => {
const fixture = createComponent(SimpleLazyMenu);