mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(menu): make menu open idempotent (#1478)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user