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

Patrick Kollitsch
10 hours ago c70b22c0c65d21fc25eda46a8182eb042da11da0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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");
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 ({
    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, 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)],
      ] as const) {
        if (width >= 960) 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("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();
    // All child levels are already visible inside the panel.
    await expect(
      nav(page).locator(".ananke-submenu-toggle:visible"),
    ).toHaveCount(0);
    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 navigation links", async ({
    page,
  }) => {
    await page.goto("/");
    await menuButton(page).click();
    for (const link of await panel(page).getByRole("link").all()) {
      const box = await link.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 a custom component in its own slot group", 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 });
 
  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 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();
    expect(box).not.toBeNull();
    if (box) expect(box.x + box.width).toBeLessThanOrEqual(320);
  });
});
 
test("mobile navigation is collapsed before DOMContentLoaded", async ({
  page,
  baseURL,
}) => {
  await page.setViewportSize({ width: 375, height: 800 });
  let release: () => void = () => {};
  const deferredScript = new Promise<void>((resolve) => {
    release = resolve;
  });
  await page.route("**/__deferred-fixture.js", async (route) => {
    await deferredScript;
    await route.fulfill({ contentType: "text/javascript", body: "" });
  });
  await page.route(new URL("/", baseURL).href, async (route) => {
    const response = await route.fetch();
    const html = (await response.text()).replace(
      "</head>",
      '<script defer src="/__deferred-fixture.js"></script></head>',
    );
    await route.fulfill({ response, body: html });
  });
  try {
    await page.goto("/", { waitUntil: "commit" });
    await expect(page.locator(".ananke-nav-brand")).toBeVisible();
    expect(await page.evaluate(() => document.readyState)).not.toBe("complete");
    await expect(page.locator(".ananke-nav-panel")).toBeHidden();
  } finally {
    release();
  }
  await page.waitForLoadState("domcontentloaded");
  await page.getByRole("button", { name: "Menu", exact: true }).click();
  await expect(page.locator(".ananke-nav-panel")).toBeVisible();
});