Skip to content

Commit

Permalink
fix(menu): make menu open idempotent (#1478)
Browse files Browse the repository at this point in the history
  • Loading branch information
kara authored Oct 11, 2016
1 parent fcc5900 commit a5b3296
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 27 additions & 8 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
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', () => {

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;
}

0 comments on commit a5b3296

Please sign in to comment.