Skip to content

Commit

Permalink
test(menu): add e2e tests for keyboard support for horizontal & verti…
Browse files Browse the repository at this point in the history
…cal layout (#6827)

**Related Issue:** #6531

## Summary
Add's e2e test for keyboard support in menu for both horizontal and
vertical layout
  • Loading branch information
anveshmekala authored Apr 21, 2023
1 parent d38ad53 commit 6daadc7
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 61 deletions.
61 changes: 0 additions & 61 deletions src/components/menu-item/menu-item.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,65 +46,4 @@ describe("calcite-menu-item", () => {
});

it("is focusable", () => focusable("calcite-menu-item"));

describe("mouse support", () => {
it("should open the submenu on click", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-menu>
<calcite-menu-item id="ArcGISOnline" text="ArcGISOnline">
<calcite-menu-item id="ArcGISJS" text="ArcGISJS" slot="menu-item-dropdown">
<calcite-menu-item text="API" id="API" slot="menu-item-dropdown"></calcite-menu-item>
<calcite-menu-item text="Widgets" id="Widgets" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
<calcite-menu-item text="Calcite" id="Calcite" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
</calcite-menu>`);

const menuItem = await page.find("calcite-menu-item[id='ArcGISOnline']");
const menuItemMenu = await page.find("calcite-menu-item[id='ArcGISOnline'] >>> calcite-menu");

expect(await menuItemMenu.isVisible()).toBe(false);

await menuItem.click();
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(true);
expect(await page.evaluate(() => document.activeElement.id)).toBe("ArcGISOnline");

const subMenuItem = await page.find("calcite-menu-item[text='ArcGISJS']");
const subMenuItemMenu = await page.find("calcite-menu-item[text='ArcGISJS'] >>> calcite-menu");
expect(await subMenuItemMenu.isVisible()).toBe(false);
await subMenuItem.click();
await page.waitForChanges();
expect(await subMenuItemMenu.isVisible()).toBe(true);
expect(await page.evaluate(() => document.activeElement.id)).toBe("ArcGISJS");
});

it("should close any opened submenu when clicked outside", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-menu>
<calcite-menu-item id="ArcGISOnline" text="ArcGISOnline">
<calcite-menu-item text="ArcGISJS" slot="menu-item-dropdown"> </calcite-menu-item>
<calcite-menu-item text="Calcite" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
</calcite-menu>`);

const menuItem = await page.find("calcite-menu-item[id='ArcGISOnline']");
const menuItemMenu = await page.find("calcite-menu-item[id='ArcGISOnline'] >>> calcite-menu");

expect(await menuItemMenu.isVisible()).toBe(false);

await menuItem.click();
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(true);
expect(await page.evaluate(() => document.activeElement.id)).toBe("ArcGISOnline");

const menuElement = await page.$("calcite-menu");
const { x, y, width, height } = await menuElement.boundingBox();

await page.mouse.click(x + width + 50, y + height + 50);
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(false);
expect(await page.evaluate(() => document.activeElement.id)).not.toBe("ArcGISOnline");
});
});
});
215 changes: 215 additions & 0 deletions src/components/menu/menu.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { newE2EPage } from "@stencil/core/testing";
import { html } from "../../../support/formatting";
import { accessible, hidden, renders } from "../../tests/commonTests";

