2024-09-24 22:15:16 +02:00
|
|
|
/*
|
|
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-24 22:15:16 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-01-08 11:41:44 -05:00
|
|
|
import { act, renderHook } from "@testing-library/react";
|
2026-09-02 13:03:23 +02:00
|
|
|
import { createElement, type FC, type PropsWithChildren } from "react";
|
|
|
|
|
import { Subject } from "rxjs";
|
2024-09-24 22:15:16 +02:00
|
|
|
import {
|
|
|
|
|
afterEach,
|
|
|
|
|
beforeEach,
|
|
|
|
|
describe,
|
|
|
|
|
expect,
|
2024-12-11 09:27:55 +00:00
|
|
|
type Mock,
|
2024-09-24 22:15:16 +02:00
|
|
|
test,
|
|
|
|
|
vi,
|
|
|
|
|
} from "vitest";
|
|
|
|
|
|
|
|
|
|
import { useTheme } from "./useTheme";
|
2026-09-02 10:31:48 +02:00
|
|
|
import { useUrlParams } from "./UrlParams";
|
2026-09-02 13:03:23 +02:00
|
|
|
import {
|
|
|
|
|
type HostBridge,
|
|
|
|
|
HostBridgeProvider,
|
|
|
|
|
type HostRequest,
|
|
|
|
|
nullHostBridge,
|
|
|
|
|
} from "./HostBridge";
|
2024-09-24 22:15:16 +02:00
|
|
|
|
2026-09-02 10:31:48 +02:00
|
|
|
vi.mock("./UrlParams", () => ({ useUrlParams: vi.fn() }));
|
2024-09-24 22:15:16 +02:00
|
|
|
|
|
|
|
|
describe("useTheme", () => {
|
|
|
|
|
let originalClassList: DOMTokenList;
|
2026-09-02 13:03:23 +02:00
|
|
|
let themeChange$: Subject<HostRequest<{ name?: string }>>;
|
|
|
|
|
let wrapper: FC<PropsWithChildren>;
|
|
|
|
|
|
2024-09-24 22:15:16 +02:00
|
|
|
beforeEach(() => {
|
2026-09-02 13:03:23 +02:00
|
|
|
themeChange$ = new Subject();
|
|
|
|
|
const hostBridge: HostBridge = { ...nullHostBridge, themeChange$ };
|
|
|
|
|
wrapper = ({ children }) =>
|
|
|
|
|
createElement(HostBridgeProvider, { value: hostBridge }, children);
|
2024-09-24 22:15:16 +02:00
|
|
|
// Save the original classList to setup spies
|
|
|
|
|
originalClassList = document.body.classList;
|
|
|
|
|
|
|
|
|
|
vi.spyOn(originalClassList, "add");
|
|
|
|
|
vi.spyOn(originalClassList, "remove");
|
|
|
|
|
vi.spyOn(originalClassList, "item").mockReturnValue(null);
|
2026-09-02 10:31:48 +02:00
|
|
|
(useUrlParams as Mock).mockReturnValue({ theme: "dark" });
|
2024-09-24 22:15:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe.each([
|
|
|
|
|
{ setTheme: null, add: ["cpd-theme-dark"] },
|
|
|
|
|
{ setTheme: "light", add: ["cpd-theme-light"] },
|
|
|
|
|
{ setTheme: "dark-high-contrast", add: ["cpd-theme-dark-hc"] },
|
|
|
|
|
{ setTheme: "light-high-contrast", add: ["cpd-theme-light-hc"] },
|
|
|
|
|
])("apply procedure", ({ setTheme, add }) => {
|
|
|
|
|
test(`should apply ${add[0]} theme when ${setTheme} theme is specified`, () => {
|
2026-09-02 10:31:48 +02:00
|
|
|
(useUrlParams as Mock).mockReturnValue({ theme: setTheme });
|
2024-09-24 22:15:16 +02:00
|
|
|
|
2026-09-02 13:03:23 +02:00
|
|
|
renderHook(() => useTheme(), { wrapper });
|
2024-09-24 22:15:16 +02:00
|
|
|
|
|
|
|
|
expect(originalClassList.remove).toHaveBeenCalledWith(
|
|
|
|
|
"cpd-theme-light",
|
|
|
|
|
"cpd-theme-dark",
|
|
|
|
|
"cpd-theme-light-hc",
|
|
|
|
|
"cpd-theme-dark-hc",
|
|
|
|
|
);
|
|
|
|
|
expect(originalClassList.add).toHaveBeenCalledWith(...add);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("should not reapply the same theme if it hasn't changed", () => {
|
|
|
|
|
// Simulate a previous theme
|
|
|
|
|
originalClassList.item = vi.fn().mockReturnValue("cpd-theme-dark");
|
|
|
|
|
|
2026-09-02 13:03:23 +02:00
|
|
|
renderHook(() => useTheme(), { wrapper });
|
2024-09-24 22:15:16 +02:00
|
|
|
|
|
|
|
|
expect(document.body.classList.add).not.toHaveBeenCalledWith(
|
|
|
|
|
"cpd-theme-dark",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Ensure the 'no-theme' class is removed
|
|
|
|
|
expect(document.body.classList.remove).toHaveBeenCalledWith("no-theme");
|
|
|
|
|
expect(originalClassList.add).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2024-12-17 19:42:04 -05:00
|
|
|
|
2026-09-02 13:03:23 +02:00
|
|
|
test("theme changes in response to host requests", () => {
|
|
|
|
|
renderHook(() => useTheme(), { wrapper });
|
2024-12-17 19:42:04 -05:00
|
|
|
|
|
|
|
|
expect(originalClassList.add).toHaveBeenCalledWith("cpd-theme-dark");
|
2026-09-02 13:03:23 +02:00
|
|
|
const reply = vi.fn();
|
|
|
|
|
act(() => themeChange$.next({ data: { name: "light" }, reply }));
|
|
|
|
|
expect(reply).toHaveBeenCalledOnce();
|
2024-12-17 19:42:04 -05:00
|
|
|
expect(originalClassList.remove).toHaveBeenCalledWith(
|
|
|
|
|
"cpd-theme-light",
|
|
|
|
|
"cpd-theme-dark",
|
|
|
|
|
"cpd-theme-light-hc",
|
|
|
|
|
"cpd-theme-dark-hc",
|
|
|
|
|
);
|
|
|
|
|
expect(originalClassList.add).toHaveBeenLastCalledWith("cpd-theme-light");
|
|
|
|
|
});
|
2024-09-24 22:15:16 +02:00
|
|
|
});
|