2022-02-09 16:21:40 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +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";
|
|
|
|
import { prettyDOM, render, RenderResult, screen } from "@testing-library/react";
|
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-12-12 11:24:14 +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,
|
2022-12-12 11:24:14 +00:00
|
|
|
} from "../../../../src/utils/space";
|
2022-03-24 21:25:44 +00:00
|
|
|
import { leaveSpace } from "../../../../src/utils/leave-behaviour";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
|
|
|
|
import { UIComponent } from "../../../../src/settings/UIFeature";
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
|
2022-02-09 16:21:40 +00:00
|
|
|
shouldShowComponent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 11:24:14 +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(),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../../../src/utils/leave-behaviour", () => ({
|
2022-03-24 21:25:44 +00:00
|
|
|
leaveSpace: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 11:24:14 +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-12-12 11:24:14 +00:00
|
|
|
const makeMockSpace = (props = {}) =>
|
|
|
|
({
|
|
|
|
name: "test space",
|
|
|
|
getJoinRule: jest.fn(),
|
|
|
|
canInvite: jest.fn(),
|
|
|
|
currentState: {
|
|
|
|
maySendStateEvent: jest.fn(),
|
|
|
|
},
|
|
|
|
client: mockClient,
|
|
|
|
getMyMembership: jest.fn(),
|
|
|
|
...props,
|
|
|
|
} 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
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +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
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders invite option when space is public", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
const space = makeMockSpace({
|
2022-12-12 11:24:14 +00:00
|
|
|
getJoinRule: jest.fn().mockReturnValue("public"),
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
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-12-12 11:24:14 +00:00
|
|
|
it("renders invite option when user is has invite rights for space", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
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({
|
2022-12-12 11:24:14 +00:00
|
|
|
getJoinRule: jest.fn().mockReturnValue("public"),
|
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("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-12-12 11:24:14 +00:00
|
|
|
it("renders space settings option when user has rights", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
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-12-12 11:24:14 +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-12-12 11:24:14 +00:00
|
|
|
describe("add children section", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
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-12-12 11:24:14 +00:00
|
|
|
it("does not render section when user does not have permission to add children", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
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-12-12 11:24:14 +00:00
|
|
|
it("does not render section when UIComponent customisations disable room and space creation", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders section with add room button when UIComponent customisation allows CreateRoom", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
// only allow CreateRoom
|
2022-12-12 11:24:14 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders section with add space button when UIComponent customisation allows CreateSpace", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
// only allow CreateSpaces
|
2022-12-12 11:24:14 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|