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

pull/21833/head
Michael Telatynski 2022-04-12 15:23:04 +01:00 committed by GitHub
parent 59fda5273f
commit 8cd715c6a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 2 deletions

View File

@ -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);
@ -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;
@ -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}

View File

@ -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);
});
});

View File

@ -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;
}