2022-12-22 21:18:38 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2022-12-22 21:18:38 +08:00
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-12-22 21:18:38 +08:00
|
|
|
*/
|
|
|
|
|
2023-03-28 04:10:18 +08:00
|
|
|
import { fireEvent, getByLabelText, render, screen } from "@testing-library/react";
|
2022-12-22 21:18:38 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2024-03-18 22:40:52 +08:00
|
|
|
import { ReceiptType, MatrixClient, PendingEventOrdering, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2022-12-22 21:18:38 +08:00
|
|
|
import React from "react";
|
2023-04-19 19:34:27 +08:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2024-06-13 22:15:59 +08:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
2022-12-22 21:18:38 +08:00
|
|
|
|
|
|
|
import { ChevronFace } from "../../../../src/components/structures/ContextMenu";
|
|
|
|
import {
|
|
|
|
RoomGeneralContextMenu,
|
|
|
|
RoomGeneralContextMenuProps,
|
|
|
|
} from "../../../../src/components/views/context_menus/RoomGeneralContextMenu";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
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";
|
2023-03-28 04:10:18 +08:00
|
|
|
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
|
|
|
|
import { UIComponent } from "../../../../src/settings/UIFeature";
|
2023-04-19 19:34:27 +08:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
2024-07-05 21:39:13 +08:00
|
|
|
import { clearAllModals } from "../../../test-utils";
|
2023-03-28 04:10:18 +08:00
|
|
|
|
|
|
|
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
|
|
|
|
shouldShowComponent: jest.fn(),
|
|
|
|
}));
|
2022-12-22 21:18:38 +08:00
|
|
|
|
|
|
|
describe("RoomGeneralContextMenu", () => {
|
|
|
|
const ROOM_ID = "!123:matrix.org";
|
|
|
|
|
|
|
|
let room: Room;
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
|
|
|
|
let onFinished: () => void;
|
|
|
|
|
|
|
|
function getComponent(props?: Partial<RoomGeneralContextMenuProps>) {
|
|
|
|
return render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomGeneralContextMenu
|
|
|
|
room={room}
|
|
|
|
onFinished={onFinished}
|
|
|
|
{...props}
|
|
|
|
managed={true}
|
|
|
|
mountAsChild={true}
|
|
|
|
left={1}
|
|
|
|
top={1}
|
|
|
|
chevronFace={ChevronFace.Left}
|
|
|
|
/>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
|
|
|
stubClient();
|
2023-06-06 01:12:23 +08:00
|
|
|
mockClient = mocked(MatrixClientPeg.safeGet());
|
2022-12-22 21:18:38 +08:00
|
|
|
|
|
|
|
room = new Room(ROOM_ID, mockClient, mockClient.getUserId() ?? "", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
|
|
|
|
const dmRoomMap = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
|
|
|
|
jest.spyOn(RoomListStore.instance, "getTagsForRoom").mockReturnValueOnce([
|
|
|
|
DefaultTagID.DM,
|
|
|
|
DefaultTagID.Favourite,
|
|
|
|
]);
|
|
|
|
|
|
|
|
onFinished = jest.fn();
|
|
|
|
});
|
|
|
|
|
2024-07-05 21:39:13 +08:00
|
|
|
afterEach(async () => {
|
|
|
|
await clearAllModals();
|
2023-04-19 19:34:27 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:18:38 +08:00
|
|
|
it("renders an empty context menu for archived rooms", async () => {
|
|
|
|
jest.spyOn(RoomListStore.instance, "getTagsForRoom").mockReturnValueOnce([DefaultTagID.Archived]);
|
|
|
|
|
|
|
|
const { container } = getComponent({});
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders the default context menu", async () => {
|
|
|
|
const { container } = getComponent({});
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2023-03-28 04:10:18 +08:00
|
|
|
it("does not render invite menu item when UIComponent customisations disable room invite", () => {
|
2024-03-12 22:52:54 +08:00
|
|
|
room.updateMyMembership(KnownMembership.Join);
|
2023-03-28 04:10:18 +08:00
|
|
|
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", () => {
|
2024-03-12 22:52:54 +08:00
|
|
|
room.updateMyMembership(KnownMembership.Join);
|
2023-03-28 04:10:18 +08:00
|
|
|
jest.spyOn(room, "canInvite").mockReturnValue(true);
|
|
|
|
mocked(shouldShowComponent).mockReturnValue(true);
|
|
|
|
|
|
|
|
getComponent({});
|
|
|
|
|
|
|
|
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.InviteUsers);
|
|
|
|
expect(screen.getByRole("menuitem", { name: "Invite" })).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2022-12-22 21:18:38 +08:00
|
|
|
it("marks the room as read", async () => {
|
|
|
|
const event = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: "!room:id",
|
|
|
|
user: "@user:id",
|
|
|
|
ts: 1000,
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event], {});
|
|
|
|
|
|
|
|
const { container } = getComponent({});
|
|
|
|
|
|
|
|
const markAsReadBtn = getByLabelText(container, "Mark as read");
|
|
|
|
fireEvent.click(markAsReadBtn);
|
|
|
|
|
2024-06-13 22:15:59 +08:00
|
|
|
await sleep(0);
|
2024-03-19 21:28:20 +08:00
|
|
|
|
2022-12-22 21:18:38 +08:00
|
|
|
expect(mockClient.sendReadReceipt).toHaveBeenCalledWith(event, ReceiptType.Read, true);
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2023-04-19 19:34:27 +08:00
|
|
|
|
2024-03-19 21:28:20 +08:00
|
|
|
it("marks the room as unread", async () => {
|
|
|
|
room.updateMyMembership("join");
|
|
|
|
|
|
|
|
const { container } = getComponent({});
|
|
|
|
|
|
|
|
const markAsUnreadBtn = getByLabelText(container, "Mark as unread");
|
|
|
|
fireEvent.click(markAsUnreadBtn);
|
|
|
|
|
2024-06-13 22:15:59 +08:00
|
|
|
await sleep(0);
|
2024-03-19 21:28:20 +08:00
|
|
|
|
|
|
|
expect(mockClient.setRoomAccountData).toHaveBeenCalledWith(ROOM_ID, "com.famedly.marked_unread", {
|
|
|
|
unread: true,
|
|
|
|
});
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-04-19 19:34:27 +08:00
|
|
|
it("when developer mode is disabled, it should not render the developer tools option", () => {
|
|
|
|
getComponent();
|
|
|
|
expect(screen.queryByText("Developer tools")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when developer mode is enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === "developerMode");
|
|
|
|
getComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render the developer tools option", async () => {
|
|
|
|
const developerToolsItem = screen.getByRole("menuitem", { name: "Developer tools" });
|
|
|
|
expect(developerToolsItem).toBeInTheDocument();
|
|
|
|
|
|
|
|
// click open developer tools dialog
|
|
|
|
await userEvent.click(developerToolsItem);
|
|
|
|
|
|
|
|
// assert that the dialog is displayed by searching some if its contents
|
|
|
|
expect(await screen.findByText("Toolbox")).toBeInTheDocument();
|
|
|
|
expect(await screen.findByText(`Room ID: ${ROOM_ID}`)).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
2022-12-22 21:18:38 +08:00
|
|
|
});
|