element-web-Github/test/unit-tests/components/views/dialogs/RoomSettingsDialog-test.tsx

186 lines
7.5 KiB
TypeScript
Raw Normal View History

/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import React from "react";
import { fireEvent, render, screen, waitFor } from "jest-matrix-react";
import {
EventTimeline,
EventType,
JoinRule,
MatrixEvent,
Room,
RoomStateEvent,
Visibility,
} from "matrix-js-sdk/src/matrix";
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";
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
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);
room.name = "Test Room";
const room2 = new Room("!room2:server.org", mockClient, userId);
room2.name = "Another Room";
jest.spyOn(SettingsStore, "getValue");
beforeEach(() => {
jest.clearAllMocks();
mockClient.getRoom.mockImplementation((roomId) => {
if (roomId === room.roomId) return room;
if (roomId === room2.roomId) return room2;
return null;
});
jest.spyOn(SettingsStore, "getValue").mockReset().mockReturnValue(false);
Fix display of no avatar in avatar setting controls (#12558) * New user profile UI in User Settings Using new Edit In Place component. * Show avatar upload error * Fix avatar upload error * Wire up errors & feedback for display name setting * Implement avatar upload / remove progress toast * Add 768px breakpoint * Fix display of no avatar in avatar setting controls There was supposed to be a person icon but it was invisible, and also would have been inappropriate for room avatars anyway. This makes it match the designs by being the same as whatever the default avatar is. * Fix room profile display * Update to released compund-web with required components / fixes * Require compound-web 4.4.0 because we do need it * Update snapshots Because of course all the auto-generated IDs of unrelated things have changed. * Fix duplicate import * Fix CSS comment * Update snapshot * Run all the tests so the ids stay the same * Start of a test for ProfileSettings * More tests * Test that a toast appears * Test ToastRack * Update snapshots * Add the usernamee control * Fix playwright tests * New compound version for editinplace fixes * Fix useId to not just generate a constant ID * Use the label in the username component * Fix widths of test boxes * Update screenshots * Put ^ back on compound-web version * Split CSS for room & user profile settings and name the components correspondingly * Fix playwright test * Update room settings screenshot * Use original screenshot instead * Add required props in test * Fix test * Also here * Update screenshots * Remove user icon ...which is unused now, as far as I can see. * Fix styling of unrelated buttons Needed to be added in other places otherwise the specificity changes. Also put the old screenshots back. * Add copyright year * Fix copyright year * Switch to useMatrixClientContext * Fix other test
2024-06-07 01:35:44 +08:00
const dmRoomMap = {
getUserIdForRoomId: jest.fn(),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
});
const getComponent = (onFinished = jest.fn(), propRoomId = roomId) =>
render(<RoomSettingsDialog roomId={propRoomId} onFinished={onFinished} />, {
wrapper: ({ children }) => (
<MatrixClientContext.Provider value={mockClient}>{children}</MatrixClientContext.Provider>
),
});
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();
});
describe("Settings tabs", () => {
it("renders default tabs correctly", () => {
const { container } = getComponent();
expect(container.querySelectorAll(".mx_TabbedView_tabLabel")).toMatchSnapshot();
});
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();
});
it("re-renders on room join rule changes", async () => {
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,
new MatrixEvent({ content: {}, type: EventType.RoomJoinRules }),
room.getLiveTimeline().getState(EventTimeline.FORWARDS)!,
null,
);
await waitFor(() =>
expect(screen.queryByTestId("settings-tab-ROOM_PEOPLE_TAB")).not.toBeInTheDocument(),
);
});
});
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();
fireEvent.click(screen.getByText("Polls"));
expect(container.querySelector(".mx_SettingsTab")).toMatchSnapshot();
});
});
});