Skip to content

Commit a5b3296

Browse files
authored
fix(menu): make menu open idempotent (#1478)
1 parent fcc5900 commit a5b3296

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/lib/menu/menu-trigger.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
6666
}
6767

6868
openMenu(): void {
69-
this._createOverlay();
70-
this._overlayRef.attach(this._portal);
71-
this._initMenu();
69+
if (!this._menuOpen) {
70+
this._createOverlay();
71+
this._overlayRef.attach(this._portal);
72+
this._initMenu();
73+
}
7274
}
7375

7476
closeMenu(): void {

src/lib/menu/menu.spec.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
import {TestBed, async} from '@angular/core/testing';
2-
import {Component} from '@angular/core';
3-
import {MdMenuModule} from './menu';
2+
import {Component, ViewChild} from '@angular/core';
3+
import {By} from '@angular/platform-browser';
4+
import {MdMenuModule, MdMenuTrigger} from './menu';
45

56

67
describe('MdMenu', () => {
78

89
beforeEach(async(() => {
910
TestBed.configureTestingModule({
1011
imports: [MdMenuModule.forRoot()],
11-
declarations: [TestMenu],
12+
declarations: [SimpleMenu],
1213
});
1314

1415
TestBed.compileComponents();
1516
}));
1617

17-
it('should add and remove focus class on focus/blur', () => {
18-
let fixture = TestBed.createComponent(TestMenu);
19-
expect(fixture).toBeTruthy();
18+
it('should open the menu as an idempotent operation', () => {
19+
let fixture = TestBed.createComponent(SimpleMenu);
20+
fixture.detectChanges();
21+
let menu = fixture.debugElement.query(By.css('.md-menu'));
22+
expect(menu).toBe(null);
23+
expect(() => {
24+
fixture.componentInstance.trigger.openMenu();
25+
fixture.componentInstance.trigger.openMenu();
26+
27+
menu = fixture.debugElement.query(By.css('.md-menu'));
28+
expect(menu.nativeElement.innerHTML.trim()).toEqual('Content');
29+
}).not.toThrowError();
2030
});
2131
});
2232

23-
@Component({template: ``})
24-
class TestMenu {}
33+
@Component({
34+
template: `
35+
<button [md-menu-trigger-for]="menu">Toggle menu</button>
36+
<md-menu #menu="mdMenu">
37+
Content
38+
</md-menu>
39+
`
40+
})
41+
class SimpleMenu {
42+
@ViewChild(MdMenuTrigger) trigger: MdMenuTrigger;
43+
}

0 commit comments

Comments
 (0)