Skip to content

Commit f732059

Browse files
crisbetojelbourn
authored andcommitted
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.
1 parent 3ce4e8d commit f732059

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/lib/menu/menu-trigger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
195195
return;
196196
}
197197

198+
this._checkMenu();
199+
198200
const overlayRef = this._createOverlay();
199201
this._setPosition(overlayRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy);
200202
overlayRef.attach(this._portal);

src/lib/menu/menu.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,19 @@ describe('MatMenu', () => {
427427
expect(triggerEl.hasAttribute('aria-expanded')).toBe(false);
428428
});
429429

430+
it('should throw the correct error if the menu is not defined after init', () => {
431+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
432+
fixture.detectChanges();
433+
434+
fixture.componentInstance.trigger.menu = null!;
435+
fixture.detectChanges();
436+
437+
expect(() => {
438+
fixture.componentInstance.trigger.openMenu();
439+
fixture.detectChanges();
440+
}).toThrowError(/must pass in an mat-menu instance/);
441+
});
442+
430443
describe('lazy rendering', () => {
431444
it('should be able to render the menu content lazily', fakeAsync(() => {
432445
const fixture = createComponent(SimpleLazyMenu);

0 commit comments

Comments
 (0)