mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
13 hours ago 7aeaa1d5b6254c380818bf094b8b84799d7b5c33
test(e2e): cover header submenus

Add child menu entries to the fixture site: a parent with its own page (children from configuration and front matter), a parent without a URL with a third-level entry, and a flat entry. Add a page with a featured image.

Test mouse and keyboard toggling, Escape, outside click, focus leaving, one open submenu at a time, aria-expanded and aria-current, the no-JavaScript fallback, and viewport containment from 320px to 1280px. Run axe on the header with closed and open submenus.
3 files modified
2 files added
334 ■■■■■ changed files
tests/e2e/accessibility.spec.ts 35 ●●●●● patch | view | raw | blame | history
tests/e2e/navigation.spec.ts 253 ●●●●● patch | view | raw | blame | history
tests/fixtures/site/content/featured.md 7 ●●●●● patch | view | raw | blame | history
tests/fixtures/site/content/posts/hello.md 4 ●●●●● patch | view | raw | blame | history
tests/fixtures/site/hugo.toml 35 ●●●●● patch | view | raw | blame | history
tests/e2e/accessibility.spec.ts
@@ -45,3 +45,38 @@
    expect(results.violations).toEqual([]);
  });
}
/**
 * Header submenus (tests/e2e/navigation.spec.ts): audit the header with every
 * submenu closed and with one submenu open, on a plain header and on a header
 * with a featured image.
 */
for (const path of ["/", "/featured/"]) {
  test(`header with closed submenus on ${path} has no automatically detectable a11y violations`, async ({
    page,
  }) => {
    await page.goto(path);
    await expect(page.locator(".ananke-submenu-toggle").first()).toBeVisible();
    const results = await new AxeBuilder({ page })
      .include("header")
      .withTags(TAGS)
      .analyze();
    expect(results.violations).toEqual([]);
  });
  for (const toggle of ["Posts submenu", "Topics"]) {
    test(`header with the open "${toggle}" button on ${path} has no automatically detectable a11y violations`, async ({
      page,
    }) => {
      await page.goto(path);
      const button = page.getByRole("button", { name: toggle });
      await button.click();
      await expect(button).toHaveAttribute("aria-expanded", "true");
      const results = await new AxeBuilder({ page })
        .include("header")
        .withTags(TAGS)
        .analyze();
      expect(results.violations).toEqual([]);
    });
  }
}
tests/e2e/navigation.spec.ts
New file
@@ -0,0 +1,253 @@
import { expect, type Page, test } from "@playwright/test";
/**
 * Two-level main menu (disclosure pattern) in the site header.
 *
 * Fixture menu (tests/fixtures/site/hugo.toml):
 *   Posts  - parent with its own page; children "Hello World" (front matter)
 *            and "Second Post" (config)
 *   Topics - parent without a URL; children "Categories" and "Tags", and
 *            "Alpha" one level below "Tags"
 *   About  - flat entry
 */
const nav = (page: Page) => page.getByRole("navigation");
const postsToggle = (page: Page) =>
  nav(page).getByRole("button", { name: "Posts submenu" });
const topicsToggle = (page: Page) =>
  nav(page).getByRole("button", { name: "Topics" });
