Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 95820fd

Browse files
author
Mikhail Aheichyk
committed
Use UIComponent.ExploreRooms to display menu item "Explore public rooms" ("Explore rooms" in case of space room) in context menu of "Rooms" section of RoomList component. The context menu will be available if one of UIComponent.ExploreRooms or UIComponent.CreateRooms is enabled.
Signed-off-by: Mikhail Aheichyk <[email protected]>
1 parent ac1ea8e commit 95820fd

File tree

2 files changed

+261
-41
lines changed

2 files changed

+261
-41
lines changed

Diff for: src/components/views/rooms/RoomList.tsx

+51-41
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ const UntaggedAuxButton: React.FC<IAuxButtonProps> = ({ tabIndex }) => {
213213
});
214214

215215
const showCreateRoom = shouldShowComponent(UIComponent.CreateRooms);
216+
const showExploreRooms = shouldShowComponent(UIComponent.ExploreRooms);
217+
216218
const videoRoomsEnabled = useFeatureEnabled("feature_video_rooms");
217219
const elementCallVideoRoomsEnabled = useFeatureEnabled("feature_element_call_video_rooms");
218220

@@ -225,21 +227,23 @@ const UntaggedAuxButton: React.FC<IAuxButtonProps> = ({ tabIndex }) => {
225227

226228
contextMenuContent = (
227229
<IconizedContextMenuOptionList first>
228-
<IconizedContextMenuOption
229-
label={_t("Explore rooms")}
230-
iconClassName="mx_RoomList_iconExplore"
231-
onClick={(e) => {
232-
e.preventDefault();
233-
e.stopPropagation();
234-
closeMenu();
235-
defaultDispatcher.dispatch<ViewRoomPayload>({
236-
action: Action.ViewRoom,
237-
room_id: activeSpace.roomId,
238-
metricsTrigger: undefined, // other
239-
});
240-
PosthogTrackers.trackInteraction("WebRoomListRoomsSublistPlusMenuExploreRoomsItem", e);
241-
}}
242-
/>
230+
{showExploreRooms ? (
231+
<IconizedContextMenuOption
232+
label={_t("Explore rooms")}
233+
iconClassName="mx_RoomList_iconExplore"
234+
onClick={(e) => {
235+
e.preventDefault();
236+
e.stopPropagation();
237+
closeMenu();
238+
defaultDispatcher.dispatch<ViewRoomPayload>({
239+
action: Action.ViewRoom,
240+
room_id: activeSpace.roomId,
241+
metricsTrigger: undefined, // other
242+
});
243+
PosthogTrackers.trackInteraction("WebRoomListRoomsSublistPlusMenuExploreRoomsItem", e);
244+
}}
245+
/>
246+
) : null}
243247
{showCreateRoom ? (
244248
<>
245249
<IconizedContextMenuOption
@@ -337,17 +341,19 @@ const UntaggedAuxButton: React.FC<IAuxButtonProps> = ({ tabIndex }) => {
337341
)}
338342
</>
339343
)}
340-
<IconizedContextMenuOption
341-
label={_t("Explore public rooms")}
342-
iconClassName="mx_RoomList_iconExplore"
343-
onClick={(e) => {
344-
e.preventDefault();
345-
e.stopPropagation();
346-
closeMenu();
347-
PosthogTrackers.trackInteraction("WebRoomListRoomsSublistPlusMenuExploreRoomsItem", e);
348-
defaultDispatcher.fire(Action.ViewRoomDirectory);
349-
}}
350-
/>
344+
{showExploreRooms ? (
345+
<IconizedContextMenuOption
346+
label={_t("Explore public rooms")}
347+
iconClassName="mx_RoomList_iconExplore"
348+
onClick={(e) => {
349+
e.preventDefault();
350+
e.stopPropagation();
351+
closeMenu();
352+
PosthogTrackers.trackInteraction("WebRoomListRoomsSublistPlusMenuExploreRoomsItem", e);
353+
defaultDispatcher.fire(Action.ViewRoomDirectory);
354+
}}
355+
/>
356+
) : null}
351357
</IconizedContextMenuOptionList>
352358
);
353359
}
@@ -361,22 +367,26 @@ const UntaggedAuxButton: React.FC<IAuxButtonProps> = ({ tabIndex }) => {
361367
);
362368
}
363369

364-
return (
365-
<>
366-
<ContextMenuTooltipButton
367-
tabIndex={tabIndex}
368-
onClick={openMenu}
369-
className="mx_RoomSublist_auxButton"
370-
tooltipClassName="mx_RoomSublist_addRoomTooltip"
371-
aria-label={_t("Add room")}
372-
title={_t("Add room")}
373-
isExpanded={menuDisplayed}
374-
inputRef={handle}
375-
/>
370+
if (showCreateRoom || showExploreRooms) {
371+
return (
372+
<>
373+
<ContextMenuTooltipButton
374+
tabIndex={tabIndex}
375+
onClick={openMenu}
376+
className="mx_RoomSublist_auxButton"
377+
tooltipClassName="mx_RoomSublist_addRoomTooltip"
378+
aria-label={_t("Add room")}
379+
title={_t("Add room")}
380+
isExpanded={menuDisplayed}
381+
inputRef={handle}
382+
/>
376383

377-
{contextMenu}
378-
</>
379-
);
384+
{contextMenu}
385+
</>
386+
);
387+
}
388+
389+
return null;
380390
};
381391

382392
const TAG_AESTHETICS: TagAestheticsMap = {

Diff for: test/components/views/rooms/RoomList-test.tsx

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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

Comments
 (0)