2022-02-09 16:21:40 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-02-09 16:21:40 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00: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-02-09 16:21:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2023-02-09 11:23:22 +00:00
|
|
|
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { Mocked, mocked } from "jest-mock";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { prettyDOM, render, RenderResult, screen } from "jest-matrix-react";
|
2023-02-09 11:23:22 +00:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import SpaceContextMenu from "../../../../../src/components/views/context_menus/SpaceContextMenu";
|
|
|
|
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext";
|
2022-02-09 16:21:40 +00:00
|
|
|
import {
|
|
|
|
shouldShowSpaceSettings,
|
|
|
|
showCreateNewRoom,
|
|
|
|
showCreateNewSubspace,
|
|
|
|
showSpaceInvite,
|
|
|
|
showSpaceSettings,
|
2024-10-15 13:57:26 +00:00
|
|
|
} from "../../../../../src/utils/space";
|
|
|
|
import { leaveSpace } from "../../../../../src/utils/leave-behaviour";
|
|
|
|
import { shouldShowComponent } from "../../../../../src/customisations/helpers/UIComponents";
|
|
|
|
import { UIComponent } from "../../../../../src/settings/UIFeature";
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/customisations/helpers/UIComponents", () => ({
|
2022-02-09 16:21:40 +00:00
|
|
|
shouldShowComponent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/utils/space", () => ({
|
2022-02-09 16:21:40 +00:00
|
|
|
shouldShowSpaceSettings: jest.fn(),
|
|
|
|
showCreateNewRoom: jest.fn(),
|
|
|
|
showCreateNewSubspace: jest.fn(),
|
|
|
|
showSpaceInvite: jest.fn(),
|
|
|
|
showSpacePreferences: jest.fn(),
|
|
|
|
showSpaceSettings: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/utils/leave-behaviour", () => ({
|
2022-03-24 21:25:44 +00:00
|
|
|
leaveSpace: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
describe("<SpaceContextMenu />", () => {
|
|
|
|
const userId = "@test:server";
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
const mockClient = {
|
|
|
|
getUserId: jest.fn().mockReturnValue(userId),
|
2023-08-01 15:26:14 +00:00
|
|
|
getSafeUserId: jest.fn().mockReturnValue(userId),
|
2023-02-09 11:23:22 +00:00
|
|
|
} as unknown as Mocked<MatrixClient>;
|
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
const makeMockSpace = (props = {}) =>
|
|
|
|
({
|
|
|
|
name: "test space",
|
|
|
|
getJoinRule: jest.fn(),
|
|
|
|
canInvite: jest.fn(),
|
|
|
|
currentState: {
|
|
|
|
maySendStateEvent: jest.fn(),
|
|
|
|
},
|
|
|
|
client: mockClient,
|
|
|
|
getMyMembership: jest.fn(),
|
|
|
|
...props,
|
2024-01-02 18:56:39 +00:00
|
|
|
}) as unknown as Room;
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
const defaultProps = {
|
|
|
|
space: makeMockSpace(),
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
};
|
2023-02-09 11:23:22 +00:00
|
|
|
|
|
|
|
const renderComponent = (props = {}): RenderResult =>
|
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<SpaceContextMenu {...defaultProps} {...props} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2022-02-09 16:21:40 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
mockClient.getUserId.mockReturnValue(userId);
|
2023-08-01 15:26:14 +00:00
|
|
|
mockClient.getSafeUserId.mockReturnValue(userId);
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders menu correctly", () => {
|
2023-02-09 11:23:22 +00:00
|
|
|
const { baseElement } = renderComponent();
|
|
|
|
expect(prettyDOM(baseElement)).toMatchSnapshot();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders invite option when space is public", () => {
|
|
|
|
const space = makeMockSpace({
|
|
|
|
getJoinRule: jest.fn().mockReturnValue("public"),
|
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space });
|
|
|
|
expect(screen.getByTestId("invite-option")).toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
it("renders invite option when user is has invite rights for space", () => {
|
|
|
|
const space = makeMockSpace({
|
|
|
|
canInvite: jest.fn().mockReturnValue(true),
|
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space });
|
2022-02-09 16:21:40 +00:00
|
|
|
expect(space.canInvite).toHaveBeenCalledWith(userId);
|
2023-02-09 11:23:22 +00:00
|
|
|
expect(screen.getByTestId("invite-option")).toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
|
|
|
it("opens invite dialog when invite option is clicked", async () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
const space = makeMockSpace({
|
|
|
|
getJoinRule: jest.fn().mockReturnValue("public"),
|
|
|
|
});
|
|
|
|
const onFinished = jest.fn();
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space, onFinished });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
await userEvent.click(screen.getByTestId("invite-option"));
|
2022-02-09 16:21:40 +00:00
|
|
|
|
|
|
|
expect(showSpaceInvite).toHaveBeenCalledWith(space);
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
it("renders space settings option when user has rights", () => {
|
|
|
|
mocked(shouldShowSpaceSettings).mockReturnValue(true);
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent();
|
2022-02-09 16:21:40 +00:00
|
|
|
expect(shouldShowSpaceSettings).toHaveBeenCalledWith(defaultProps.space);
|
2023-02-09 11:23:22 +00:00
|
|
|
expect(screen.getByTestId("settings-option")).toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
|
|
|
it("opens space settings when space settings option is clicked", async () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
mocked(shouldShowSpaceSettings).mockReturnValue(true);
|
|
|
|
const onFinished = jest.fn();
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ onFinished });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
await userEvent.click(screen.getByTestId("settings-option"));
|
2022-02-09 16:21:40 +00:00
|
|
|
|
|
|
|
expect(showSpaceSettings).toHaveBeenCalledWith(defaultProps.space);
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
it("renders leave option when user does not have rights to see space settings", () => {
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent();
|
|
|
|
expect(screen.getByTestId("leave-option")).toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
|
|
|
it("leaves space when leave option is clicked", async () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
const onFinished = jest.fn();
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ onFinished });
|
|
|
|
await userEvent.click(screen.getByTestId("leave-option"));
|
2022-02-09 16:21:40 +00:00
|
|
|
expect(leaveSpace).toHaveBeenCalledWith(defaultProps.space);
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
describe("add children section", () => {
|
|
|
|
const space = makeMockSpace();
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
// set space to allow adding children to space
|
|
|
|
mocked(space.currentState.maySendStateEvent).mockReturnValue(true);
|
|
|
|
mocked(shouldShowComponent).mockReturnValue(true);
|
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
it("does not render section when user does not have permission to add children", () => {
|
|
|
|
mocked(space.currentState.maySendStateEvent).mockReturnValue(false);
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
expect(screen.queryByTestId("add-to-space-header")).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByTestId("new-room-option")).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByTestId("new-subspace-option")).not.toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
2022-02-09 16:21:40 +00:00
|
|
|
it("does not render section when UIComponent customisations disable room and space creation", () => {
|
|
|
|
mocked(shouldShowComponent).mockReturnValue(false);
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
|
|
|
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.CreateRooms);
|
|
|
|
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.CreateSpaces);
|
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
expect(screen.queryByTestId("add-to-space-header")).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByTestId("new-room-option")).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByTestId("new-subspace-option")).not.toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders section with add room button when UIComponent customisation allows CreateRoom", () => {
|
|
|
|
// only allow CreateRoom
|
|
|
|
mocked(shouldShowComponent).mockImplementation((feature) => feature === UIComponent.CreateRooms);
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
expect(screen.getByTestId("add-to-space-header")).toBeInTheDocument();
|
|
|
|
expect(screen.getByTestId("new-room-option")).toBeInTheDocument();
|
|
|
|
expect(screen.queryByTestId("new-subspace-option")).not.toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders section with add space button when UIComponent customisation allows CreateSpace", () => {
|
|
|
|
// only allow CreateSpaces
|
|
|
|
mocked(shouldShowComponent).mockImplementation((feature) => feature === UIComponent.CreateSpaces);
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
expect(screen.getByTestId("add-to-space-header")).toBeInTheDocument();
|
|
|
|
expect(screen.queryByTestId("new-room-option")).not.toBeInTheDocument();
|
|
|
|
expect(screen.getByTestId("new-subspace-option")).toBeInTheDocument();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
it("opens create room dialog on add room button click", async () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
const onFinished = jest.fn();
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space, onFinished });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
await userEvent.click(screen.getByTestId("new-room-option"));
|
2022-02-09 16:21:40 +00:00
|
|
|
expect(showCreateNewRoom).toHaveBeenCalledWith(space);
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
2023-02-09 11:23:22 +00:00
|
|
|
|
|
|
|
it("opens create space dialog on add space button click", async () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
const onFinished = jest.fn();
|
2023-02-09 11:23:22 +00:00
|
|
|
renderComponent({ space, onFinished });
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2023-02-09 11:23:22 +00:00
|
|
|
await userEvent.click(screen.getByTestId("new-subspace-option"));
|
2022-02-09 16:21:40 +00:00
|
|
|
expect(showCreateNewSubspace).toHaveBeenCalledWith(space);
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|