From a5b32964cbd2d8b6024c643727b5968e2ddd5416 Mon Sep 17 00:00:00 2001 From: Kara Date: Tue, 11 Oct 2016 13:40:43 -0700 Subject: [PATCH] fix(menu): make menu open idempotent (#1478) --- src/lib/menu/menu-trigger.ts | 8 +++++--- src/lib/menu/menu.spec.ts | 35 +++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/lib/menu/menu-trigger.ts b/src/lib/menu/menu-trigger.ts index 419d8a827d09..d7ce7b122fb8 100644 --- a/src/lib/menu/menu-trigger.ts +++ b/src/lib/menu/menu-trigger.ts @@ -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 { diff --git a/src/lib/menu/menu.spec.ts b/src/lib/menu/menu.spec.ts index b54f2e6a555f..1bdf9b2e1977 100644 --- a/src/lib/menu/menu.spec.ts +++ b/src/lib/menu/menu.spec.ts @@ -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: ` + + + Content + + ` +}) +class SimpleMenu { + @ViewChild(MdMenuTrigger) trigger: MdMenuTrigger; +}