Skip to content

Commit a458697

Browse files
committed
test(aria/menu): generate additional methods and tests for Menu harness
1 parent 5cb2692 commit a458697

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

goldens/aria/menu/testing/index.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class MenuHarness extends ComponentHarness {
1616
_getTrigger(): Promise<TestElement | null>;
1717
// (undocumented)
1818
static hostSelector: string;
19+
isMenuBar(): Promise<boolean>;
1920
isOpen(): Promise<boolean>;
2021
open(): Promise<void>;
2122
// (undocumented)
@@ -32,10 +33,12 @@ export class MenuItemHarness extends ComponentHarness {
3233
click(): Promise<void>;
3334
getSubmenu(): Promise<MenuHarness | null>;
3435
getText(): Promise<string>;
36+
hasSubmenu(): Promise<boolean>;
3537
// (undocumented)
3638
static hostSelector: string;
3739
isDisabled(): Promise<boolean>;
3840
isExpanded(): Promise<boolean>;
41+
isFocused(): Promise<boolean>;
3942
// (undocumented)
4043
static with(options?: MenuItemHarnessFilters): HarnessPredicate<MenuItemHarness>;
4144
}

src/aria/menu/testing/menu-harness.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,36 @@ describe('Aria Menu Harness', () => {
117117
expect(await items[0].getText()).toBe('File');
118118
expect(await items[1].getText()).toBe('Edit');
119119
});
120+
121+
it('should be able to get whether a menu item is focused', async () => {
122+
const menu = await loader.getHarness(MenuHarness.with({triggerText: 'Open Menu'}));
123+
await menu.open();
124+
const items = await menu.getItems();
125+
126+
const itemHost = await items[0].host();
127+
await itemHost.focus();
128+
fixture.detectChanges();
129+
130+
expect(await items[0].isFocused()).toBe(true);
131+
expect(await items[1].isFocused()).toBe(false);
132+
});
133+
134+
it('should be able to get whether a menu item has a submenu', async () => {
135+
const menu = await loader.getHarness(MenuHarness.with({triggerText: 'Open Menu'}));
136+
await menu.open();
137+
const items = await menu.getItems();
138+
139+
expect(await items[0].hasSubmenu()).toBe(false);
140+
expect(await items[2].hasSubmenu()).toBe(true);
141+
});
142+
143+
it('should be able to get whether a menu is a menu bar', async () => {
144+
const menubar = await loader.getHarness(MenuHarness.with({selector: '[ngMenuBar]'}));
145+
expect(await menubar.isMenuBar()).toBe(true);
146+
147+
const menu = await loader.getHarness(MenuHarness.with({triggerText: 'Open Menu'}));
148+
expect(await menu.isMenuBar()).toBe(false);
149+
});
120150
});
121151

122152
@Component({

src/aria/menu/testing/menu-harness.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ export class MenuItemHarness extends ComponentHarness {
6262
}
6363
return null;
6464
}
65+
66+
/** Whether the menu item has focus. */
67+
async isFocused(): Promise<boolean> {
68+
return (await this.host()).isFocused();
69+
}
70+
71+
/** Whether the menu item acts as a submenu trigger. */
72+
async hasSubmenu(): Promise<boolean> {
73+
const host = await this.host();
74+
return (
75+
(await host.getAttribute('aria-haspopup')) === 'true' ||
76+
!!(await host.getAttribute('aria-controls'))
77+
);
78+
}
6579
}
6680

6781
/** Harness for interacting with a standard ngMenu or ngMenuBar in tests. */
@@ -97,6 +111,12 @@ export class MenuHarness extends ComponentHarness {
97111
return (await host.getAttribute('data-visible')) === 'true';
98112
}
99113

114+
/** Whether the menu is a menu bar. */
115+
async isMenuBar(): Promise<boolean> {
116+
const host = await this.host();
117+
return host.matchesSelector('[ngMenuBar]');
118+
}
119+
100120
/** Opens the menu if it is currently closed. */
101121
async open(): Promise<void> {
102122
if (!(await this.isOpen())) {

0 commit comments

Comments
 (0)