2023-03-27 22:10:18 +02:00
|
|
|
/*
|
|
|
|
Copyright 2023 Mikhail Aheichyk
|
|
|
|
Copyright 2023 Nordeck IT + Consulting GmbH.
|
2023-04-19 13:34:27 +02:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
2023-03-27 22:10:18 +02:00
|
|
|
|
|
|
|
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 { render, screen } from "@testing-library/react";
|
|
|
|
import React, { ComponentProps } from "react";
|
|
|
|
import { mocked } from "jest-mock";
|
2023-08-09 09:18:41 +02:00
|
|
|
import { MatrixClient, PendingEventOrdering, Room } from "matrix-js-sdk/src/matrix";
|
2023-03-27 22:10:18 +02:00
|
|
|
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import RoomContextMenu from "../../../../src/components/views/context_menus/RoomContextMenu";
|
|
|
|
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
|
|
|
|
import { stubClient } from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2023-04-19 13:34:27 +02:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
2023-08-22 19:47:33 +02:00
|
|
|
import { EchoChamber } from "../../../../src/stores/local-echo/EchoChamber";
|
|
|
|
import { RoomNotifState } from "../../../../src/RoomNotifs";
|
2023-03-27 22:10:18 +02:00
|
|
|
|
|
|
|
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
|
|
|
|
shouldShowComponent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("RoomContextMenu", () => {
|
|
|
|
const ROOM_ID = "!123:matrix.org";
|
|
|
|
|
|
|
|
let room: Room;
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
|
|
|
|
let onFinished: () => void;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
|
|
|
stubClient();
|
2023-06-05 19:12:23 +02:00
|
|
|
mockClient = mocked(MatrixClientPeg.safeGet());
|
2023-03-27 22:10:18 +02:00
|
|
|
|
|
|
|
room = new Room(ROOM_ID, mockClient, mockClient.getUserId() ?? "", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
|
|
|
|
const dmRoomMap = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
|
|
|
|
onFinished = jest.fn();
|
|
|
|
});
|
|
|
|
|
2023-04-19 13:34:27 +02:00
|
|
|
function renderComponent(props: Partial<ComponentProps<typeof RoomContextMenu>> = {}) {
|
|
|
|
render(
|
2023-03-27 22:10:18 +02:00
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContextMenu room={room} onFinished={onFinished} {...props} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
it("does not render invite menu item when UIComponent customisations disable invite", () => {
|
|
|
|
jest.spyOn(room, "canInvite").mockReturnValue(true);
|
|
|
|
mocked(shouldShowComponent).mockReturnValue(false);
|
|
|
|
|
2023-04-19 13:34:27 +02:00
|
|
|
renderComponent();
|
2023-03-27 22:10:18 +02:00
|
|
|
|
|
|
|
expect(screen.queryByRole("menuitem", { name: "Invite" })).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders invite menu item when UIComponent customisations enable invite", () => {
|
|
|
|
jest.spyOn(room, "canInvite").mockReturnValue(true);
|
|
|
|
mocked(shouldShowComponent).mockReturnValue(true);
|
|
|
|
|
2023-04-19 13:34:27 +02:00
|
|
|
renderComponent();
|
2023-03-27 22:10:18 +02:00
|
|
|
|
|
|
|
expect(screen.getByRole("menuitem", { name: "Invite" })).toBeInTheDocument();
|
|
|
|
});
|
2023-04-19 13:34:27 +02:00
|
|
|
|
|
|
|
it("when developer mode is disabled, it should not render the developer tools option", () => {
|
|
|
|
renderComponent();
|
|
|
|
expect(screen.queryByText("Developer tools")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when developer mode is enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === "developerMode");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render the developer tools option", () => {
|
|
|
|
renderComponent();
|
|
|
|
expect(screen.getByText("Developer tools")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
2023-08-22 19:47:33 +02:00
|
|
|
|
|
|
|
it("should render notification option for joined rooms", () => {
|
|
|
|
const chamber = EchoChamber.forRoom(room);
|
|
|
|
chamber.notificationVolume = RoomNotifState.Mute;
|
|
|
|
jest.spyOn(room, "getMyMembership").mockReturnValue("join");
|
|
|
|
renderComponent();
|
|
|
|
|
|
|
|
expect(
|
|
|
|
screen.getByRole("menuitem", { name: "Notifications" }).querySelector(".mx_IconizedContextMenu_sublabel"),
|
|
|
|
).toHaveTextContent("Mute");
|
|
|
|
});
|
2023-03-27 22:10:18 +02:00
|
|
|
});
|