|
1 | 1 | import {TestBed, async} from '@angular/core/testing'; |
2 | 2 | import {Component, ViewChild} from '@angular/core'; |
3 | | -import {By} from '@angular/platform-browser'; |
4 | 3 | import {MdMenuModule, MdMenuTrigger} from './menu'; |
| 4 | +import {OverlayContainer} from '../core/overlay/overlay-container'; |
5 | 5 |
|
6 | 6 |
|
7 | 7 | describe('MdMenu', () => { |
| 8 | + let overlayContainerElement: HTMLElement; |
8 | 9 |
|
9 | 10 | beforeEach(async(() => { |
10 | 11 | TestBed.configureTestingModule({ |
11 | 12 | imports: [MdMenuModule.forRoot()], |
12 | 13 | declarations: [SimpleMenu], |
| 14 | + providers: [ |
| 15 | + {provide: OverlayContainer, useFactory: () => { |
| 16 | + overlayContainerElement = document.createElement('div'); |
| 17 | + return {getContainerElement: () => overlayContainerElement}; |
| 18 | + }} |
| 19 | + ] |
13 | 20 | }); |
14 | 21 |
|
15 | 22 | TestBed.compileComponents(); |
16 | 23 | })); |
17 | 24 |
|
18 | 25 | it('should open the menu as an idempotent operation', () => { |
19 | | - let fixture = TestBed.createComponent(SimpleMenu); |
| 26 | + const fixture = TestBed.createComponent(SimpleMenu); |
20 | 27 | fixture.detectChanges(); |
21 | | - let menu = fixture.debugElement.query(By.css('.md-menu-panel')); |
22 | | - expect(menu).toBe(null); |
| 28 | + expect(overlayContainerElement.textContent).toBe(''); |
23 | 29 | expect(() => { |
24 | 30 | fixture.componentInstance.trigger.openMenu(); |
25 | 31 | fixture.componentInstance.trigger.openMenu(); |
26 | 32 |
|
27 | | - menu = fixture.debugElement.query(By.css('.md-menu-panel')); |
28 | | - expect(menu.nativeElement.innerHTML.trim()).toEqual('Content'); |
| 33 | + expect(overlayContainerElement.textContent.trim()).toBe('Content'); |
29 | 34 | }).not.toThrowError(); |
30 | 35 | }); |
| 36 | + |
| 37 | + it('should close the menu when a click occurs outside the menu', () => { |
| 38 | + const fixture = TestBed.createComponent(SimpleMenu); |
| 39 | + fixture.detectChanges(); |
| 40 | + fixture.componentInstance.trigger.openMenu(); |
| 41 | + |
| 42 | + const backdrop = <HTMLElement>overlayContainerElement.querySelector('.md-overlay-backdrop'); |
| 43 | + backdrop.click(); |
| 44 | + fixture.detectChanges(); |
| 45 | + |
| 46 | + expect(overlayContainerElement.textContent).toBe(''); |
| 47 | + }); |
| 48 | + |
31 | 49 | }); |
32 | 50 |
|
33 | 51 | @Component({ |
34 | 52 | template: ` |
35 | 53 | <button [md-menu-trigger-for]="menu">Toggle menu</button> |
36 | 54 | <md-menu #menu="mdMenu"> |
37 | | - Content |
| 55 | + <button md-menu-item> Content </button> |
38 | 56 | </md-menu> |
39 | 57 | ` |
40 | 58 | }) |
|
0 commit comments