2023-03-13 22:08:59 +00:00
|
|
|
/*
|
|
|
|
Copyright 2023 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 from "react";
|
2024-08-06 17:22:02 +00:00
|
|
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
2023-08-16 08:16:19 +00:00
|
|
|
import {
|
|
|
|
EventTimeline,
|
|
|
|
EventType,
|
|
|
|
JoinRule,
|
|
|
|
MatrixEvent,
|
|
|
|
Room,
|
|
|
|
RoomStateEvent,
|
|
|
|
Visibility,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2023-03-13 22:08:59 +00:00
|
|
|
|
|
|
|
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils";
|
|
|
|
import RoomSettingsDialog from "../../../../src/components/views/dialogs/RoomSettingsDialog";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { UIFeature } from "../../../../src/settings/UIFeature";
|
2024-06-06 17:35:44 +00:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2023-03-13 22:08:59 +00:00
|
|
|
|
|
|
|
describe("<RoomSettingsDialog />", () => {
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(userId),
|
|
|
|
isRoomEncrypted: jest.fn().mockReturnValue(false),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getDomain: jest.fn().mockReturnValue("server.org"),
|
|
|
|
getLocalAliases: jest.fn().mockResolvedValue({ aliases: [] }),
|
|
|
|
getRoomDirectoryVisibility: jest.fn().mockResolvedValue({ visibility: Visibility.Private }),
|
|
|
|
getOrCreateFilter: jest.fn(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const roomId = "!room:server.org";
|
|
|
|
const room = new Room(roomId, mockClient, userId);
|
2023-04-27 01:20:02 +00:00
|
|
|
room.name = "Test Room";
|
|
|
|
const room2 = new Room("!room2:server.org", mockClient, userId);
|
|
|
|
room2.name = "Another Room";
|
2023-03-13 22:08:59 +00:00
|
|
|
|
|
|
|
jest.spyOn(SettingsStore, "getValue");
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
2023-04-27 01:20:02 +00:00
|
|
|
mockClient.getRoom.mockImplementation((roomId) => {
|
|
|
|
if (roomId === room.roomId) return room;
|
|
|
|
if (roomId === room2.roomId) return room2;
|
|
|
|
return null;
|
|
|
|
});
|
2023-03-13 22:08:59 +00:00
|
|
|
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReset().mockReturnValue(false);
|
2024-06-06 17:35:44 +00:00
|
|
|
|
|
|
|
const dmRoomMap = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
2023-03-13 22:08:59 +00:00
|
|
|
});
|
|
|
|
|
2023-04-27 01:20:02 +00:00
|
|
|
const getComponent = (onFinished = jest.fn(), propRoomId = roomId) =>
|
|
|
|
render(<RoomSettingsDialog roomId={propRoomId} onFinished={onFinished} />, {
|
2023-03-13 22:08:59 +00:00
|
|
|
wrapper: ({ children }) => (
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>{children}</MatrixClientContext.Provider>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2023-04-27 01:20:02 +00:00
|
|
|
it("catches errors when room is not found", () => {
|
|
|
|
getComponent(undefined, "!room-that-does-not-exist");
|
|
|
|
expect(screen.getByText("Something went wrong!")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates when roomId prop changes", () => {
|
|
|
|
const { rerender, getByText } = getComponent(undefined, roomId);
|
|
|
|
|
|
|
|
expect(getByText(`Room Settings - ${room.name}`)).toBeInTheDocument();
|
|
|
|
|
|
|
|
rerender(<RoomSettingsDialog roomId={room2.roomId} onFinished={jest.fn()} />);
|
|
|
|
|
|
|
|
expect(getByText(`Room Settings - ${room2.name}`)).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2023-03-13 22:08:59 +00:00
|
|
|
describe("Settings tabs", () => {
|
|
|
|
it("renders default tabs correctly", () => {
|
|
|
|
const { container } = getComponent();
|
|
|
|
expect(container.querySelectorAll(".mx_TabbedView_tabLabel")).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2023-08-16 08:16:19 +00:00
|
|
|
describe("people settings tab", () => {
|
|
|
|
it("does not render when disabled and room join rule is not knock", () => {
|
|
|
|
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Invite);
|
|
|
|
getComponent();
|
|
|
|
expect(screen.queryByTestId("settings-tab-ROOM_PEOPLE_TAB")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not render when disabled and room join rule is knock", () => {
|
|
|
|
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Knock);
|
|
|
|
getComponent();
|
|
|
|
expect(screen.queryByTestId("settings-tab-ROOM_PEOPLE_TAB")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not render when enabled and room join rule is not knock", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(setting) => setting === "feature_ask_to_join",
|
|
|
|
);
|
|
|
|
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Invite);
|
|
|
|
getComponent();
|
|
|
|
expect(screen.queryByTestId("settings-tab-ROOM_PEOPLE_TAB")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders when enabled and room join rule is knock", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(setting) => setting === "feature_ask_to_join",
|
|
|
|
);
|
|
|
|
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Knock);
|
|
|
|
getComponent();
|
|
|
|
expect(screen.getByTestId("settings-tab-ROOM_PEOPLE_TAB")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2024-08-06 17:22:02 +00:00
|
|
|
it("re-renders on room join rule changes", async () => {
|
2023-08-16 08:16:19 +00:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(setting) => setting === "feature_ask_to_join",
|
|
|
|
);
|
|
|
|
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Knock);
|
|
|
|
getComponent();
|
|
|
|
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Invite);
|
|
|
|
mockClient.emit(
|
|
|
|
RoomStateEvent.Events,
|
2023-08-23 09:51:27 +00:00
|
|
|
new MatrixEvent({ content: {}, type: EventType.RoomJoinRules }),
|
2023-08-16 08:16:19 +00:00
|
|
|
room.getLiveTimeline().getState(EventTimeline.FORWARDS)!,
|
|
|
|
null,
|
|
|
|
);
|
2024-08-06 17:22:02 +00:00
|
|
|
await waitFor(() =>
|
|
|
|
expect(screen.queryByTestId("settings-tab-ROOM_PEOPLE_TAB")).not.toBeInTheDocument(),
|
|
|
|
);
|
2023-08-16 08:16:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-13 22:08:59 +00:00
|
|
|
it("renders voip settings tab when enabled", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(settingName) => settingName === "feature_group_calls",
|
|
|
|
);
|
|
|
|
getComponent();
|
|
|
|
expect(screen.getByTestId("settings-tab-ROOM_VOIP_TAB")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders bridges settings tab when enabled", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(settingName) => settingName === "feature_bridge_state",
|
|
|
|
);
|
|
|
|
getComponent();
|
|
|
|
expect(screen.getByTestId("settings-tab-ROOM_BRIDGES_TAB")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders advanced settings tab when enabled", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(settingName) => settingName === UIFeature.AdvancedSettings,
|
|
|
|
);
|
|
|
|
getComponent();
|
|
|
|
expect(screen.getByTestId("settings-tab-ROOM_ADVANCED_TAB")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("poll history", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockClient.getOrCreateFilter.mockResolvedValue("filterId");
|
|
|
|
});
|
|
|
|
it("renders poll history tab", () => {
|
|
|
|
getComponent();
|
|
|
|
expect(screen.getByTestId("settings-tab-ROOM_POLL_HISTORY_TAB")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("displays poll history when tab clicked", () => {
|
|
|
|
const { container } = getComponent();
|
|
|
|
|
2024-08-01 12:00:40 +00:00
|
|
|
fireEvent.click(screen.getByText("Polls"));
|
2023-03-13 22:08:59 +00:00
|
|
|
|
|
|
|
expect(container.querySelector(".mx_SettingsTab")).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|