/ opencodehub / tests / e2e / theme-toggle.spec.ts
Swadhin Biswas Feature/ai code reviewer (#73) · 8/2/2026
105a287
56 lines · 2.2 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
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-palette)').first();
    await expect(themeBtn).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-palette)').first();

    if (await themeBtn.isVisible()) {
      await themeBtn.click();
      const lightOption = page.locator('[role="menuitem"]').filter({ hasText: /Light/i }).first();
      await expect(lightOption).toBeVisible({ timeout: 5000 });
      await lightOption.click();

      await page.waitForFunction(
        () => localStorage.getItem("theme") === "light",
        { timeout: 5000 },
      );
      const newStorage = await page.evaluate(() => localStorage.getItem("theme"));
      expect(newStorage).toBe("light");
    }
  });

  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-palette)').first();

    if (await themeBtn.isVisible()) {
      await themeBtn.click();
      const darkOption = page.locator('[role="menuitem"]').filter({ hasText: /Default Dark/i }).first();
      await expect(darkOption).toBeVisible({ timeout: 5000 });
      await darkOption.click();

      await page.waitForFunction(
        () => localStorage.getItem("theme") !== null,
        { timeout: 5000 },
      );
      const storageAfterClick = await page.evaluate(() => localStorage.getItem("theme"));

      await page.reload();
      await page.waitForFunction(
        () => document.readyState === "complete",
        { timeout: 10000 },
      );
      const storageAfterReload = await page.evaluate(() => localStorage.getItem("theme"));

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