|
1 | 1 | import {Component, ViewChildren, QueryList, ElementRef, ViewChild, Type} from '@angular/core';
|
2 |
| -import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {ComponentFixture, TestBed, fakeAsync, tick, waitForAsync} from '@angular/core/testing'; |
3 | 3 | import {By} from '@angular/platform-browser';
|
4 | 4 | import {dispatchKeyboardEvent} from '../../cdk/testing/private';
|
5 | 5 | import {TAB, SPACE, ENTER} from '@angular/cdk/keycodes';
|
@@ -539,6 +539,26 @@ describe('MenuTrigger', () => {
|
539 | 539 | expect(fixture.componentInstance.trigger.isOpen()).toBeFalse();
|
540 | 540 | });
|
541 | 541 | });
|
| 542 | + |
| 543 | + it('should focus the first item when opening on click', fakeAsync(() => { |
| 544 | + TestBed.configureTestingModule({ |
| 545 | + imports: [CdkMenuModule], |
| 546 | + declarations: [TriggersWithSameMenuDifferentMenuBars], |
| 547 | + }).compileComponents(); |
| 548 | + |
| 549 | + const fixture = TestBed.createComponent(TriggersWithSameMenuDifferentMenuBars); |
| 550 | + fixture.detectChanges(); |
| 551 | + |
| 552 | + fixture.componentInstance.nativeTriggers.first.nativeElement.click(); |
| 553 | + fixture.detectChanges(); |
| 554 | + tick(); |
| 555 | + |
| 556 | + const firstItem = |
| 557 | + fixture.componentInstance.nativeMenus.first.nativeElement.querySelector('.cdk-menu-item'); |
| 558 | + |
| 559 | + expect(firstItem).toBeTruthy(); |
| 560 | + expect(document.activeElement).toBe(firstItem); |
| 561 | + })); |
542 | 562 | });
|
543 | 563 |
|
544 | 564 | @Component({
|
@@ -599,7 +619,10 @@ class MenuBarWithNestedSubMenus {
|
599 | 619 | })
|
600 | 620 | class TriggersWithSameMenuDifferentMenuBars {
|
601 | 621 | @ViewChildren(CdkMenuTrigger) triggers: QueryList<CdkMenuTrigger>;
|
| 622 | + @ViewChildren(CdkMenuTrigger, {read: ElementRef}) nativeTriggers: QueryList<ElementRef>; |
| 623 | + |
602 | 624 | @ViewChildren(CdkMenu) menus: QueryList<CdkMenu>;
|
| 625 | + @ViewChildren(CdkMenu, {read: ElementRef}) nativeMenus: QueryList<ElementRef>; |
603 | 626 | }
|
604 | 627 |
|
605 | 628 | @Component({
|
|
0 commit comments