Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Show "Invite" menu option if "UIComponent.sendInvites" is enabled. #10363

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/components/views/context_menus/RoomContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
import SettingsStore from "../../../settings/SettingsStore";
import DevtoolsDialog from "../dialogs/DevtoolsDialog";
import { SdkContextClass } from "../../../contexts/SDKContext";
import { shouldShowComponent } from "../../../customisations/helpers/UIComponents";
import { UIComponent } from "../../../settings/UIFeature";

interface IProps extends IContextMenuProps {
room: Room;
Expand Down Expand Up @@ -113,7 +115,7 @@ const RoomContextMenu: React.FC<IProps> = ({ room, onFinished, ...props }) => {
videoRoomsEnabled && (room.isElementVideoRoom() || (elementCallVideoRoomsEnabled && room.isCallRoom()));

let inviteOption: JSX.Element | undefined;
if (room.canInvite(cli.getUserId()!) && !isDm) {
if (room.canInvite(cli.getUserId()!) && !isDm && shouldShowComponent(UIComponent.InviteUsers)) {
const onInviteClick = (ev: ButtonEvent): void => {
ev.preventDefault();
ev.stopPropagation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import IconizedContextMenu, {
IconizedContextMenuOptionList,
} from "../context_menus/IconizedContextMenu";
import { ButtonEvent } from "../elements/AccessibleButton";
import { shouldShowComponent } from "../../../customisations/helpers/UIComponents";
import { UIComponent } from "../../../settings/UIFeature";

export interface RoomGeneralContextMenuProps extends IContextMenuProps {
room: Room;
Expand Down Expand Up @@ -119,7 +121,7 @@ export const RoomGeneralContextMenu: React.FC<RoomGeneralContextMenuProps> = ({
);

let inviteOption: JSX.Element | null = null;
if (room.canInvite(cli.getUserId()!) && !isDm) {
if (room.canInvite(cli.getUserId()!) && !isDm && shouldShowComponent(UIComponent.InviteUsers)) {
inviteOption = (
<IconizedContextMenuOption
onClick={wrapHandler(
Expand Down
86 changes: 86 additions & 0 deletions test/components/views/context_menus/RoomContextMenu-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2023 Mikhail Aheichyk
Copyright 2023 Nordeck IT + Consulting GmbH.

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";
import { MatrixClient, PendingEventOrdering } from "matrix-js-sdk/src/client";
import { Room } from "matrix-js-sdk/src/models/room";

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";

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();
mockClient = mocked(MatrixClientPeg.get());

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();
});

function getComponent(props: Partial<ComponentProps<typeof RoomContextMenu>> = {}) {
return render(
<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);

getComponent();

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);

getComponent();

expect(screen.getByRole("menuitem", { name: "Invite" })).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { fireEvent, getByLabelText, render } from "@testing-library/react";
import { fireEvent, getByLabelText, render, screen } from "@testing-library/react";
import { mocked } from "jest-mock";
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";
import { MatrixClient, PendingEventOrdering } from "matrix-js-sdk/src/client";
Expand All @@ -32,6 +32,12 @@ import { DefaultTagID } from "../../../../src/stores/room-list/models";
import RoomListStore from "../../../../src/stores/room-list/RoomListStore";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { mkMessage, stubClient } from "../../../test-utils/test-utils";
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
import { UIComponent } from "../../../../src/settings/UIFeature";

jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
shouldShowComponent: jest.fn(),
}));

describe("RoomGeneralContextMenu", () => {
const ROOM_ID = "!123:matrix.org";
Expand Down Expand Up @@ -93,6 +99,28 @@ describe("RoomGeneralContextMenu", () => {
expect(container).toMatchSnapshot();
});

it("does not render invite menu item when UIComponent customisations disable room invite", () => {
room.updateMyMembership("join");
jest.spyOn(room, "canInvite").mockReturnValue(true);
mocked(shouldShowComponent).mockReturnValue(false);

getComponent({});

expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.InviteUsers);
expect(screen.queryByRole("menuitem", { name: "Invite" })).not.toBeInTheDocument();
});

it("renders invite menu item when UIComponent customisations enables room invite", () => {
room.updateMyMembership("join");
jest.spyOn(room, "canInvite").mockReturnValue(true);
mocked(shouldShowComponent).mockReturnValue(true);

getComponent({});

expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.InviteUsers);
expect(screen.getByRole("menuitem", { name: "Invite" })).toBeInTheDocument();
});

it("marks the room as read", async () => {
const event = mkMessage({
event: true,
Expand Down