|
| 1 | +/* |
| 2 | +Copyright 2023 Mikhail Aheichyk |
| 3 | +Copyright 2023 Nordeck IT + Consulting GmbH. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +import { render, screen } from "@testing-library/react"; |
| 19 | +import React, { ComponentProps } from "react"; |
| 20 | +import { mocked } from "jest-mock"; |
| 21 | +import { MatrixClient, PendingEventOrdering } from "matrix-js-sdk/src/client"; |
| 22 | +import { Room } from "matrix-js-sdk/src/models/room"; |
| 23 | + |
| 24 | +import MatrixClientContext from "../../../../src/contexts/MatrixClientContext"; |
| 25 | +import RoomContextMenu from "../../../../src/components/views/context_menus/RoomContextMenu"; |
| 26 | +import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents"; |
| 27 | +import { stubClient } from "../../../test-utils"; |
| 28 | +import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; |
| 29 | +import DMRoomMap from "../../../../src/utils/DMRoomMap"; |
| 30 | + |
| 31 | +jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({ |
| 32 | + shouldShowComponent: jest.fn(), |
| 33 | +})); |
| 34 | + |
| 35 | +describe("RoomContextMenu", () => { |
| 36 | + const ROOM_ID = "!123:matrix.org"; |
| 37 | + |
| 38 | + let room: Room; |
| 39 | + let mockClient: MatrixClient; |
| 40 | + |
| 41 | + let onFinished: () => void; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + jest.clearAllMocks(); |
| 45 | + |
| 46 | + stubClient(); |
| 47 | + mockClient = mocked(MatrixClientPeg.get()); |
| 48 | + |
| 49 | + room = new Room(ROOM_ID, mockClient, mockClient.getUserId() ?? "", { |
| 50 | + pendingEventOrdering: PendingEventOrdering.Detached, |
| 51 | + }); |
| 52 | + |
| 53 | + const dmRoomMap = { |
| 54 | + getUserIdForRoomId: jest.fn(), |
| 55 | + } as unknown as DMRoomMap; |
| 56 | + DMRoomMap.setShared(dmRoomMap); |
| 57 | + |
| 58 | + onFinished = jest.fn(); |
| 59 | + }); |
| 60 | + |
| 61 | + function getComponent(props: Partial<ComponentProps<typeof RoomContextMenu>> = {}) { |
| 62 | + return render( |
| 63 | + <MatrixClientContext.Provider value={mockClient}> |
| 64 | + <RoomContextMenu room={room} onFinished={onFinished} {...props} /> |
| 65 | + </MatrixClientContext.Provider>, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + it("does not render invite menu item when UIComponent customisations disable invite", () => { |
| 70 | + jest.spyOn(room, "canInvite").mockReturnValue(true); |
| 71 | + mocked(shouldShowComponent).mockReturnValue(false); |
| 72 | + |
| 73 | + getComponent(); |
| 74 | + |
| 75 | + expect(screen.queryByRole("menuitem", { name: "Invite" })).not.toBeInTheDocument(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("renders invite menu item when UIComponent customisations enable invite", () => { |
| 79 | + jest.spyOn(room, "canInvite").mockReturnValue(true); |
| 80 | + mocked(shouldShowComponent).mockReturnValue(true); |
| 81 | + |
| 82 | + getComponent(); |
| 83 | + |
| 84 | + expect(screen.getByRole("menuitem", { name: "Invite" })).toBeInTheDocument(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments