fix(menu): make menu open idempotent (#1478)

This commit is contained in:
Kara
2016-10-11 13:40:43 -07:00
committed by GitHub
parent fcc5900e49
commit a5b32964cb
2 changed files with 32 additions and 11 deletions
+5 -3
View File
@@ -66,9 +66,11 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
}
openMenu(): void {
this._createOverlay();
this._overlayRef.attach(this._portal);
this._initMenu();
if (!this._menuOpen) {
this._createOverlay();
this._overlayRef.attach(this._portal);
this._initMenu();
}
}
closeMenu(): void {
+27 -8
View File
@@ -1,6 +1,7 @@
import {TestBed, async} from '@angular/core/testing';
import {Component} from '@angular/core';
import {MdMenuModule} from './menu';
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdMenuModule, MdMenuTrigger} from './menu';
describe('MdMenu', () => {
@@ -8,17 +9,35 @@ describe('MdMenu', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdMenuModule.forRoot()],
declarations: [TestMenu],
declarations: [SimpleMenu],
});
TestBed.compileComponents();
}));
it('should add and remove focus class on focus/blur', () => {
let fixture = TestBed.createComponent(TestMenu);
expect(fixture).toBeTruthy();
it('should open the menu as an idempotent operation', () => {
let fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();
let menu = fixture.debugElement.query(By.css('.md-menu'));
expect(menu).toBe(null);
expect(() => {
fixture.componentInstance.trigger.openMenu();
fixture.componentInstance.trigger.openMenu();
menu = fixture.debugElement.query(By.css('.md-menu'));
expect(menu.nativeElement.innerHTML.trim()).toEqual('Content');
}).not.toThrowError();
});
});
@Component({template: ``})
class TestMenu {}
@Component({
template: `
<button [md-menu-trigger-for]="menu">Toggle menu</button>
<md-menu #menu="mdMenu">
Content
</md-menu>
`
})
class SimpleMenu {
@ViewChild(MdMenuTrigger) trigger: MdMenuTrigger;
}