Skip to content

Commit

Permalink
fix(menu): throw better error when trying to open undefined menu (#12688
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
crisbeto authored and jelbourn committed Aug 29, 2018
1 parent 3ce4e8d commit f732059
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f732059

Please sign in to comment.