|
| 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 React, { ComponentProps } from "react"; |
| 19 | +import { render, screen, within } from "@testing-library/react"; |
| 20 | +import userEvent from "@testing-library/user-event"; |
| 21 | +import { mocked } from "jest-mock"; |
| 22 | +import { Room } from "matrix-js-sdk/src/models/room"; |
| 23 | + |
| 24 | +import RoomList from "../../../../src/components/views/rooms/RoomList"; |
| 25 | +import ResizeNotifier from "../../../../src/utils/ResizeNotifier"; |
| 26 | +import { MetaSpace } from "../../../../src/stores/spaces"; |
| 27 | +import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents"; |
| 28 | +import { UIComponent } from "../../../../src/settings/UIFeature"; |
| 29 | +import dis from "../../../../src/dispatcher/dispatcher"; |
| 30 | +import { Action } from "../../../../src/dispatcher/actions"; |
| 31 | +import * as testUtils from "../../../test-utils"; |
| 32 | +import { mkSpace, stubClient } from "../../../test-utils"; |
| 33 | +import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; |
| 34 | +import SpaceStore from "../../../../src/stores/spaces/SpaceStore"; |
| 35 | +import DMRoomMap from "../../../../src/utils/DMRoomMap"; |
| 36 | + |
| 37 | +jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({ |
| 38 | + shouldShowComponent: jest.fn(), |
| 39 | +})); |
| 40 | + |
| 41 | +jest.mock("../../../../src/dispatcher/dispatcher"); |
| 42 | + |
| 43 | +const getUserIdForRoomId = jest.fn(); |
| 44 | +const getDMRoomsForUserId = jest.fn(); |
| 45 | +// @ts-ignore |
| 46 | +DMRoomMap.sharedInstance = { getUserIdForRoomId, getDMRoomsForUserId }; |
| 47 | + |
| 48 | +describe("RoomList", () => { |
| 49 | + stubClient(); |
| 50 | + const client = MatrixClientPeg.get(); |
| 51 | + const store = SpaceStore.instance; |
| 52 | + |
| 53 | + function getComponent(props: Partial<ComponentProps<typeof RoomList>> = {}): JSX.Element { |
| 54 | + return ( |
| 55 | + <RoomList |
| 56 | + onKeyDown={jest.fn()} |
| 57 | + onFocus={jest.fn()} |
| 58 | + onBlur={jest.fn()} |
| 59 | + onResize={jest.fn()} |
| 60 | + resizeNotifier={new ResizeNotifier()} |
| 61 | + isMinimized={false} |
| 62 | + activeSpace={MetaSpace.Home} |
| 63 | + {...props} |
| 64 | + /> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + describe("Rooms", () => { |
| 69 | + describe("when meta space is active", () => { |
| 70 | + beforeEach(() => { |
| 71 | + store.setActiveSpace(MetaSpace.Home); |
| 72 | + }); |
| 73 | + |
| 74 | + it("does not render add room button when UIComponent customisation disables CreateRooms and ExploreRooms", () => { |
| 75 | + const disabled: UIComponent[] = [UIComponent.CreateRooms, UIComponent.ExploreRooms]; |
| 76 | + mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature)); |
| 77 | + render(getComponent()); |
| 78 | + |
| 79 | + const roomsList = screen.getByRole("group", { name: "Rooms" }); |
| 80 | + expect(within(roomsList).queryByRole("button", { name: "Add room" })).not.toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("renders add room button with menu when UIComponent customisation allows CreateRooms or ExploreRooms", async () => { |
| 84 | + let disabled: UIComponent[] = []; |
| 85 | + mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature)); |
| 86 | + const { rerender } = render(getComponent()); |
| 87 | + |
| 88 | + const roomsList = screen.getByRole("group", { name: "Rooms" }); |
| 89 | + const addRoomButton = within(roomsList).getByRole("button", { name: "Add room" }); |
| 90 | + expect(screen.queryByRole("menu")).not.toBeInTheDocument(); |
| 91 | + |
| 92 | + await userEvent.click(addRoomButton); |
| 93 | + |
| 94 | + const menu = screen.getByRole("menu"); |
| 95 | + |
| 96 | + expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument(); |
| 97 | + expect(within(menu).getByRole("menuitem", { name: "Explore public rooms" })).toBeInTheDocument(); |
| 98 | + |
| 99 | + disabled = [UIComponent.CreateRooms]; |
| 100 | + rerender(getComponent()); |
| 101 | + |
| 102 | + expect(addRoomButton).toBeInTheDocument(); |
| 103 | + expect(menu).toBeInTheDocument(); |
| 104 | + expect(within(menu).queryByRole("menuitem", { name: "New room" })).not.toBeInTheDocument(); |
| 105 | + expect(within(menu).getByRole("menuitem", { name: "Explore public rooms" })).toBeInTheDocument(); |
| 106 | + |
| 107 | + disabled = [UIComponent.ExploreRooms]; |
| 108 | + rerender(getComponent()); |
| 109 | + |
| 110 | + expect(addRoomButton).toBeInTheDocument(); |
| 111 | + expect(menu).toBeInTheDocument(); |
| 112 | + expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument(); |
| 113 | + expect(within(menu).queryByRole("menuitem", { name: "Explore public rooms" })).not.toBeInTheDocument(); |
| 114 | + }); |
| 115 | + |
| 116 | + it("renders add room button and clicks explore public rooms", async () => { |
| 117 | + mocked(shouldShowComponent).mockReturnValue(true); |
| 118 | + render(getComponent()); |
| 119 | + |
| 120 | + const roomsList = screen.getByRole("group", { name: "Rooms" }); |
| 121 | + await userEvent.click(within(roomsList).getByRole("button", { name: "Add room" })); |
| 122 | + |
| 123 | + const menu = screen.getByRole("menu"); |
| 124 | + await userEvent.click(within(menu).getByRole("menuitem", { name: "Explore public rooms" })); |
| 125 | + |
| 126 | + expect(dis.fire).toHaveBeenCalledWith(Action.ViewRoomDirectory); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe("when room space is active", () => { |
| 131 | + let rooms: Room[]; |
| 132 | + const mkSpaceForRooms = (spaceId: string, children: string[] = []) => |
| 133 | + mkSpace(client, spaceId, rooms, children); |
| 134 | + |
| 135 | + const space1 = "!space1:server"; |
| 136 | + |
| 137 | + beforeEach(async () => { |
| 138 | + rooms = []; |
| 139 | + mkSpaceForRooms(space1); |
| 140 | + mocked(client).getRoom.mockImplementation( |
| 141 | + (roomId) => rooms.find((room) => room.roomId === roomId) || null, |
| 142 | + ); |
| 143 | + await testUtils.setupAsyncStoreWithClient(store, client); |
| 144 | + |
| 145 | + store.setActiveSpace(space1); |
| 146 | + }); |
| 147 | + |
| 148 | + it("does not render add room button when UIComponent customisation disables CreateRooms and ExploreRooms", () => { |
| 149 | + const disabled: UIComponent[] = [UIComponent.CreateRooms, UIComponent.ExploreRooms]; |
| 150 | + mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature)); |
| 151 | + render(getComponent()); |
| 152 | + |
| 153 | + const roomsList = screen.getByRole("group", { name: "Rooms" }); |
| 154 | + expect(within(roomsList).queryByRole("button", { name: "Add room" })).not.toBeInTheDocument(); |
| 155 | + }); |
| 156 | + |
| 157 | + it("renders add room button with menu when UIComponent customisation allows CreateRooms or ExploreRooms", async () => { |
| 158 | + let disabled: UIComponent[] = []; |
| 159 | + mocked(shouldShowComponent).mockImplementation((feature) => !disabled.includes(feature)); |
| 160 | + const { rerender } = render(getComponent()); |
| 161 | + |
| 162 | + const roomsList = screen.getByRole("group", { name: "Rooms" }); |
| 163 | + const addRoomButton = within(roomsList).getByRole("button", { name: "Add room" }); |
| 164 | + expect(screen.queryByRole("menu")).not.toBeInTheDocument(); |
| 165 | + |
| 166 | + await userEvent.click(addRoomButton); |
| 167 | + |
| 168 | + const menu = screen.getByRole("menu"); |
| 169 | + |
| 170 | + expect(within(menu).getByRole("menuitem", { name: "Explore rooms" })).toBeInTheDocument(); |
| 171 | + expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument(); |
| 172 | + expect(within(menu).getByRole("menuitem", { name: "Add existing room" })).toBeInTheDocument(); |
| 173 | + |
| 174 | + disabled = [UIComponent.CreateRooms]; |
| 175 | + rerender(getComponent()); |
| 176 | + |
| 177 | + expect(addRoomButton).toBeInTheDocument(); |
| 178 | + expect(menu).toBeInTheDocument(); |
| 179 | + expect(within(menu).getByRole("menuitem", { name: "Explore rooms" })).toBeInTheDocument(); |
| 180 | + expect(within(menu).queryByRole("menuitem", { name: "New room" })).not.toBeInTheDocument(); |
| 181 | + expect(within(menu).queryByRole("menuitem", { name: "Add existing room" })).not.toBeInTheDocument(); |
| 182 | + |
| 183 | + disabled = [UIComponent.ExploreRooms]; |
| 184 | + rerender(getComponent()); |
| 185 | + |
| 186 | + expect(addRoomButton).toBeInTheDocument(); |
| 187 | + expect(menu).toBeInTheDocument(); |
| 188 | + expect(within(menu).queryByRole("menuitem", { name: "Explore rooms" })).not.toBeInTheDocument(); |
| 189 | + expect(within(menu).getByRole("menuitem", { name: "New room" })).toBeInTheDocument(); |
| 190 | + expect(within(menu).getByRole("menuitem", { name: "Add existing room" })).toBeInTheDocument(); |
| 191 | + }); |
| 192 | + |
| 193 | + it("renders add room button and clicks explore rooms", async () => { |
| 194 | + mocked(shouldShowComponent).mockReturnValue(true); |
| 195 | + render(getComponent()); |
| 196 | + |
| 197 | + const roomsList = screen.getByRole("group", { name: "Rooms" }); |
| 198 | + await userEvent.click(within(roomsList).getByRole("button", { name: "Add room" })); |
| 199 | + |
| 200 | + const menu = screen.getByRole("menu"); |
| 201 | + await userEvent.click(within(menu).getByRole("menuitem", { name: "Explore rooms" })); |
| 202 | + |
| 203 | + expect(dis.dispatch).toHaveBeenCalledWith({ |
| 204 | + action: Action.ViewRoom, |
| 205 | + room_id: space1, |
| 206 | + }); |
| 207 | + }); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments