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

Patrick Kollitsch
13 hours ago 0eb3f08f5bdc38ec9002d90feb8eb54db7aef4de
test(e2e): cover the menu button, hook slot, and navigation classes

Add a search form in the navigation-start hook and a configured menu button class to the fixture site. Test the menu button below 60em (toggle, Escape, focus return, 44px submenu targets), its absence on large screens, the class param, the hook slot, inline submenus in the viewport tests, and axe with the open menu at 375px.
3 files modified
1 files added
123 ■■■■■ changed files
tests/e2e/accessibility.spec.ts 14 ●●●●● patch | view | raw | blame | history
tests/e2e/navigation.spec.ts 100 ●●●●● patch | view | raw | blame | history
tests/fixtures/site/hugo.toml 4 ●●●●● patch | view | raw | blame | history
tests/fixtures/site/layouts/_partials/hooks/navigation-start.html 5 ●●●●● patch | view | raw | blame | history
tests/e2e/accessibility.spec.ts
@@ -80,3 +80,17 @@
    });
  }
}
test("header with the open menu and an open submenu at 375px has no automatically detectable a11y violations", async ({
  page,
}) => {
  await page.setViewportSize({ width: 375, height: 800 });
  await page.goto("/");
  await page.getByRole("button", { name: "Menu", exact: true }).click();
  await page.getByRole("button", { name: "Topics" }).click();
  const results = await new AxeBuilder({ page })
    .include("header")
    .withTags(TAGS)
    .analyze();
  expect(results.violations).toEqual([]);
});
tests/e2e/navigation.spec.ts
@@ -18,6 +18,9 @@
  nav(page).getByRole("button", { name: "Topics" });
const postsSubmenu = (page: Page) => page.locator("#ananke-submenu-0");
const topicsSubmenu = (page: Page) => page.locator("#ananke-submenu-1");
const menuButton = (page: Page) =>
  nav(page).getByRole("button", { name: "Menu", exact: true });
const panel = (page: Page) => page.locator("#ananke-nav-panel");
test.describe("header submenus", () => {
  test("toggle buttons are revealed and submenus start closed", async ({
@@ -185,13 +188,15 @@
  });
});
for (const width of [320, 375, 768, 1280]) {
for (const width of [320, 375, 768, 1024, 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);
      // Below 60em (960px) the menu is behind the menu button.
      if (width < 960) await menuButton(page).click();
      for (const [toggle, submenu] of [
        [postsToggle(page), postsSubmenu(page)],
        [topicsToggle(page), topicsSubmenu(page)],
@@ -218,6 +223,93 @@
  }
}
test.describe("menu button below the large breakpoint", () => {
  test.use({ viewport: { width: 375, height: 800 } });
  test("collapses the menu behind the button", async ({ page }) => {
    await page.goto("/");
    await expect(menuButton(page)).toBeVisible();
    await expect(menuButton(page)).toHaveAttribute("aria-expanded", "false");
    await expect(menuButton(page)).toHaveAttribute(
      "aria-controls",
      "ananke-nav-panel",
    );
    await expect(panel(page)).toBeHidden();
    // The brand link and the search slot stay visible.
    await expect(
      nav(page).getByRole("link", { name: "Ananke Test Fixture" }),
    ).toBeVisible();
    await expect(page.getByRole("searchbox", { name: "Search" })).toBeVisible();
  });
  test("opens and closes the menu", async ({ page }) => {
    await page.goto("/");
    await menuButton(page).click();
    await expect(menuButton(page)).toHaveAttribute("aria-expanded", "true");
    await expect(panel(page)).toBeVisible();
    await expect(nav(page).getByRole("link", { name: "About" })).toBeVisible();
    // Submenus open inline inside the panel.
    await topicsToggle(page).click();
    await expect(
      topicsSubmenu(page).getByRole("link", { name: "Categories" }),
    ).toBeVisible();
    await menuButton(page).click();
    await expect(menuButton(page)).toHaveAttribute("aria-expanded", "false");
    await expect(panel(page)).toBeHidden();
  });
  test("closes on Escape and returns focus to the button", async ({ page }) => {
    await page.goto("/");
    await menuButton(page).focus();
    await page.keyboard.press("Enter");
    await expect(panel(page)).toBeVisible();
    await page.keyboard.press("Tab");
    await page.keyboard.press("Escape");
    await expect(panel(page)).toBeHidden();
    await expect(menuButton(page)).toBeFocused();
  });
  test("gives touch targets of at least 44px to submenu buttons", async ({
    page,
  }) => {
    await page.goto("/");
    await menuButton(page).click();
    for (const toggle of [postsToggle(page), topicsToggle(page)]) {
      const box = await toggle.boundingBox();
      expect(box?.height ?? 0).toBeGreaterThanOrEqual(44);
    }
  });
});
test.describe("menu button on large screens", () => {
  test("is hidden and the menu is visible", async ({ page }) => {
    await page.goto("/");
    await expect(menuButton(page)).toBeHidden();
    await expect(panel(page)).toBeVisible();
  });
});
test.describe("customisation", () => {
  test("uses classes from [params.ananke.navigation.classes]", async ({
    page,
  }) => {
    await page.goto("/");
    // The fixture replaces the classes of the menu button.
    await expect(page.locator(".ananke-nav-toggle")).toHaveClass(
      /\bfixture-menu-button\b/,
    );
    // Other elements keep their default Tachyons classes.
    await expect(page.locator(".ananke-nav-brand")).toHaveClass(/\bf3\b/);
  });
  test("renders the navigation-start hook in the slot", async ({ page }) => {
    await page.goto("/");
    await expect(
      page.locator(".ananke-nav-slot").getByRole("search"),
    ).toBeVisible();
  });
});
test.describe("without JavaScript (progressive enhancement)", () => {
  test.use({ javaScriptEnabled: false });
@@ -241,9 +333,13 @@
    await expect(page.locator(".ananke-submenu-label")).toBeVisible();
  });
  test("keeps child links reachable at a narrow width", async ({ page }) => {
  test("keeps the menu and child links reachable at a narrow width", async ({
    page,
  }) => {
    await page.setViewportSize({ width: 320, height: 800 });
    await page.goto("/");
    await expect(page.locator(".ananke-nav-toggle")).toBeHidden();
    await expect(panel(page)).toBeVisible();
    const alpha = nav(page).getByRole("link", { name: "Alpha" });
    await expect(alpha).toBeVisible();
    const box = await alpha.boundingBox();
tests/fixtures/site/hugo.toml
@@ -15,6 +15,10 @@
show_categories = true
copy_code = true
# Replace the default classes of one navigation element (tests/e2e/navigation.spec.ts).
[params.ananke.navigation.classes]
toggle = "f5 white-90 hover-white fixture-menu-button"
# 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
tests/fixtures/site/layouts/_partials/hooks/navigation-start.html
New file
@@ -0,0 +1,5 @@
{{- /* Test fixture: a search form in the navigation-start hook slot. */ -}}
<form class="fixture-search" role="search" action="{{ "posts/" | relURL }}">
  <label class="clip" for="fixture-search-input">Search</label>
  <input id="fixture-search-input" class="input-reset w-100 f6 pv2 ph3 br2 ba b--white-30 bg-white-10 white" type="search" name="q" placeholder="Search">
</form>