|
| 1 | +import {Component, ElementRef, ViewChild} from '@angular/core'; |
| 2 | +import {ComponentFixture, async, TestBed} from '@angular/core/testing'; |
| 3 | +import {CdkMenuItem, CdkMenuModule, CdkMenu} from '@angular/cdk-experimental/menu'; |
| 4 | +import {MatMenuBarItem} from './menubar-item'; |
| 5 | +import {MatMenuBarModule} from './menubar-module'; |
| 6 | + |
| 7 | +describe('MatMenuBarItem', () => { |
| 8 | + let fixture: ComponentFixture<SimpleMenuBarItem>; |
| 9 | + let menubarItem: MatMenuBarItem; |
| 10 | + let nativeMenubarItem: HTMLElement; |
| 11 | + |
| 12 | + beforeEach(async(() => { |
| 13 | + TestBed.configureTestingModule({ |
| 14 | + imports: [MatMenuBarModule, CdkMenuModule], |
| 15 | + declarations: [SimpleMenuBarItem], |
| 16 | + }).compileComponents(); |
| 17 | + })); |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + fixture = TestBed.createComponent(SimpleMenuBarItem); |
| 21 | + fixture.detectChanges(); |
| 22 | + |
| 23 | + menubarItem = fixture.componentInstance.menubarItem; |
| 24 | + nativeMenubarItem = fixture.componentInstance.nativeMenubarItem.nativeElement; |
| 25 | + }); |
| 26 | + |
| 27 | + it('should have the menuitem role', () => { |
| 28 | + expect(nativeMenubarItem.getAttribute('role')).toBe('menuitem'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should be a button type', () => { |
| 32 | + expect(nativeMenubarItem.getAttribute('type')).toBe('button'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should not set the aria-disabled attribute when false', () => { |
| 36 | + expect(nativeMenubarItem.hasAttribute('aria.disabled')).toBeFalse(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should coerce and set aria-disabled attribute', () => { |
| 40 | + (menubarItem.disabled as any) = ''; |
| 41 | + fixture.detectChanges(); |
| 42 | + |
| 43 | + expect(nativeMenubarItem.getAttribute('aria-disabled')).toBe('true'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should have cdk and material classes set', () => { |
| 47 | + expect(nativeMenubarItem.classList.contains('cdk-menu-item')).toBeTrue(); |
| 48 | + expect(nativeMenubarItem.classList.contains('mat-menubar-item')).toBeTrue(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should open the attached menu on click', () => { |
| 52 | + nativeMenubarItem.click(); |
| 53 | + fixture.detectChanges(); |
| 54 | + |
| 55 | + expect(fixture.componentInstance.menu).toBeDefined(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should have initial tab index set to -1', () => { |
| 59 | + expect(nativeMenubarItem.tabIndex).toBe(-1); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +@Component({ |
| 64 | + template: ` |
| 65 | + <mat-menubar> |
| 66 | + <mat-menubar-item [cdkMenuTriggerFor]="sub">File</mat-menubar-item> |
| 67 | + <mat-menubar-item [cdkMenuTriggerFor]="sub">File</mat-menubar-item> |
| 68 | + </mat-menubar> |
| 69 | +
|
| 70 | + <ng-template cdkMenuPanel #sub="cdkMenuPanel"> |
| 71 | + <div #menu cdkMenu [cdkMenuPanel]="sub"> |
| 72 | + <button cdkMenuItem></button> |
| 73 | + </div> |
| 74 | + </ng-template> |
| 75 | + `, |
| 76 | +}) |
| 77 | +class SimpleMenuBarItem { |
| 78 | + @ViewChild(CdkMenuItem) menubarItem: MatMenuBarItem; |
| 79 | + @ViewChild(CdkMenuItem, {read: ElementRef}) nativeMenubarItem: ElementRef; |
| 80 | + |
| 81 | + @ViewChild('menu') menu: CdkMenu; |
| 82 | +} |
0 commit comments