2022-02-24 09:01:06 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-02-24 09:01:06 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00: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-02-24 09:01:06 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { render, screen, waitFor } from "jest-matrix-react";
|
2022-12-29 12:44:18 +00:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import QuickThemeSwitcher from "../../../../../src/components/views/spaces/QuickThemeSwitcher";
|
|
|
|
import { getOrderedThemes } from "../../../../../src/theme";
|
|
|
|
import SettingsStore from "../../../../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../../../../src/settings/SettingLevel";
|
|
|
|
import dis from "../../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../../src/dispatcher/actions";
|
|
|
|
import { mockPlatformPeg } from "../../../../test-utils/platform";
|
|
|
|
import { useTheme } from "../../../../../src/hooks/useTheme";
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/hooks/useTheme", () => ({
|
2024-06-26 15:47:01 +00:00
|
|
|
useTheme: jest.fn(),
|
2022-02-24 09:01:06 +00:00
|
|
|
}));
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/theme");
|
|
|
|
jest.mock("../../../../../src/settings/SettingsStore", () => ({
|
2022-02-24 09:01:06 +00:00
|
|
|
setValue: jest.fn(),
|
|
|
|
getValue: jest.fn(),
|
|
|
|
monitorSetting: jest.fn(),
|
2022-12-06 09:59:17 +00:00
|
|
|
watchSetting: jest.fn(),
|
2022-02-24 09:01:06 +00:00
|
|
|
}));
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/dispatcher/dispatcher", () => ({
|
2022-02-24 09:01:06 +00:00
|
|
|
dispatch: jest.fn(),
|
|
|
|
register: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-03-22 10:32:35 +00:00
|
|
|
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
|
2022-02-28 16:05:52 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("<QuickThemeSwitcher />", () => {
|
2022-02-24 09:01:06 +00:00
|
|
|
const defaultProps = {
|
|
|
|
requestClose: jest.fn(),
|
|
|
|
};
|
2022-12-29 12:44:18 +00:00
|
|
|
const renderComponent = (props = {}) => render(<QuickThemeSwitcher {...defaultProps} {...props} />);
|
2022-02-24 09:01:06 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(getOrderedThemes)
|
|
|
|
.mockClear()
|
|
|
|
.mockReturnValue([
|
|
|
|
{ id: "light", name: "Light" },
|
|
|
|
{ id: "dark", name: "Dark" },
|
|
|
|
]);
|
2024-06-26 15:47:01 +00:00
|
|
|
|
|
|
|
mocked(useTheme).mockClear().mockReturnValue({
|
2022-12-12 11:24:14 +00:00
|
|
|
theme: "light",
|
2024-06-26 15:47:01 +00:00
|
|
|
systemThemeActivated: false,
|
2022-02-24 09:01:06 +00:00
|
|
|
});
|
|
|
|
mocked(SettingsStore).setValue.mockClear().mockResolvedValue();
|
|
|
|
mocked(dis).dispatch.mockClear();
|
|
|
|
});
|
|
|
|
|
2022-12-29 12:44:18 +00:00
|
|
|
const selectFromDropdown = async (getByTextArg: RegExp | string) => {
|
2023-09-25 17:12:41 +00:00
|
|
|
const dropdown = screen.getByRole("button", { name: "Theme" });
|
2022-12-29 12:44:18 +00:00
|
|
|
await userEvent.click(dropdown);
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(dropdown).toHaveAttribute("aria-expanded", "true");
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2022-12-29 12:44:18 +00:00
|
|
|
await userEvent.click(screen.getByText(getByTextArg));
|
|
|
|
return waitFor(() => {
|
|
|
|
expect(dropdown).toHaveAttribute("aria-expanded", "false");
|
2022-02-24 09:01:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders dropdown correctly when light theme is selected", () => {
|
2022-12-29 12:44:18 +00:00
|
|
|
renderComponent();
|
|
|
|
expect(screen.getByText("Light")).toBeInTheDocument();
|
2022-02-24 09:01:06 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders dropdown correctly when use system theme is truthy", () => {
|
2024-06-26 15:47:01 +00:00
|
|
|
mocked(useTheme).mockClear().mockReturnValue({
|
2022-12-12 11:24:14 +00:00
|
|
|
theme: "light",
|
2024-06-26 15:47:01 +00:00
|
|
|
systemThemeActivated: true,
|
2022-02-24 09:01:06 +00:00
|
|
|
});
|
2022-12-29 12:44:18 +00:00
|
|
|
renderComponent();
|
|
|
|
expect(screen.getByText("Match system")).toBeInTheDocument();
|
2022-02-24 09:01:06 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates settings when match system is selected", async () => {
|
2022-02-24 09:01:06 +00:00
|
|
|
const requestClose = jest.fn();
|
2022-12-29 12:44:18 +00:00
|
|
|
renderComponent({ requestClose });
|
2022-02-24 09:01:06 +00:00
|
|
|
|
2022-12-29 12:44:18 +00:00
|
|
|
await selectFromDropdown(/match system/i);
|
2022-02-24 09:01:06 +00:00
|
|
|
|
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledTimes(1);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, true);
|
2022-02-24 09:01:06 +00:00
|
|
|
|
|
|
|
expect(dis.dispatch).not.toHaveBeenCalled();
|
|
|
|
expect(requestClose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates settings when a theme is selected", async () => {
|
2022-02-24 09:01:06 +00:00
|
|
|
// ie not match system
|
|
|
|
const requestClose = jest.fn();
|
2022-12-29 12:44:18 +00:00
|
|
|
renderComponent({ requestClose });
|
2022-02-24 09:01:06 +00:00
|
|
|
|
2022-12-29 12:44:18 +00:00
|
|
|
await selectFromDropdown(/dark/i);
|
2022-02-24 09:01:06 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, false);
|
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith("theme", null, SettingLevel.DEVICE, "dark");
|
2022-02-24 09:01:06 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme, forceTheme: "dark" });
|
2022-02-24 09:01:06 +00:00
|
|
|
expect(requestClose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("rechecks theme when setting theme fails", async () => {
|
|
|
|
mocked(SettingsStore.setValue).mockRejectedValue("oops");
|
2022-02-24 09:01:06 +00:00
|
|
|
const requestClose = jest.fn();
|
2022-12-29 12:44:18 +00:00
|
|
|
renderComponent({ requestClose });
|
2022-02-24 09:01:06 +00:00
|
|
|
|
2022-12-29 12:44:18 +00:00
|
|
|
await selectFromDropdown(/match system/i);
|
2022-02-24 09:01:06 +00:00
|
|
|
|
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme });
|
|
|
|
expect(requestClose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|