const postsSubmenu = (page: Page) => page.locator("#ananke-submenu-0");
const topicsSubmenu = (page: Page) => page.locator("#ananke-submenu-1");
test.describe("header submenus", () => {
  test("toggle buttons are revealed and submenus start closed", async ({
    page,
  }) => {
    await page.goto("/");
    await expect(postsToggle(page)).toBeVisible();
    await expect(postsToggle(page)).toHaveAttribute("aria-expanded", "false");
    await expect(postsToggle(page)).toHaveAttribute(
      "aria-controls",
      "ananke-submenu-0",
    );
    await expect(postsSubmenu(page)).toBeHidden();
    // The parent with its own page keeps its link next to the button.
    await expect(
      nav(page).getByRole("link", { name: "Posts", exact: true }),
    ).toHaveAttribute("href", "/posts/");
    // The parent without a URL is only a button; its no-JS label is hidden.
    await expect(topicsToggle(page)).toBeVisible();
    await expect(page.locator(".ananke-submenu-label")).toBeHidden();
  });
  test("opens and closes with the mouse", async ({ page }) => {
    await page.goto("/");
    await postsToggle(page).click();
    await expect(postsToggle(page)).toHaveAttribute("aria-expanded", "true");
    await expect(postsSubmenu(page)).toBeVisible();
    await expect(
      postsSubmenu(page).getByRole("link", { name: "Hello World" }),
    ).toBeVisible();
    await postsToggle(page).click();
    await expect(postsToggle(page)).toHaveAttribute("aria-expanded", "false");
    await expect(postsSubmenu(page)).toBeHidden();
  });
  test("keeps only one submenu open", async ({ page }) => {
    await page.goto("/");
    await postsToggle(page).click();
    await topicsToggle(page).click();
    await expect(topicsSubmenu(page)).toBeVisible();
    await expect(postsSubmenu(page)).toBeHidden();
    await expect(postsToggle(page)).toHaveAttribute("aria-expanded", "false");
  });
  test("closes on a click outside", async ({ page }) => {
    await page.goto("/");
    await topicsToggle(page).click();
    await expect(topicsSubmenu(page)).toBeVisible();
    await page.locator("main").click({ position: { x: 5, y: 5 } });
    await expect(topicsSubmenu(page)).toBeHidden();
    await expect(topicsToggle(page)).toHaveAttribute("aria-expanded", "false");
  });
  test("works with the keyboard in reading order", async ({ page }) => {
    await page.goto("/");
    await nav(page).getByRole("link", { name: "Posts", exact: true }).focus();
    await page.keyboard.press("Tab");
    await expect(postsToggle(page)).toBeFocused();
    await page.keyboard.press("Enter");
    await expect(postsToggle(page)).toHaveAttribute("aria-expanded", "true");
    await page.keyboard.press("Tab");
    await expect(
      postsSubmenu(page).getByRole("link", { name: "Hello World" }),
    ).toBeFocused();
    // Escape closes the submenu and returns focus to its button.
    await page.keyboard.press("Escape");
    await expect(postsSubmenu(page)).toBeHidden();
    await expect(postsToggle(page)).toBeFocused();
    // Space toggles as well.
    await page.keyboard.press("Space");
    await expect(postsToggle(page)).toHaveAttribute("aria-expanded", "true");
    await page.keyboard.press("Space");
    await expect(postsToggle(page)).toHaveAttribute("aria-expanded", "false");
  });
  test("closes when focus leaves the submenu", async ({ page }) => {
    await page.goto("/");
    await postsToggle(page).focus();
    await page.keyboard.press("Enter");
    // Button -> Hello World -> Second Post -> next top-level item.
    await page.keyboard.press("Tab");
    await page.keyboard.press("Tab");
    await expect(
      postsSubmenu(page).getByRole("link", { name: "Second Post" }),
    ).toBeFocused();
    await page.keyboard.press("Tab");
    await expect(topicsToggle(page)).toBeFocused();
    await expect(postsSubmenu(page)).toBeHidden();
  });
  test("shows deeper levels as a nested list inside the submenu", async ({
    page,
  }) => {
    await page.goto("/");
    await topicsToggle(page).click();
    const alpha = topicsSubmenu(page).getByRole("link", { name: "Alpha" });
    await expect(alpha).toBeVisible();
    await expect(alpha).toHaveAttribute("href", "/tags/alpha/");
    // No toggle button below the second level.
    await expect(topicsSubmenu(page).getByRole("button")).toHaveCount(0);
  });
  test("shows a visible focus indicator on the toggle", async ({ page }) => {
    await page.goto("/");
    await postsToggle(page).focus();
    await page.keyboard.press("Shift+Tab");
    await page.keyboard.press("Tab");
    const outline = await postsToggle(page).evaluate(
      (el) => getComputedStyle(el).outlineStyle,
    );
    expect(outline).toBe("solid");
  });
});
// The submenus are closed here, so the lookups include hidden elements.
test.describe("current page markers", () => {
  test("marks the child page and its parent", async ({ page }) => {
    await page.goto("/posts/hello/");
    await expect(
      nav(page).getByRole("link", { name: "Posts", exact: true }),
    ).toHaveAttribute("aria-current", "true");
    await expect(
      postsSubmenu(page).getByRole("link", {
        name: "Hello World",
        includeHidden: true,
      }),
    ).toHaveAttribute("aria-current", "page");
    await expect(
      postsSubmenu(page).getByRole("link", {
        name: "Second Post",
        includeHidden: true,
      }),
    ).not.toHaveAttribute("aria-current", /.*/);
  });
  test("marks a parent page itself as the current page", async ({ page }) => {
    await page.goto("/posts/");
    await expect(
      nav(page).getByRole("link", { name: "Posts", exact: true }),
    ).toHaveAttribute("aria-current", "page");
  });
  test("marks a parent without a URL on its button", async ({ page }) => {
    await page.goto("/tags/alpha/");
    await expect(topicsToggle(page)).toHaveAttribute("aria-current", "true");
    await expect(
      topicsSubmenu(page).getByRole("link", {
        name: "Alpha",
        includeHidden: true,
      }),
    ).toHaveAttribute("aria-current", "page");
  });
  test("marks a flat entry", async ({ page }) => {
    await page.goto("/about/");
    await expect(
      nav(page).getByRole("link", { name: "About" }),
    ).toHaveAttribute("aria-current", "page");
  });
});
for (const width of [320, 375, 768, 1280]) {
  for (const path of ["/", "/featured/"]) {
    test(`submenus stay in the viewport and above the header at ${width}px on ${path}`, async ({
      page,
    }) => {
      await page.setViewportSize({ width, height: 800 });
      await page.goto(path);
      for (const [toggle, submenu] of [
        [postsToggle(page), postsSubmenu(page)],
        [topicsToggle(page), topicsSubmenu(page)],
      ] as const) {
        await toggle.click();
        await expect(submenu).toBeVisible();
        const box = await submenu.boundingBox();
        expect(box).not.toBeNull();
        if (!box) continue;
        expect(box.x).toBeGreaterThanOrEqual(0);
        expect(box.x + box.width).toBeLessThanOrEqual(width);
        // The submenu is painted above the header text below it.
        const onTop = await submenu.evaluate((el) => {
          const r = el.getBoundingClientRect();
          const hit = document.elementFromPoint(
            r.left + r.width / 2,
            r.bottom - 4,
          );
          return el.contains(hit);
        });
        expect(onTop).toBe(true);
      }
    });
  }
}
test.describe("without JavaScript (progressive enhancement)", () => {
  test.use({ javaScriptEnabled: false });
  test("shows all child links and no toggle buttons", async ({ page }) => {
    await page.goto("/");
    await expect(page.locator(".ananke-submenu-toggle")).toHaveCount(2);
    for (const toggle of await page.locator(".ananke-submenu-toggle").all()) {
      await expect(toggle).toBeHidden();
    }
    for (const name of [
      "Hello World",
      "Second Post",
      "Categories",
      "Tags",
      "Alpha",
    ]) {
      await expect(nav(page).getByRole("link", { name })).toBeVisible();
    }
    // The parent without a URL shows its name as text.
    await expect(page.locator(".ananke-submenu-label")).toHaveText("Topics");
    await expect(page.locator(".ananke-submenu-label")).toBeVisible();
  });
  test("keeps child links reachable at a narrow width", async ({ page }) => {
    await page.setViewportSize({ width: 320, height: 800 });
    await page.goto("/");
    const alpha = nav(page).getByRole("link", { name: "Alpha" });
    await expect(alpha).toBeVisible();
    const box = await alpha.boundingBox();
    expect(box).not.toBeNull();
    if (box) expect(box.x + box.width).toBeLessThanOrEqual(320);
  });
});
tests/fixtures/site/content/featured.md
New file
@@ -0,0 +1,7 @@
---
title: "Featured Image"
description: "A page with a featured image, for the header with a background image."
featured_image: "/images/gohugo-default-sample-hero-image.jpg"
---
A standalone page whose header uses a featured image. The navigation tests open submenus over this header.
tests/fixtures/site/content/posts/hello.md
@@ -4,6 +4,10 @@
description: "A post with a fenced code block for the copy-button tests."
categories: ["News"]
tags: ["alpha", "beta"]
menus:
  main:
    parent: posts
    weight: 10
---
A post body with a fenced code block, used by the copy-button E2E tests.
tests/fixtures/site/hugo.toml
@@ -15,9 +15,44 @@
show_categories = true
copy_code = true
# Main menu with submenus (tests/e2e/navigation.spec.ts):
#   Posts  - parent with its own page; children from config and front matter
#   Topics - parent without a URL; Tags has a third-level child
#   About  - flat entry
[[menus.main]]
name = "Posts"
pageRef = "/posts/"
identifier = "posts"
weight = 10
[[menus.main]]
name = "Second Post"
pageRef = "/posts/second/"
parent = "posts"
weight = 20
[[menus.main]]
name = "Topics"
identifier = "topics"
weight = 15
[[menus.main]]
name = "Categories"
pageRef = "/categories/"
parent = "topics"
weight = 10
[[menus.main]]
name = "Tags"
pageRef = "/tags/"
identifier = "tags"
parent = "topics"
weight = 20
[[menus.main]]
name = "Alpha"
pageRef = "/tags/alpha/"
parent = "tags"
weight = 10
[[menus.main]]