fix: make theme logic safe and add better e2e tests (#16959)

* fix: improve theme logic

* remove null from ThemeLabelProps value

* add better e2e tests
This commit is contained in:
Benny Joo authored and GitHub committed 2024-10-07 11:23:30 +01:00
1 parent b0bfec7247
commit 21dd44733f
6 files changed
+127 -23

No files matched your search

+92 -8
View File
@@ -1,10 +1,70 @@
import { expect } from "@playwright/test";
import { testBothFutureAndLegacyRoutes } from "playwright/lib/future-legacy-routes";
import { test } from "./lib/fixtures";
testBothFutureAndLegacyRoutes.describe("Change Theme Test", () => {
test("change theme to dark", async ({ page, users }) => {
test.describe("Change App Theme Test", () => {
test("change app theme to dark", async ({ page, users }) => {
const pro = await users.create();
await pro.apiLogin();
await page.goto("/settings/my-account/appearance");
await page.click('[data-testid="appTheme-dark"]');
await page.click('[data-testid="update-app-theme-btn"]');
const toast = await page.waitForSelector('[data-testid="toast-success"]');
expect(toast).toBeTruthy();
const darkModeClass = await page.getAttribute("html", "class");
expect(darkModeClass).toContain("dark");
const themeValue = await page.evaluate(() => localStorage.getItem("app-theme"));
expect(themeValue).toBe("dark");
});
test("change app theme to light", async ({ page, users }) => {
const pro = await users.create();
await pro.apiLogin();
await page.goto("/settings/my-account/appearance");
await page.click('[data-testid="appTheme-light"]');
await page.click('[data-testid="update-app-theme-btn"]');
const toast = await page.waitForSelector('[data-testid="toast-success"]');
expect(toast).toBeTruthy();
const darkModeClass = await page.getAttribute("html", "class");
expect(darkModeClass).toContain("light");
const themeValue = await page.evaluate(() => localStorage.getItem("app-theme"));
expect(themeValue).toBe("light");
});
test("change app theme to system", async ({ page, users }) => {
const pro = await users.create();
await pro.apiLogin();
await page.goto("/settings/my-account/appearance");
await page.click('[data-testid="appTheme-light"]');
await page.click('[data-testid="update-app-theme-btn"]');
const toast1 = await page.waitForSelector('[data-testid="toast-success"]');
expect(toast1).toBeTruthy();
await page.click('[data-testid="appTheme-system"]');
await page.click('[data-testid="update-app-theme-btn"]');
const toast2 = await page.waitForSelector('[data-testid="toast-success"]');
expect(toast2).toBeTruthy();
await page.waitForTimeout(3000);
const themeValue = await page.evaluate(() => localStorage.getItem("app-theme"));
expect(themeValue).toBe("system");
const systemTheme = await page.evaluate(() => {
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
});
expect(await page.getAttribute("html", "class")).toContain(systemTheme);
});
});
test.describe("Change Booking Page Theme Test", () => {
test("change booking page theme to dark", async ({ page, users }) => {
const pro = await users.create();
await pro.apiLogin();
@@ -19,11 +79,11 @@ testBothFutureAndLegacyRoutes.describe("Change Theme Test", () => {
expect(toast).toBeTruthy();
//Go to the profile page and check if the theme is dark
await page.goto(`/${pro.username}`);
const darkModeClass = await page.getAttribute("html", "class");
expect(darkModeClass).toContain("dark");
const htmlClass = await page.getAttribute("html", "class");
expect(htmlClass).toContain("dark");
});
test("change theme to light", async ({ page, users }) => {
test("change booking page theme to light", async ({ page, users }) => {
const pro = await users.create();
await pro.apiLogin();
@@ -38,7 +98,31 @@ testBothFutureAndLegacyRoutes.describe("Change Theme Test", () => {
expect(toast).toBeTruthy();
//Go to the profile page and check if the theme is light
await page.goto(`/${pro.username}`);
const darkModeClass = await page.getAttribute("html", "class");
expect(darkModeClass).toContain("light");
const htmlClass = await page.getAttribute("html", "class");
expect(htmlClass).toContain("light");
});
test("change booking page theme to system", async ({ page, users }) => {
const pro = await users.create();
await pro.apiLogin();
await page.goto("/settings/my-account/appearance");
await page.click('[data-testid="theme-light"]');
await page.click('[data-testid="update-theme-btn"]');
const toast1 = await page.waitForSelector('[data-testid="toast-success"]');
expect(toast1).toBeTruthy();
await page.click('[data-testid="theme-system"]');
await page.click('[data-testid="update-theme-btn"]');
const toast2 = await page.waitForSelector('[data-testid="toast-success"]');
expect(toast2).toBeTruthy();
await page.goto(`/${pro.username}`);
const systemTheme = await page.evaluate(() => {
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
});
expect(await page.getAttribute("html", "class")).toContain(systemTheme);
});
});