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

Prevent soft crash around room list header context menu when space changes #8289

Merged
merged 4 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/components/views/rooms/RoomListHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ const RoomListHeader = ({ onVisibilityChange }: IProps) => {
}
});

const canShowMainMenu = activeSpace || spaceKey === MetaSpace.Home;

useEffect(() => {
if (mainMenuDisplayed && !canShowMainMenu) {
// Space changed under us and we no longer has a main menu to draw
closeMainMenu();
}
}, [closeMainMenu, canShowMainMenu, mainMenuDisplayed]);

// we pass null for the queryLength to inhibit the metrics hook for when there is no filterCondition
useWebSearchMetrics(count, filterCondition ? filterCondition.search.length : null, false);

Expand Down Expand Up @@ -168,7 +177,7 @@ const RoomListHeader = ({ onVisibilityChange }: IProps) => {
const canShowPlusMenu = canCreateRooms || canExploreRooms || activeSpace;

let contextMenu: JSX.Element;
if (mainMenuDisplayed) {
if (mainMenuDisplayed && mainMenuHandle.current) {
let ContextMenuComponent;
if (activeSpace) {
ContextMenuComponent = SpaceContextMenu;
Expand Down Expand Up @@ -364,7 +373,7 @@ const RoomListHeader = ({ onVisibilityChange }: IProps) => {
.join("\n");

let contextMenuButton: JSX.Element = <div className="mx_RoomListHeader_contextLessTitle">{ title }</div>;
if (activeSpace || spaceKey === MetaSpace.Home) {
if (canShowMainMenu) {
contextMenuButton = <ContextMenuTooltipButton
inputRef={mainMenuHandle}
onClick={openMainMenu}
Expand Down
136 changes: 136 additions & 0 deletions test/components/views/rooms/RoomListHeader-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
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.
*/

import React from 'react';
import { mount } from 'enzyme';
import { MatrixClient } from 'matrix-js-sdk/src/client';
import { act } from "react-dom/test-utils";

import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
import { MetaSpace } from "../../../../src/stores/spaces";
import RoomListHeader from "../../../../src/components/views/rooms/RoomListHeader";
import * as testUtils from "../../../test-utils";
import { createTestClient, mkSpace } from "../../../test-utils";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { SettingLevel } from "../../../../src/settings/SettingLevel";

describe("RoomListHeader", () => {
let client: MatrixClient;

beforeEach(() => {
client = createTestClient();
});

it("renders a main menu for the home space", () => {
act(() => {
SpaceStore.instance.setActiveSpace(MetaSpace.Home);
});

const wrapper = mount(<MatrixClientContext.Provider value={client}>
<RoomListHeader />
</MatrixClientContext.Provider>);

expect(wrapper.text()).toBe("Home");
act(() => {
wrapper.find('[aria-label="Home options"]').hostNodes().simulate("click");
});
wrapper.update();

const menu = wrapper.find(".mx_IconizedContextMenu");
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
expect(items).toHaveLength(1);
expect(items.at(0).text()).toBe("Show all rooms");
});

it("renders a main menu for spaces", async () => {
const testSpace = mkSpace(client, "!space:server");
testSpace.name = "Test Space";
client.getRoom = () => testSpace;

const getUserIdForRoomId = jest.fn();
const getDMRoomsForUserId = jest.fn();
// @ts-ignore
DMRoomMap.sharedInstance = { getUserIdForRoomId, getDMRoomsForUserId };

await testUtils.setupAsyncStoreWithClient(SpaceStore.instance, client);
act(() => {
SpaceStore.instance.setActiveSpace(testSpace.roomId);
});

const wrapper = mount(<MatrixClientContext.Provider value={client}>
<RoomListHeader />
</MatrixClientContext.Provider>);

expect(wrapper.text()).toBe("Test Space");
act(() => {
wrapper.find('[aria-label="Test Space menu"]').hostNodes().simulate("click");
});
wrapper.update();

const menu = wrapper.find(".mx_IconizedContextMenu");
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
expect(items).toHaveLength(6);
expect(items.at(0).text()).toBe("Space home");
expect(items.at(1).text()).toBe("Manage & explore rooms");
expect(items.at(2).text()).toBe("Preferences");
expect(items.at(3).text()).toBe("Settings");
expect(items.at(4).text()).toBe("Room");
expect(items.at(4).text()).toBe("Room");
});

it("closes menu if space changes from under it", async () => {
await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, {
[MetaSpace.Home]: true,
[MetaSpace.Favourites]: true,
});

const testSpace = mkSpace(client, "!space:server");
testSpace.name = "Test Space";
client.getRoom = () => testSpace;

const getUserIdForRoomId = jest.fn();
const getDMRoomsForUserId = jest.fn();
// @ts-ignore
DMRoomMap.sharedInstance = { getUserIdForRoomId, getDMRoomsForUserId };

await testUtils.setupAsyncStoreWithClient(SpaceStore.instance, client);
act(() => {
SpaceStore.instance.setActiveSpace(testSpace.roomId);
});

const wrapper = mount(<MatrixClientContext.Provider value={client}>
<RoomListHeader />
</MatrixClientContext.Provider>);

expect(wrapper.text()).toBe("Test Space");
act(() => {
wrapper.find('[aria-label="Test Space menu"]').hostNodes().simulate("click");
});
wrapper.update();

act(() => {
SpaceStore.instance.setActiveSpace(MetaSpace.Favourites);
});
wrapper.update();

expect(wrapper.text()).toBe("Favourites");

const menu = wrapper.find(".mx_IconizedContextMenu");
expect(menu).toHaveLength(0);
});
});
1 change: 1 addition & 0 deletions test/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export function mkStubRoom(roomId: string = null, name: string, client: MatrixCl
getJoinRule: jest.fn().mockReturnValue("invite"),
loadMembersIfNeeded: jest.fn(),
client,
canInvite: jest.fn(),
} as unknown as Room;
}

Expand Down