describe("calcite-menu", () => {
Expand All @@ -6,4 +8,217 @@ describe("calcite-menu", () => {
it("honors hidden attribute", async () => hidden("calcite-menu"));

it("is accessible", () => accessible("calcite-menu"));

describe("mouse support", () => {
it("should open the submenu on click", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-menu>
<calcite-menu-item id="ArcGISOnline" text="ArcGISOnline">
<calcite-menu-item id="ArcGISJS" text="ArcGISJS" slot="menu-item-dropdown">
<calcite-menu-item text="API" id="API" slot="menu-item-dropdown"></calcite-menu-item>
<calcite-menu-item text="Widgets" id="Widgets" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
<calcite-menu-item text="Calcite" id="Calcite" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
</calcite-menu>`);

const menuItem = await page.find("calcite-menu-item[id='ArcGISOnline']");
const menuItemMenu = await page.find("calcite-menu-item[id='ArcGISOnline'] >>> calcite-menu");

expect(await menuItemMenu.isVisible()).toBe(false);

await menuItem.click();
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(true);
expect(await page.evaluate(() => document.activeElement.id)).toBe("ArcGISOnline");

const subMenuItem = await page.find("calcite-menu-item[text='ArcGISJS']");
const subMenuItemMenu = await page.find("calcite-menu-item[text='ArcGISJS'] >>> calcite-menu");
expect(await subMenuItemMenu.isVisible()).toBe(false);
await subMenuItem.click();
await page.waitForChanges();
expect(await subMenuItemMenu.isVisible()).toBe(true);
expect(await page.evaluate(() => document.activeElement.id)).toBe("ArcGISJS");
});

it("should close any opened submenu when clicked outside", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-menu>
<calcite-menu-item id="ArcGISOnline" text="ArcGISOnline">
<calcite-menu-item text="ArcGISJS" slot="menu-item-dropdown"> </calcite-menu-item>
<calcite-menu-item text="Calcite" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
</calcite-menu>`);

const menuItem = await page.find("calcite-menu-item[id='ArcGISOnline']");
const menuItemMenu = await page.find("calcite-menu-item[id='ArcGISOnline'] >>> calcite-menu");

expect(await menuItemMenu.isVisible()).toBe(false);

await menuItem.click();
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(true);
expect(await page.evaluate(() => document.activeElement.id)).toBe("ArcGISOnline");

const menuElement = await page.$("calcite-menu");
const { x, y, width, height } = await menuElement.boundingBox();

await page.mouse.click(x + width + 50, y + height + 50);
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(false);
expect(await page.evaluate(() => document.activeElement.id)).not.toBe("ArcGISOnline");
});
});

describe("keyboard support", () => {
it("should open and close horizontal calcite-menu", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-menu>
<calcite-menu-item id="Nature" text="Nature" href="#arcgisonline">
<calcite-menu-item id="Mountains" text="Mountains" slot="menu-item-dropdown">
<calcite-menu-item
text="Rocky Mountains"
id="Rocky Mountains"
slot="menu-item-dropdown"
></calcite-menu-item>
<calcite-menu-item text="Smoky Mountains" id="Smoky Mountains" slot="menu-item-dropdown">
</calcite-menu-item>
</calcite-menu-item>
<calcite-menu-item text="Rivers" id="Rivers" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
<calcite-menu-item id="Planets" text="Planets"> </calcite-menu-item>
</calcite-menu>`);

const menuItemMenu = await page.find("calcite-menu-item[id='Nature'] >>> calcite-menu");
const subMenuItemMenu = await page.find("calcite-menu-item[id='Mountains'] >>> calcite-menu");
expect(await menuItemMenu.isVisible()).toBe(false);

await page.keyboard.press("Tab");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");

await page.keyboard.press("Enter");
expect(await menuItemMenu.isVisible()).toBe(false);

await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(true);

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Mountains");

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rivers");

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rivers");

await page.keyboard.press("ArrowUp");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Mountains");

expect(await subMenuItemMenu.isVisible()).toBe(false);
await page.keyboard.press("Enter");
expect(await subMenuItemMenu.isVisible()).toBe(true);

await page.keyboard.press("ArrowRight");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rocky Mountains");

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Smoky Mountains");

await page.keyboard.press("ArrowUp");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rocky Mountains");

await page.keyboard.press("ArrowLeft");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Mountains");
expect(await subMenuItemMenu.isVisible()).toBe(false);
expect(await menuItemMenu.isVisible()).toBe(true);

await page.keyboard.press("ArrowLeft");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");
expect(await menuItemMenu.isVisible()).toBe(false);

await page.keyboard.press("ArrowRight");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Planets");

await page.keyboard.press("ArrowLeft");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");
});

it("should open and close vertical calcite-menu", async () => {
const page = await newE2EPage();
await page.setContent(html`<calcite-menu layout="vertical">
<calcite-menu-item id="Nature" text="Nature" href="#arcgisonline">
<calcite-menu-item id="Mountains" text="Mountains" slot="menu-item-dropdown">
<calcite-menu-item
text="Rocky Mountains"
id="Rocky Mountains"
slot="menu-item-dropdown"
></calcite-menu-item>
<calcite-menu-item text="Smoky Mountains" id="Smoky Mountains" slot="menu-item-dropdown">
</calcite-menu-item>
</calcite-menu-item>
<calcite-menu-item text="Rivers" id="Rivers" slot="menu-item-dropdown"> </calcite-menu-item>
</calcite-menu-item>
<calcite-menu-item id="Planets" text="Planets"> </calcite-menu-item>
</calcite-menu>`);

const menuItemMenu = await page.find("calcite-menu-item[id='Nature'] >>> calcite-menu");
const subMenuItemMenu = await page.find("calcite-menu-item[id='Mountains'] >>> calcite-menu");
expect(await menuItemMenu.isVisible()).toBe(false);

await page.keyboard.press("Tab");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");

await page.keyboard.press("Enter");
expect(await menuItemMenu.isVisible()).toBe(false);

await page.keyboard.press("Tab");
await page.keyboard.press("Enter");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");
await page.waitForChanges();
expect(await menuItemMenu.isVisible()).toBe(true);

await page.keyboard.press("ArrowRight");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Mountains");

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rivers");

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rivers");

await page.keyboard.press("ArrowUp");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Mountains");

expect(await subMenuItemMenu.isVisible()).toBe(false);
await page.keyboard.press("Enter");
expect(await subMenuItemMenu.isVisible()).toBe(true);

await page.keyboard.press("ArrowRight");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rocky Mountains");

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Smoky Mountains");

await page.keyboard.press("ArrowUp");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Rocky Mountains");

await page.keyboard.press("ArrowLeft");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Mountains");
expect(await subMenuItemMenu.isVisible()).toBe(false);
expect(await menuItemMenu.isVisible()).toBe(true);

await page.keyboard.press("ArrowLeft");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");
expect(await menuItemMenu.isVisible()).toBe(false);

await page.keyboard.press("ArrowDown");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Planets");

await page.keyboard.press("ArrowUp");
expect(await page.evaluate(() => document.activeElement.id)).toBe("Nature");
});
});
});
30 changes: 30 additions & 0 deletions src/components/menu/menu.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,33 @@ export const withNesting = (): string => html`<calcite-panel>
</calcite-menu-item>
<calcite-menu-item text="Example nav item 4" text-enabled></calcite-menu-item> </calcite-menu
></calcite-panel>`;

export const WithSubMenuOpen_TestOnly = (): string => html`<calcite-nav
><calcite-menu slot="primary-content-center">
<calcite-menu-item
text="My nav item"
href="#mynav"
/>
<calcite-menu-item
text="item1"
active
>
</calcite-menu-item>
</calcite-menu-item>
</calcite-menu
></calcite-nav>`;

export const WithSubMenuOpenInVerticalLayout_TestOnly = (): string => html`<calcite-nav
><calcite-menu slot="primary-content-center" layout="vertical">
<calcite-menu-item
text="My nav item"
href="#mynav"
/>
<calcite-menu-item
text="item1"
active
>
</calcite-menu-item>
</calcite-menu-item>
</calcite-menu
></calcite-nav>`;
1 change: 1 addition & 0 deletions src/components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class CalciteMenu {
const parentEl = el.parentElement as HTMLCalciteMenuItemElement;
if (parentEl) {
focusElement(parentEl);
parentEl.open = false;
}
};

Expand Down

0 comments on commit 6daadc7

Please sign in to comment.