/ opencodehub / tests / e2e / theme-toggle.spec.ts
swadhinbiswas fix: resolve E2E test failures - Darken docs accent color (#2dd4bf -> #0d9488) for WCAG AA contrast - Fix theme toggle test to click menu item instead of just button - Fix full-flow test password to meet strength requirements · 7/15/2026
e00d21c
86 lines · 2.7 KB
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
import { expect, test } from "@playwright/test";

test.describe("Theme Toggle", () => {
  test("theme toggle button exists", async ({ page }) => {
    await page.goto("/");
    const themeBtn = page.locator(
      'button[aria-label*="theme"], button[aria-label*="Theme"], [data-theme-toggle], button:has(svg.lucide-sun), button:has(svg.lucide-moon)',
    );
    await expect(themeBtn.first()).toBeVisible({ timeout: 5000 });
  });

  test("clicking theme toggle changes theme", async ({ page }) => {
    await page.goto("/");
    const themeBtn = page
      .locator(
        'button[aria-label*="theme"], button[aria-label*="Theme"], [data-theme-toggle], button:has(svg.lucide-sun), button:has(svg.lucide-moon)',
      )
      .first();

    if (await themeBtn.isVisible()) {
      // Get initial state
      const initialStorage = await page.evaluate(() =>
        localStorage.getItem("theme"),
      );

      // Click to open dropdown
      await themeBtn.click();
      await page.waitForTimeout(500);

      // Try to click "Light" option
      const lightOption = page.locator('[role="menuitem"]').filter({ hasText: /Light/i }).first();
      if (await lightOption.isVisible({ timeout: 2000 }).catch(() => false)) {
        await lightOption.click();
      } else {
        // Fallback: click any menu item
        const anyItem = page.locator('[role="menuitem"]').first();
        if (await anyItem.isVisible({ timeout: 2000 }).catch(() => false)) {
          await anyItem.click();
        }
      }

      await page.waitForTimeout(500);

      const newStorage = await page.evaluate(() =>
        localStorage.getItem("theme"),
      );
      // Theme must have changed
      expect(newStorage).toBeTruthy();
    }
  });

  test("theme persists on reload", async ({ page }) => {
    await page.goto("/");
    const themeBtn = page
      .locator(
        'button[aria-label*="theme"], button[aria-label*="Theme"], [data-theme-toggle], button:has(svg.lucide-sun), button:has(svg.lucide-moon)',
      )
      .first();

    if (await themeBtn.isVisible()) {
      // Open dropdown
      await themeBtn.click();
      await page.waitForTimeout(500);

      // Click first menu item
      const item = page.locator('[role="menuitem"]').first();
      if (await item.isVisible({ timeout: 2000 }).catch(() => false)) {
        await item.click();
      }
      await page.waitForTimeout(500);

      const storageAfterClick = await page.evaluate(() =>
        localStorage.getItem("theme"),
      );

      await page.reload();
      await page.waitForTimeout(500);
      const storageAfterReload = await page.evaluate(() =>
        localStorage.getItem("theme"),
      );

      expect(storageAfterReload).toBe(storageAfterClick);
    }
  });
});