2022-09-28 18:42:40 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-09-28 18:42:40 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-09-28 18:42:40 +08:00
|
|
|
*/
|
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
|
|
|
import { enumerateThemes, getOrderedThemes, setTheme } from "../../src/theme";
|
2022-09-28 18:42:40 +08:00
|
|
|
|
|
|
|
describe("theme", () => {
|
2024-07-17 21:51:42 +08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
2022-09-28 18:42:40 +08:00
|
|
|
describe("setTheme", () => {
|
2023-02-13 19:39:16 +08:00
|
|
|
let lightTheme: HTMLStyleElement;
|
|
|
|
let darkTheme: HTMLStyleElement;
|
2024-02-13 22:07:58 +08:00
|
|
|
let lightCustomTheme: HTMLStyleElement;
|
2022-09-28 18:42:40 +08:00
|
|
|
|
2022-09-28 21:25:16 +08:00
|
|
|
let spyQuerySelectorAll: jest.MockInstance<NodeListOf<Element>, [selectors: string]>;
|
2023-06-14 23:49:14 +08:00
|
|
|
let spyClassList: jest.SpyInstance<void, string[], any>;
|
2022-09-28 21:25:16 +08:00
|
|
|
|
2022-09-28 18:42:40 +08:00
|
|
|
beforeEach(() => {
|
2024-02-13 22:07:58 +08:00
|
|
|
const styles = ["light", "dark", "light-custom", "dark-custom"].map(
|
|
|
|
(theme) =>
|
|
|
|
({
|
|
|
|
dataset: {
|
|
|
|
mxTheme: theme,
|
|
|
|
},
|
|
|
|
disabled: true,
|
|
|
|
href: "fake URL",
|
|
|
|
onload: (): void => void 0,
|
|
|
|
}) as unknown as HTMLStyleElement,
|
|
|
|
);
|
2022-09-28 18:42:40 +08:00
|
|
|
lightTheme = styles[0];
|
|
|
|
darkTheme = styles[1];
|
2024-02-13 22:07:58 +08:00
|
|
|
lightCustomTheme = styles[2];
|
2022-09-28 18:42:40 +08:00
|
|
|
|
|
|
|
jest.spyOn(document.body, "style", "get").mockReturnValue([] as any);
|
2022-09-28 21:25:16 +08:00
|
|
|
spyQuerySelectorAll = jest.spyOn(document, "querySelectorAll").mockReturnValue(styles as any);
|
2023-06-14 23:49:14 +08:00
|
|
|
spyClassList = jest.spyOn(document.body.classList, "add");
|
2022-09-28 18:42:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should switch theme on onload call", async () => {
|
|
|
|
// When
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
setTheme("light").then(resolve);
|
2023-02-17 01:21:44 +08:00
|
|
|
lightTheme.onload!({} as Event);
|
2022-09-28 18:42:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
2022-09-28 21:25:16 +08:00
|
|
|
expect(spyQuerySelectorAll).toHaveBeenCalledWith("[data-mx-theme]");
|
2023-03-01 23:23:35 +08:00
|
|
|
expect(spyQuerySelectorAll).toHaveBeenCalledTimes(1);
|
2022-09-28 18:42:40 +08:00
|
|
|
expect(lightTheme.disabled).toBe(false);
|
|
|
|
expect(darkTheme.disabled).toBe(true);
|
2023-06-14 23:49:14 +08:00
|
|
|
expect(spyClassList).toHaveBeenCalledWith("cpd-theme-light");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should switch to dark", async () => {
|
|
|
|
// When
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
setTheme("dark").then(resolve);
|
|
|
|
darkTheme.onload!({} as Event);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyClassList).toHaveBeenCalledWith("cpd-theme-dark");
|
2022-09-28 18:42:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should reject promise on onerror call", () => {
|
|
|
|
return expect(
|
|
|
|
new Promise((resolve) => {
|
|
|
|
setTheme("light").catch((e) => resolve(e));
|
2023-02-17 01:21:44 +08:00
|
|
|
lightTheme.onerror!("call onerror");
|
2022-09-28 18:42:40 +08:00
|
|
|
}),
|
|
|
|
).resolves.toBe("call onerror");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should switch theme if CSS are preloaded", async () => {
|
|
|
|
// When
|
|
|
|
jest.spyOn(document, "styleSheets", "get").mockReturnValue([lightTheme] as any);
|
|
|
|
|
|
|
|
await setTheme("light");
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(lightTheme.disabled).toBe(false);
|
|
|
|
expect(darkTheme.disabled).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should switch theme if CSS is loaded during pooling", async () => {
|
|
|
|
// When
|
|
|
|
jest.useFakeTimers();
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
setTheme("light").then(resolve);
|
|
|
|
jest.spyOn(document, "styleSheets", "get").mockReturnValue([lightTheme] as any);
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(lightTheme.disabled).toBe(false);
|
|
|
|
expect(darkTheme.disabled).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should reject promise if pooling maximum value is reached", () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTheme("light").catch(resolve);
|
|
|
|
jest.advanceTimersByTime(200 * 10);
|
|
|
|
});
|
|
|
|
});
|
2024-02-13 22:07:58 +08:00
|
|
|
|
|
|
|
it("applies a custom Compound theme", async () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue([
|
|
|
|
{
|
|
|
|
name: "blue",
|
|
|
|
compound: {
|
|
|
|
"--cpd-color-icon-accent-tertiary": "var(--cpd-color-blue-800)",
|
|
|
|
"--cpd-color-text-action-accent": "var(--cpd-color-blue-900)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const spy = jest.spyOn(document.head, "appendChild").mockImplementation();
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
setTheme("custom-blue").then(resolve);
|
|
|
|
lightCustomTheme.onload!({} as Event);
|
|
|
|
});
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
expect(spy.mock.calls[0][0].textContent).toMatchSnapshot();
|
|
|
|
spy.mockRestore();
|
|
|
|
});
|
2022-09-28 18:42:40 +08:00
|
|
|
});
|
2023-06-28 22:39:19 +08:00
|
|
|
|
|
|
|
describe("enumerateThemes", () => {
|
|
|
|
it("should return a list of themes", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue([{ name: "pink" }]);
|
|
|
|
expect(enumerateThemes()).toEqual({
|
|
|
|
"light": "Light",
|
|
|
|
"light-high-contrast": "Light high contrast",
|
|
|
|
"dark": "Dark",
|
|
|
|
"custom-pink": "pink",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be robust to malformed custom_themes values", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue([23]);
|
|
|
|
expect(enumerateThemes()).toEqual({
|
|
|
|
"light": "Light",
|
|
|
|
"light-high-contrast": "Light high contrast",
|
|
|
|
"dark": "Dark",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-07-17 21:51:42 +08:00
|
|
|
|
|
|
|
describe("getOrderedThemes", () => {
|
|
|
|
it("should return a list of themes in the correct order", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue([{ name: "Zebra Striped" }, { name: "Apple Green" }]);
|
|
|
|
expect(getOrderedThemes()).toEqual([
|
|
|
|
{ id: "light", name: "Light" },
|
|
|
|
{ id: "dark", name: "Dark" },
|
|
|
|
{ id: "custom-Apple Green", name: "Apple Green" },
|
|
|
|
{ id: "custom-Zebra Striped", name: "Zebra Striped" },
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2022-09-28 18:42:40 +08:00
|
|
|
});
|