2022-08-01 06:47:13 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { ReactElement } from "react";
|
|
|
|
import { render } from "@testing-library/react";
|
2023-10-15 23:03:25 +00:00
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2022-08-01 06:47:13 +00: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";
|
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
mockClientMethodsUser,
|
|
|
|
mockClientMethodsServer,
|
|
|
|
mockPlatformPeg,
|
|
|
|
} from "../../../test-utils";
|
|
|
|
import { UIFeature } from "../../../../src/settings/UIFeature";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
2023-10-15 23:03:25 +00:00
|
|
|
import { SdkContextClass } from "../../../../src/contexts/SDKContext";
|
2022-08-01 06:47:13 +00:00
|
|
|
|
|
|
|
mockPlatformPeg({
|
|
|
|
supportsSpellCheckSettings: jest.fn().mockReturnValue(false),
|
|
|
|
getAppVersion: jest.fn().mockResolvedValue("1"),
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock("../../../../src/settings/SettingsStore", () => ({
|
|
|
|
getValue: jest.fn(),
|
|
|
|
getValueAt: jest.fn(),
|
|
|
|
canSetValue: jest.fn(),
|
|
|
|
monitorSetting: jest.fn(),
|
|
|
|
watchSetting: jest.fn(),
|
|
|
|
unwatchSetting: jest.fn(),
|
|
|
|
getFeatureSettingNames: jest.fn(),
|
|
|
|
getBetaInfo: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
jest.mock("../../../../src/SdkConfig", () => ({
|
|
|
|
get: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("<UserSettingsDialog />", () => {
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const mockSettingsStore = mocked(SettingsStore);
|
|
|
|
const mockSdkConfig = mocked(SdkConfig);
|
2023-10-15 23:03:25 +00:00
|
|
|
let mockClient!: MockedObject<MatrixClient>;
|
2022-08-01 06:47:13 +00:00
|
|
|
|
2023-10-15 23:03:25 +00:00
|
|
|
let sdkContext: SdkContextClass;
|
2022-08-01 06:47:13 +00:00
|
|
|
const defaultProps = { onFinished: jest.fn() };
|
2023-02-28 10:31:48 +00:00
|
|
|
const getComponent = (props: Partial<typeof defaultProps & { initialTabId?: UserTab }> = {}): ReactElement => (
|
2023-10-15 23:03:25 +00:00
|
|
|
<UserSettingsDialog sdkContext={sdkContext} {...defaultProps} {...props} />
|
2022-08-01 06:47:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
2023-10-15 23:03:25 +00:00
|
|
|
mockClient = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(userId),
|
|
|
|
...mockClientMethodsServer(),
|
|
|
|
});
|
|
|
|
sdkContext = new SdkContextClass();
|
|
|
|
sdkContext.client = mockClient;
|
2022-08-01 06:47:13 +00:00
|
|
|
mockSettingsStore.getValue.mockReturnValue(false);
|
|
|
|
mockSettingsStore.getFeatureSettingNames.mockReturnValue([]);
|
|
|
|
mockSdkConfig.get.mockReturnValue({ brand: "Test" });
|
|
|
|
});
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const getActiveTabLabel = (container: Element) =>
|
2023-02-24 15:28:40 +00:00
|
|
|
container.querySelector(".mx_TabbedView_tabLabel_active")?.textContent;
|
2023-05-03 22:35:43 +00:00
|
|
|
const getActiveTabHeading = (container: Element) =>
|
2023-07-04 16:54:28 +00:00
|
|
|
container.querySelector(".mx_SettingsSection .mx_Heading_h3")?.textContent;
|
2022-08-01 06:47:13 +00:00
|
|
|
|
|
|
|
it("should render general settings tab when no initialTabId", () => {
|
|
|
|
const { container } = render(getComponent());
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("General");
|
|
|
|
expect(getActiveTabHeading(container)).toEqual("General");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render initial tab when initialTabId is set", () => {
|
|
|
|
const { container } = render(getComponent({ initialTabId: UserTab.Help }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("Help & About");
|
|
|
|
expect(getActiveTabHeading(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 }));
|
|
|
|
|
|
|
|
expect(getActiveTabLabel(container)).toEqual("General");
|
|
|
|
expect(getActiveTabHeading(container)).toEqual("General");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders tabs correctly", () => {
|
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container.querySelectorAll(".mx_TabbedView_tabLabel")).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders ignored users tab when feature_mjolnir is enabled", () => {
|
2022-11-04 10:48:08 +00:00
|
|
|
mockSettingsStore.getValue.mockImplementation((settingName): any => settingName === "feature_mjolnir");
|
2022-08-01 06:47:13 +00:00
|
|
|
const { getByTestId } = render(getComponent());
|
|
|
|
expect(getByTestId(`settings-tab-${UserTab.Mjolnir}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders voip tab when voip is enabled", () => {
|
2022-11-04 10:48:08 +00:00
|
|
|
mockSettingsStore.getValue.mockImplementation((settingName): any => settingName === UIFeature.Voip);
|
2022-08-01 06:47:13 +00:00
|
|
|
const { getByTestId } = render(getComponent());
|
|
|
|
expect(getByTestId(`settings-tab-${UserTab.Voice}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2023-05-26 01:58:28 +00:00
|
|
|
it("renders session manager tab", () => {
|
2022-08-08 13:51:00 +00:00
|
|
|
const { getByTestId } = render(getComponent());
|
|
|
|
expect(getByTestId(`settings-tab-${UserTab.SessionManager}`)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-08-01 06:47:13 +00:00
|
|
|
it("renders labs tab when show_labs_settings is enabled in config", () => {
|
2022-11-04 10:48:08 +00:00
|
|
|
// @ts-ignore simplified test stub
|
2022-08-01 06:47:13 +00:00
|
|
|
mockSdkConfig.get.mockImplementation((configName) => configName === "show_labs_settings");
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:51:00 +00:00
|
|
|
it("watches settings", () => {
|
|
|
|
const watchSettingCallbacks: Record<string, CallbackFn> = {};
|
2022-08-01 06:47:13 +00:00
|
|
|
|
|
|
|
mockSettingsStore.watchSetting.mockImplementation((settingName, roomId, callback) => {
|
2022-08-08 13:51:00 +00:00
|
|
|
watchSettingCallbacks[settingName] = callback;
|
2022-08-01 06:47:13 +00:00
|
|
|
return `mock-watcher-id-${settingName}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
const { queryByTestId, unmount } = render(getComponent());
|
|
|
|
expect(queryByTestId(`settings-tab-${UserTab.Mjolnir}`)).toBeFalsy();
|
|
|
|
|
|
|
|
expect(mockSettingsStore.watchSetting.mock.calls[0][0]).toEqual("feature_mjolnir");
|
|
|
|
|
|
|
|
// call the watch setting callback
|
2022-08-08 13:51:00 +00:00
|
|
|
watchSettingCallbacks["feature_mjolnir"]("feature_mjolnir", "", SettingLevel.ACCOUNT, true, true);
|
2022-08-01 06:47:13 +00:00
|
|
|
// tab is rendered now
|
|
|
|
expect(queryByTestId(`settings-tab-${UserTab.Mjolnir}`)).toBeTruthy();
|
|
|
|
|
|
|
|
unmount();
|
|
|
|
|
2022-08-08 13:51:00 +00:00
|
|
|
// unwatches settings on unmount
|
2022-08-01 06:47:13 +00:00
|
|
|
expect(mockSettingsStore.unwatchSetting).toHaveBeenCalledWith("mock-watcher-id-feature_mjolnir");
|
|
|
|
});
|
|
|
|
});
|