Skip to content

Commit 530fd07

Browse files
committed
fixup! fix(cdk-experimental/menu): refocus trigger after closeAll
1 parent c8eb797 commit 530fd07

File tree

2 files changed

+57
-59
lines changed

2 files changed

+57
-59
lines changed

src/cdk-experimental/menu/menu-bar.spec.ts

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
import {ComponentFixture, TestBed, waitForAsync, fakeAsync, tick} from '@angular/core/testing';
1+
import {ComponentFixture, fakeAsync, TestBed, tick, waitForAsync} from '@angular/core/testing';
22
import {
33
Component,
4-
ViewChild,
54
ElementRef,
6-
ViewChildren,
7-
QueryList,
85
EventEmitter,
6+
QueryList,
7+
ViewChild,
8+
ViewChildren,
99
} from '@angular/core';
1010
import {By} from '@angular/platform-browser';
1111
import {
12-
TAB,
13-
RIGHT_ARROW,
14-
LEFT_ARROW,
12+
D,
1513
DOWN_ARROW,
16-
UP_ARROW,
17-
SPACE,
18-
HOME,
19-
END,
2014
E,
21-
D,
15+
END,
2216
ESCAPE,
23-
S,
2417
H,
18+
HOME,
19+
LEFT_ARROW,
20+
RIGHT_ARROW,
21+
S,
22+
SPACE,
23+
TAB,
24+
UP_ARROW,
2525
} from '@angular/cdk/keycodes';
2626
import {
27-
dispatchKeyboardEvent,
2827
createKeyboardEvent,
2928
dispatchEvent,
29+
dispatchKeyboardEvent,
3030
dispatchMouseEvent,
31+
triggerFocus,
3132
} from '../../cdk/testing/private';
3233
import {CdkMenuBar} from './menu-bar';
3334
import {CdkMenuModule} from './menu-module';
@@ -802,7 +803,7 @@ describe('MenuBar', () => {
802803

803804
/** set the menus and triggers arrays. */
804805
function grabElementsForTesting() {
805-
popoutMenus = fixture.componentInstance.menus.toArray().filter(el => !el._isInline());
806+
popoutMenus = fixture.componentInstance.menus.toArray().filter(el => !el._isInline);
806807
triggers = fixture.componentInstance.triggers.toArray();
807808
nativeInlineMenuItem = fixture.componentInstance.nativeInlineMenuItem.nativeElement;
808809
}
@@ -923,6 +924,7 @@ describe('MenuBar', () => {
923924
dispatchMouseEvent(menuBarNativeItems[0], 'mouseenter');
924925
dispatchMouseEvent(menuBarNativeItems[0], 'click');
925926
dispatchMouseEvent(menuBarNativeItems[0], 'mouseenter');
927+
triggerFocus(menuBarNativeItems[0]);
926928
detectChanges();
927929
}
928930

@@ -1063,19 +1065,17 @@ describe('MenuBar', () => {
10631065
expect(nativeMenus.length).toBe(0);
10641066
});
10651067

1066-
it(
1067-
'should allow keyboard down arrow to focus next item after mouse sets focus to' +
1068-
' initial item',
1069-
() => {
1070-
openFileMenu();
1071-
dispatchMouseEvent(fileMenuNativeItems[0], 'mouseenter');
1072-
detectChanges();
1068+
it('should allow keyboard down arrow to focus next item after focusing initial item', () => {
1069+
openFileMenu();
10731070

1074-
dispatchKeyboardEvent(nativeMenus[0], 'keydown', DOWN_ARROW);
1071+
dispatchKeyboardEvent(nativeMenus[0], 'keydown', DOWN_ARROW);
1072+
detectChanges();
1073+
expect(document.activeElement).toEqual(fileMenuNativeItems[0]);
10751074

1076-
expect(document.activeElement).toEqual(fileMenuNativeItems[1]);
1077-
},
1078-
);
1075+
dispatchKeyboardEvent(nativeMenus[0], 'keydown', DOWN_ARROW);
1076+
detectChanges();
1077+
expect(document.activeElement).toEqual(fileMenuNativeItems[1]);
1078+
});
10791079

10801080
it(
10811081
'should not re-open a menu when hovering over the trigger in the menubar after clicking to ' +
@@ -1103,49 +1103,41 @@ describe('MenuBar', () => {
11031103
},
11041104
);
11051105

1106-
it(
1107-
'should set the tabindex of the opened trigger to 0 and toggle tabindex' +
1108-
' when hovering between items',
1109-
() => {
1110-
openFileMenu();
1106+
it('should not change the tabindex when hovering between items', () => {
1107+
openFileMenu();
11111108

1112-
expect(menuBarNativeItems[0].tabIndex).toBe(0);
1109+
expect(menuBarNativeItems[0].tabIndex).toBe(0);
11131110

1114-
dispatchMouseEvent(menuBarNativeItems[1], 'mouseenter');
1115-
detectChanges();
1111+
dispatchMouseEvent(menuBarNativeItems[1], 'mouseenter');
1112+
detectChanges();
11161113

1117-
expect(menuBarNativeItems[0].tabIndex).toBe(-1);
1118-
expect(menuBarNativeItems[1].tabIndex).toBe(0);
1114+
expect(menuBarNativeItems[0].tabIndex).toBe(0);
1115+
expect(menuBarNativeItems[1].tabIndex).toBe(-1);
11191116

1120-
dispatchMouseEvent(menuBarNativeItems[0], 'mouseenter');
1121-
detectChanges();
1117+
dispatchMouseEvent(menuBarNativeItems[0], 'mouseenter');
1118+
detectChanges();
11221119

1123-
expect(menuBarNativeItems[0].tabIndex).toBe(0);
1124-
expect(menuBarNativeItems[1].tabIndex).toBe(-1);
1125-
},
1126-
);
1120+
expect(menuBarNativeItems[0].tabIndex).toBe(0);
1121+
expect(menuBarNativeItems[1].tabIndex).toBe(-1);
1122+
});
11271123

1128-
it(
1129-
'should set the tabindex to 0 on the active item and reset the previous active items ' +
1130-
'to -1 when navigating down to a submenu and within it using a mouse',
1131-
() => {
1132-
openFileMenu();
1133-
expect(menuBarNativeItems[0].tabIndex).toBe(0);
1124+
it('should not change the tabindex when navigating down to a submenu and within it using a mouse', () => {
1125+
openFileMenu();
1126+
expect(menuBarNativeItems[0].tabIndex).toBe(0);
11341127

1135-
dispatchMouseEvent(fileMenuNativeItems[0], 'mouseenter');
1136-
dispatchMouseEvent(menuBarNativeItems[0], 'mouseout');
1137-
detectChanges();
1128+
dispatchMouseEvent(fileMenuNativeItems[0], 'mouseenter');
1129+
dispatchMouseEvent(menuBarNativeItems[0], 'mouseout');
1130+
detectChanges();
11381131

1139-
expect(menuBarNativeItems[0].tabIndex).toBe(-1);
1140-
expect(fileMenuNativeItems[0].tabIndex).toBe(0);
1132+
expect(menuBarNativeItems[0].tabIndex).toBe(0);
1133+
expect(fileMenuNativeItems[0].tabIndex).toBe(-1);
11411134

1142-
dispatchMouseEvent(fileMenuNativeItems[1], 'mouseenter');
1143-
detectChanges();
1135+
dispatchMouseEvent(fileMenuNativeItems[1], 'mouseenter');
1136+
detectChanges();
11441137

1145-
expect(fileMenuNativeItems[0].tabIndex).toBe(-1);
1146-
expect(fileMenuNativeItems[1].tabIndex).toBe(0);
1147-
},
1148-
);
1138+
expect(fileMenuNativeItems[0].tabIndex).toBe(-1);
1139+
expect(fileMenuNativeItems[1].tabIndex).toBe(-1);
1140+
});
11491141
});
11501142
});
11511143

src/dev-app/cdk-experimental-menu/cdk-menu-demo.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ <h3>Inline Menu Example</h3>
7575
</div>
7676
</div>
7777

78+
<div class="example-menu-container">
79+
<h3>Standalone Menu Example</h3>
80+
81+
<p><button [cdkMenuTriggerFor]="someMenu">Standalone Menu</button></p>
82+
</div>
83+
7884
<div class="example-menu-container">
7985
<h3>Context Menu Example</h3>
8086

0 commit comments

Comments
 (0)