2022-02-24 10:01:06 +01:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import React from "react";
|
2022-08-02 15:10:43 +02:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-12-12 12:24:14 +01:00
|
|
|
import { mount } from "enzyme";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
|
|
|
|
import QuickThemeSwitcher from "../../../../src/components/views/spaces/QuickThemeSwitcher";
|
|
|
|
import { getOrderedThemes } from "../../../../src/theme";
|
|
|
|
import ThemeChoicePanel from "../../../../src/components/views/settings/ThemeChoicePanel";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { findById } from "../../../test-utils";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
|
|
|
import dis from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
|
|
|
import { mockPlatformPeg } from "../../../test-utils/platform";
|
|
|
|
|
|
|
|
jest.mock("../../../../src/theme");
|
|
|
|
jest.mock("../../../../src/components/views/settings/ThemeChoicePanel", () => ({
|
2022-02-24 10:01:06 +01:00
|
|
|
calculateThemeState: jest.fn(),
|
|
|
|
}));
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/settings/SettingsStore", () => ({
|
2022-02-24 10:01:06 +01:00
|
|
|
setValue: jest.fn(),
|
|
|
|
getValue: jest.fn(),
|
|
|
|
monitorSetting: jest.fn(),
|
2022-12-06 10:59:17 +01:00
|
|
|
watchSetting: jest.fn(),
|
2022-02-24 10:01:06 +01:00
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/dispatcher/dispatcher", () => ({
|
2022-02-24 10:01:06 +01:00
|
|
|
dispatch: jest.fn(),
|
|
|
|
register: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-03-22 11:32:35 +01:00
|
|
|
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
|
2022-02-28 17:05:52 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("<QuickThemeSwitcher />", () => {
|
2022-02-24 10:01:06 +01:00
|
|
|
const defaultProps = {
|
|
|
|
requestClose: jest.fn(),
|
|
|
|
};
|
2022-12-12 12:24:14 +01:00
|
|
|
const getComponent = (props = {}) => mount(<QuickThemeSwitcher {...defaultProps} {...props} />);
|
2022-02-24 10:01:06 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
mocked(getOrderedThemes)
|
|
|
|
.mockClear()
|
|
|
|
.mockReturnValue([
|
|
|
|
{ id: "light", name: "Light" },
|
|
|
|
{ id: "dark", name: "Dark" },
|
|
|
|
]);
|
2022-02-24 10:01:06 +01:00
|
|
|
mocked(ThemeChoicePanel).calculateThemeState.mockClear().mockReturnValue({
|
2022-12-12 12:24:14 +01:00
|
|
|
theme: "light",
|
|
|
|
useSystemTheme: false,
|
2022-02-24 10:01:06 +01:00
|
|
|
});
|
|
|
|
mocked(SettingsStore).setValue.mockClear().mockResolvedValue();
|
|
|
|
mocked(dis).dispatch.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
const getSelectedLabel = (component) =>
|
|
|
|
findById(component, "mx_QuickSettingsButton_themePickerDropdown_value").text();
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const openDropdown = (component) =>
|
|
|
|
act(async () => {
|
|
|
|
component.find(".mx_Dropdown_input").at(0).simulate("click");
|
|
|
|
component.setProps({});
|
|
|
|
});
|
2022-02-24 10:01:06 +01:00
|
|
|
const getOption = (component, themeId) =>
|
|
|
|
findById(component, `mx_QuickSettingsButton_themePickerDropdown__${themeId}`).at(0);
|
|
|
|
|
|
|
|
const selectOption = async (component, themeId: string) => {
|
|
|
|
await openDropdown(component);
|
|
|
|
await act(async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
getOption(component, themeId).simulate("click");
|
2022-02-24 10:01:06 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders dropdown correctly when light theme is selected", () => {
|
2022-02-24 10:01:06 +01:00
|
|
|
const component = getComponent();
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(getSelectedLabel(component)).toEqual("Light");
|
2022-02-24 10:01:06 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders dropdown correctly when use system theme is truthy", () => {
|
2022-02-24 10:01:06 +01:00
|
|
|
mocked(ThemeChoicePanel).calculateThemeState.mockClear().mockReturnValue({
|
2022-12-12 12:24:14 +01:00
|
|
|
theme: "light",
|
|
|
|
useSystemTheme: true,
|
2022-02-24 10:01:06 +01:00
|
|
|
});
|
|
|
|
const component = getComponent();
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(getSelectedLabel(component)).toEqual("Match system");
|
2022-02-24 10:01:06 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("updates settings when match system is selected", async () => {
|
2022-02-24 10:01:06 +01:00
|
|
|
const requestClose = jest.fn();
|
|
|
|
const component = getComponent({ requestClose });
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
await selectOption(component, "MATCH_SYSTEM_THEME_ID");
|
2022-02-24 10:01:06 +01:00
|
|
|
|
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledTimes(1);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, true);
|
2022-02-24 10:01:06 +01:00
|
|
|
|
|
|
|
expect(dis.dispatch).not.toHaveBeenCalled();
|
|
|
|
expect(requestClose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("updates settings when a theme is selected", async () => {
|
2022-02-24 10:01:06 +01:00
|
|
|
// ie not match system
|
|
|
|
const requestClose = jest.fn();
|
|
|
|
const component = getComponent({ requestClose });
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
await selectOption(component, "dark");
|
2022-02-24 10:01:06 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith("use_system_theme", null, SettingLevel.DEVICE, false);
|
|
|
|
expect(SettingsStore.setValue).toHaveBeenCalledWith("theme", null, SettingLevel.DEVICE, "dark");
|
2022-02-24 10:01:06 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme, forceTheme: "dark" });
|
2022-02-24 10:01:06 +01:00
|
|
|
expect(requestClose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("rechecks theme when setting theme fails", async () => {
|
|
|
|
mocked(SettingsStore.setValue).mockRejectedValue("oops");
|
2022-02-24 10:01:06 +01:00
|
|
|
const requestClose = jest.fn();
|
|
|
|
const component = getComponent({ requestClose });
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
await selectOption(component, "MATCH_SYSTEM_THEME_ID");
|
2022-02-24 10:01:06 +01:00
|
|
|
|
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({ action: Action.RecheckTheme });
|
|
|
|
expect(requestClose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|