2022-08-01 09:47:13 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-08-01 09:47:13 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03: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-08-01 09:47:13 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { ReactElement } from "react";
|
2024-10-14 19:11:58 +03:00
|
|
|
import { render, screen } from "jest-matrix-react";
|
2023-10-16 02:03:25 +03:00
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
import SettingsStore, { CallbackFn } from "../../../../../src/settings/SettingsStore";
|
|
|
|
import SdkConfig from "../../../../../src/SdkConfig";
|
|
|
|
import { UserTab } from "../../../../../src/components/views/dialogs/UserTab";
|
|
|
|
import UserSettingsDialog from "../../../../../src/components/views/dialogs/UserSettingsDialog";
|
2022-08-01 09:47:13 +03:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
mockClientMethodsUser,
|
|
|
|
mockClientMethodsServer,
|
|
|
|
mockPlatformPeg,
|
2023-10-25 18:34:03 +03:00
|
|
|
mockClientMethodsCrypto,
|
2024-05-07 12:32:24 +03:00
|
|
|
mockClientMethodsRooms,
|
|
|
|
useMockMediaDevices,
|
2024-10-15 16:57:26 +03:00
|
|
|
} from "../../../../test-utils";
|
|
|
|
import { UIFeature } from "../../../../../src/settings/UIFeature";
|
|
|
|
import { SettingLevel } from "../../../../../src/settings/SettingLevel";
|
|
|
|
import { SdkContextClass } from "../../../../../src/contexts/SDKContext";
|
2022-08-01 09:47:13 +03:00
|
|
|
|
|
|
|
mockPlatformPeg({
|
|
|
|
supportsSpellCheckSettings: jest.fn().mockReturnValue(false),
|
|
|
|
getAppVersion: jest.fn().mockResolvedValue("1"),
|
|
|
|
});
|
|
|
|
|
2024-10-15 16:57:26 +03:00
|
|
|
jest.mock("../../../../../src/settings/SettingsStore", () => ({
|
2022-08-01 09:47:13 +03:00
|
|
|
getValue: jest.fn(),
|
|
|
|
getValueAt: jest.fn(),
|
|
|
|
canSetValue: jest.fn(),
|
|
|
|
monitorSetting: jest.fn(),
|
|
|
|
watchSetting: jest.fn(),
|
|
|
|
unwatchSetting: jest.fn(),
|
|
|
|
getFeatureSettingNames: jest.fn(),
|
|
|
|
getBetaInfo: jest.fn(),
|
2024-05-07 12:32:24 +03:00
|
|
|
getDisplayName: jest.fn(),
|
|
|
|
getDescription: jest.fn(),
|
|
|
|
shouldHaveWarning: jest.fn(),
|
|
|
|
disabledMessage: jest.fn(),
|
2024-06-14 14:00:30 +03:00
|
|
|
settingIsOveriddenAtConfigLevel: jest.fn(),
|
2022-08-01 09:47:13 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
describe("<UserSettingsDialog />", () => {
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const mockSettingsStore = mocked(SettingsStore);
|
2023-10-16 02:03:25 +03:00
|
|
|
let mockClient!: MockedObject<MatrixClient>;
|
2022-08-01 09:47:13 +03:00
|
|
|
|
2023-10-16 02:03:25 +03:00
|
|
|
let sdkContext: SdkContextClass;
|
2022-08-01 09:47:13 +03:00
|
|
|
const defaultProps = { onFinished: jest.fn() };
|
2023-02-28 13:31:48 +03:00
|
|
|
const getComponent = (props: Partial<typeof defaultProps & { initialTabId?: UserTab }> = {}): ReactElement => (
|
2023-10-16 02:03:25 +03:00
|
|
|
<UserSettingsDialog sdkContext={sdkContext} {...defaultProps} {...props} />
|
2022-08-01 09:47:13 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
2023-10-16 02:03:25 +03:00
|
|
|
mockClient = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(userId),
|
|
|
|
...mockClientMethodsServer(),
|
2023-10-25 18:34:03 +03:00
|
|
|
...mockClientMethodsCrypto(),
|
2024-05-07 12:32:24 +03:00
|
|
|
...mockClientMethodsRooms(),
|
|
|
|
getIgnoredUsers: jest.fn().mockResolvedValue([]),
|
|
|
|
getPushers: jest.fn().mockResolvedValue([]),
|
|
|
|
getProfileInfo: jest.fn().mockResolvedValue({}),
|
2023-10-16 02:03:25 +03:00
|
|
|
});
|
|
|
|
sdkContext = new SdkContextClass();
|
|
|
|
sdkContext.client = mockClient;
|
2022-08-01 09:47:13 +03:00
|
|
|
mockSettingsStore.getValue.mockReturnValue(false);
|
2024-05-07 12:32:24 +03:00
|
|
|
mockSettingsStore.getValueAt.mockReturnValue(false);
|
2022-08-01 09:47:13 +03:00
|
|
|
mockSettingsStore.getFeatureSettingNames.mockReturnValue([]);
|
2024-08-06 20:22:02 +03:00
|
|
|
SdkConfig.reset();
|
|
|
|
SdkConfig.put({ brand: "Test" });
|
2022-08-01 09:47:13 +03:00
|
|
|
});
|
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
const getActiveTabLabel = (container: Element) =>
|
2023-02-24 18:28:40 +03:00
|
|
|
container.querySelector(".mx_TabbedView_tabLabel_active")?.textContent;
|
2022-08-01 09:47:13 +03:00
|
|
|
|
|
|
|
it("should render general settings tab when no initialTabId", () => {
|
|
|
|
const { container } = render(getComponent());
|
|
|
|
|
2024-08-06 20:05:08 +03:00
|
|
|
expect(getActiveTabLabel(container)).toEqual("Account");
|
2022-08-01 09:47:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should render initial tab when initialTabId is set", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Help }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Help & About");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render general tab if initialTabId tab cannot be rendered", () => {
|
|
|
|
// mjolnir tab is only rendered in some configs
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Mjolnir }));
|
|
|
|
|
2024-08-06 20:05:08 +03:00
|
|
|
expect(getActiveTabLabel(container)).toEqual("Account");
|
2022-08-01 09:47:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders tabs correctly", () => {
|
2024-08-06 20:22:02 +03:00
|
|
|
SdkConfig.add({
|
|
|
|
show_labs_settings: true,
|
|
|
|
});
|
2022-08-01 09:47:13 +03:00
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container.querySelectorAll(".mx_TabbedView_tabLabel")).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders ignored users tab when feature_mjolnir is enabled", () => {
|
2022-11-04 13:48:08 +03:00
|
|
|
mockSettingsStore.getValue.mockImplementation((settingName): any => settingName === "feature_mjolnir");
|
2022-08-01 09:47:13 +03:00
|
|
|
const { getByTestId } = render(getComponent());
|
|
|
|
expect(getByTestId(`settings-tab-${UserTab.Mjolnir}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders voip tab when voip is enabled", () => {
|
2022-11-04 13:48:08 +03:00
|
|
|
mockSettingsStore.getValue.mockImplementation((settingName): any => settingName === UIFeature.Voip);
|
2022-08-01 09:47:13 +03:00
|
|
|
const { getByTestId } = render(getComponent());
|
|
|
|
expect(getByTestId(`settings-tab-${UserTab.Voice}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2024-05-07 12:32:24 +03:00
|
|
|
it("renders with session manager tab selected", () => {
|
|
|
|
const { getByTestId } = render(getComponent({ initialTabId: UserTab.SessionManager }));
|
2022-08-08 16:51:00 +03:00
|
|
|
expect(getByTestId(`settings-tab-${UserTab.SessionManager}`)).toBeTruthy();
|
2024-05-07 12:32:24 +03:00
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Sessions");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with appearance tab selected", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Appearance }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Appearance");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Appearance");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with notifications tab selected", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Notifications }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Notifications");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Notifications");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with preferences tab selected", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Preferences }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Preferences");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Preferences");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with keyboard tab selected", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Keyboard }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Keyboard");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Keyboard");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with sidebar tab selected", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Sidebar }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Sidebar");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Sidebar");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with voip tab selected", () => {
|
|
|
|
useMockMediaDevices();
|
|
|
|
mockSettingsStore.getValue.mockImplementation((settingName): any => settingName === UIFeature.Voip);
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Voice }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Voice & Video");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Voice & Video");
|
|
|
|
});
|
|
|
|
|
2024-08-06 20:22:02 +03:00
|
|
|
it("renders with security tab selected", () => {
|
2024-05-07 12:32:24 +03:00
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Security }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Security & Privacy");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Security & Privacy");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with labs tab selected", () => {
|
2024-08-06 20:22:02 +03:00
|
|
|
SdkConfig.add({
|
|
|
|
show_labs_settings: true,
|
2024-05-07 12:32:24 +03:00
|
|
|
});
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Labs }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Labs");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Labs");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with mjolnir tab selected", () => {
|
|
|
|
mockSettingsStore.getValue.mockImplementation((settingName): any => settingName === "feature_mjolnir");
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Mjolnir }));
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Ignored users");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Ignored Users");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders with help tab selected", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Help }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Help & About");
|
|
|
|
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Settings: Help & About");
|
2022-08-08 16:51:00 +03:00
|
|
|
});
|
|
|
|
|
2022-08-01 09:47:13 +03:00
|
|
|
it("renders labs tab when show_labs_settings is enabled in config", () => {
|
2024-08-06 20:22:02 +03:00
|
|
|
SdkConfig.add({
|
|
|
|
show_labs_settings: true,
|
|
|
|
});
|
2022-08-01 09:47:13 +03:00
|
|
|
const { getByTestId } = render(getComponent());
|
|
|
|
expect(getByTestId(`settings-tab-${UserTab.Labs}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders labs tab when some feature is in beta", () => {
|
|
|
|
mockSettingsStore.getFeatureSettingNames.mockReturnValue(["feature_beta_setting", "feature_just_normal_labs"]);
|
|
|
|
mockSettingsStore.getBetaInfo.mockImplementation((settingName) =>
|
|
|
|
settingName === "feature_beta_setting" ? ({} as any) : undefined,
|
|
|
|
);
|
|
|
|
const { getByTestId } = render(getComponent());
|
|
|
|
expect(getByTestId(`settings-tab-${UserTab.Labs}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2024-08-06 20:22:02 +03:00
|
|
|
it("watches settings", async () => {
|
2022-08-08 16:51:00 +03:00
|
|
|
const watchSettingCallbacks: Record<string, CallbackFn> = {};
|
2022-08-01 09:47:13 +03:00
|
|
|
|
|
|
|
mockSettingsStore.watchSetting.mockImplementation((settingName, roomId, callback) => {
|
2022-08-08 16:51:00 +03:00
|
|
|
watchSettingCallbacks[settingName] = callback;
|
2022-08-01 09:47:13 +03:00
|
|
|
return `mock-watcher-id-${settingName}`;
|
|
|
|
});
|
2024-04-30 21:52:32 +03:00
|
|
|
mockSettingsStore.getValue.mockReturnValue(false);
|
2022-08-01 09:47:13 +03:00
|
|
|
|
2024-08-06 20:22:02 +03:00
|
|
|
const { queryByTestId, findByTestId, unmount } = render(getComponent());
|
2022-08-01 09:47:13 +03:00
|
|
|
expect(queryByTestId(`settings-tab-${UserTab.Mjolnir}`)).toBeFalsy();
|
|
|
|
|
2024-04-30 21:52:32 +03:00
|
|
|
expect(mockSettingsStore.watchSetting).toHaveBeenCalledWith("feature_mjolnir", null, expect.anything());
|
2022-08-01 09:47:13 +03:00
|
|
|
|
|
|
|
// call the watch setting callback
|
2024-04-30 21:52:32 +03:00
|
|
|
mockSettingsStore.getValue.mockReturnValue(true);
|
2022-08-08 16:51:00 +03:00
|
|
|
watchSettingCallbacks["feature_mjolnir"]("feature_mjolnir", "", SettingLevel.ACCOUNT, true, true);
|
2024-04-30 21:52:32 +03:00
|
|
|
|
2022-08-01 09:47:13 +03:00
|
|
|
// tab is rendered now
|
2024-08-06 20:22:02 +03:00
|
|
|
await expect(findByTestId(`settings-tab-${UserTab.Mjolnir}`)).resolves.toBeTruthy();
|
2022-08-01 09:47:13 +03:00
|
|
|
|
|
|
|
unmount();
|
|
|
|
|
2022-08-08 16:51:00 +03:00
|
|
|
// unwatches settings on unmount
|
2022-08-01 09:47:13 +03:00
|
|
|
expect(mockSettingsStore.unwatchSetting).toHaveBeenCalledWith("mock-watcher-id-feature_mjolnir");
|
|
|
|
});
|
|
|
|
});
|