2022-04-12 16:23:04 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import React from "react";
|
2023-08-09 09:18:41 +02:00
|
|
|
import { MatrixClient, Room, EventType } from "matrix-js-sdk/src/matrix";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { mocked } from "jest-mock";
|
2023-02-22 11:52:55 +01:00
|
|
|
import { act, render, screen, fireEvent, RenderResult } from "@testing-library/react";
|
2022-04-12 16:23:04 +02:00
|
|
|
|
|
|
|
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
|
|
|
|
import { MetaSpace } from "../../../../src/stores/spaces";
|
2022-08-03 17:19:24 +02:00
|
|
|
import _RoomListHeader from "../../../../src/components/views/rooms/RoomListHeader";
|
2022-04-12 16:23:04 +02:00
|
|
|
import * as testUtils from "../../../test-utils";
|
2022-08-03 17:19:24 +02:00
|
|
|
import { stubClient, mkSpace } from "../../../test-utils";
|
2022-04-12 16:23:04 +02:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2022-08-03 17:19:24 +02:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-04-12 16:23:04 +02:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
|
|
|
|
import { UIComponent } from "../../../../src/settings/UIFeature";
|
2022-08-03 17:19:24 +02:00
|
|
|
|
|
|
|
const RoomListHeader = testUtils.wrapInMatrixClientContext(_RoomListHeader);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
|
2022-08-03 17:19:24 +02:00
|
|
|
shouldShowComponent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const blockUIComponent = (component: UIComponent): void => {
|
2022-12-12 12:24:14 +01:00
|
|
|
mocked(shouldShowComponent).mockImplementation((feature) => feature !== component);
|
2022-08-03 17:19:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const setupSpace = (client: MatrixClient): Room => {
|
|
|
|
const testSpace: Room = mkSpace(client, "!space:server");
|
|
|
|
testSpace.name = "Test Space";
|
|
|
|
client.getRoom = () => testSpace;
|
|
|
|
return testSpace;
|
|
|
|
};
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const setupMainMenu = async (client: MatrixClient, testSpace: Room): Promise<RenderResult> => {
|
2022-08-03 17:19:24 +02:00
|
|
|
await testUtils.setupAsyncStoreWithClient(SpaceStore.instance, client);
|
|
|
|
act(() => {
|
|
|
|
SpaceStore.instance.setActiveSpace(testSpace.roomId);
|
|
|
|
});
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const wrapper = render(<RoomListHeader />);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
expect(wrapper.container.textContent).toBe("Test Space");
|
2022-08-03 17:19:24 +02:00
|
|
|
act(() => {
|
2023-02-13 18:01:43 +01:00
|
|
|
wrapper.container.querySelector<HTMLElement>('[aria-label="Test Space menu"]')?.click();
|
2022-08-03 17:19:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const setupPlusMenu = async (client: MatrixClient, testSpace: Room): Promise<RenderResult> => {
|
2022-08-03 17:19:24 +02:00
|
|
|
await testUtils.setupAsyncStoreWithClient(SpaceStore.instance, client);
|
|
|
|
act(() => {
|
|
|
|
SpaceStore.instance.setActiveSpace(testSpace.roomId);
|
|
|
|
});
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const wrapper = render(<RoomListHeader />);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
expect(wrapper.container.textContent).toBe("Test Space");
|
2022-08-03 17:19:24 +02:00
|
|
|
act(() => {
|
2023-02-13 18:01:43 +01:00
|
|
|
wrapper.container.querySelector<HTMLElement>('[aria-label="Add"]')?.click();
|
2022-08-03 17:19:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const checkIsDisabled = (menuItem: HTMLElement): void => {
|
|
|
|
expect(menuItem).toHaveAttribute("disabled");
|
|
|
|
expect(menuItem).toHaveAttribute("aria-disabled", "true");
|
2022-08-03 17:19:24 +02:00
|
|
|
};
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const checkMenuLabels = (items: NodeListOf<Element>, labelArray: Array<string>) => {
|
2022-08-03 17:19:24 +02:00
|
|
|
expect(items).toHaveLength(labelArray.length);
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const checkLabel = (item: Element, label: string) => {
|
2023-02-13 18:01:43 +01:00
|
|
|
expect(item.querySelector(".mx_IconizedContextMenu_label")?.textContent).toBe(label);
|
2022-08-03 17:19:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
labelArray.forEach((label, index) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
console.log("index", index, "label", label);
|
2022-10-10 17:29:10 +02:00
|
|
|
checkLabel(items[index], label);
|
2022-08-03 17:19:24 +02:00
|
|
|
});
|
|
|
|
};
|
2022-04-12 16:23:04 +02:00
|
|
|
|
|
|
|
describe("RoomListHeader", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-08-03 17:19:24 +02:00
|
|
|
jest.resetAllMocks();
|
|
|
|
|
|
|
|
const dmRoomMap = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
getDMRoomsForUserId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
stubClient();
|
2023-06-05 19:12:23 +02:00
|
|
|
client = MatrixClientPeg.safeGet();
|
2022-08-03 17:19:24 +02:00
|
|
|
mocked(shouldShowComponent).mockReturnValue(true); // show all UIComponents
|
2022-04-12 16:23:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders a main menu for the home space", () => {
|
|
|
|
act(() => {
|
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Home);
|
|
|
|
});
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const { container } = render(<RoomListHeader />);
|
2022-04-12 16:23:04 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
expect(container.textContent).toBe("Home");
|
|
|
|
fireEvent.click(screen.getByLabelText("Home options"));
|
2022-04-12 16:23:04 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll(".mx_IconizedContextMenu_item");
|
2022-04-12 16:23:04 +02:00
|
|
|
expect(items).toHaveLength(1);
|
2022-10-10 17:29:10 +02:00
|
|
|
expect(items[0].textContent).toBe("Show all rooms");
|
2022-04-12 16:23:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders a main menu for spaces", async () => {
|
2022-08-03 17:19:24 +02:00
|
|
|
const testSpace = setupSpace(client);
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupMainMenu(client, testSpace);
|
2022-04-12 16:23:04 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll(".mx_IconizedContextMenu_item");
|
2022-04-12 16:23:04 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
checkMenuLabels(items, ["Space home", "Manage & explore rooms", "Preferences", "Settings", "Room", "Space"]);
|
2022-08-03 17:19:24 +02:00
|
|
|
});
|
2022-04-12 16:23:04 +02:00
|
|
|
|
2022-08-03 17:19:24 +02:00
|
|
|
it("renders a plus menu for spaces", async () => {
|
|
|
|
const testSpace = setupSpace(client);
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupPlusMenu(client, testSpace);
|
2022-04-12 16:23:04 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll(".mx_IconizedContextMenu_item");
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
checkMenuLabels(items, ["New room", "Explore rooms", "Add existing room", "Add space"]);
|
2022-04-12 16:23:04 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("closes menu if space changes from under it", async () => {
|
|
|
|
await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, {
|
|
|
|
[MetaSpace.Home]: true,
|
|
|
|
[MetaSpace.Favourites]: true,
|
|
|
|
});
|
|
|
|
|
2022-08-03 17:19:24 +02:00
|
|
|
const testSpace = setupSpace(client);
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupMainMenu(client, testSpace);
|
2022-04-12 16:23:04 +02:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Favourites);
|
|
|
|
});
|
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
screen.getByText("Favourites");
|
|
|
|
expect(screen.queryByRole("menu")).toBeFalsy();
|
2022-04-12 16:23:04 +02:00
|
|
|
});
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("UIComponents", () => {
|
|
|
|
describe("Main menu", () => {
|
|
|
|
it("does not render Add Space when user does not have permission to add spaces", async () => {
|
2022-08-03 17:19:24 +02:00
|
|
|
// User does not have permission to add spaces, anywhere
|
|
|
|
blockUIComponent(UIComponent.CreateSpaces);
|
|
|
|
|
|
|
|
const testSpace = setupSpace(client);
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupMainMenu(client, testSpace);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll(".mx_IconizedContextMenu_item");
|
2022-08-03 17:19:24 +02:00
|
|
|
checkMenuLabels(items, [
|
|
|
|
"Space home",
|
|
|
|
"Manage & explore rooms",
|
|
|
|
"Preferences",
|
|
|
|
"Settings",
|
|
|
|
"Room",
|
|
|
|
// no add space
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not render Add Room when user does not have permission to add rooms", async () => {
|
2022-08-03 17:19:24 +02:00
|
|
|
// User does not have permission to add rooms
|
|
|
|
blockUIComponent(UIComponent.CreateRooms);
|
|
|
|
|
|
|
|
const testSpace = setupSpace(client);
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupMainMenu(client, testSpace);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll(".mx_IconizedContextMenu_item");
|
2022-08-03 17:19:24 +02:00
|
|
|
checkMenuLabels(items, [
|
|
|
|
"Space home",
|
|
|
|
"Explore rooms", // not Manage & explore rooms
|
|
|
|
"Preferences",
|
|
|
|
"Settings",
|
|
|
|
// no add room
|
|
|
|
"Space",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Plus menu", () => {
|
|
|
|
it("does not render Add Space when user does not have permission to add spaces", async () => {
|
2022-08-03 17:19:24 +02:00
|
|
|
// User does not have permission to add spaces, anywhere
|
|
|
|
blockUIComponent(UIComponent.CreateSpaces);
|
|
|
|
|
|
|
|
const testSpace = setupSpace(client);
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupPlusMenu(client, testSpace);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll(".mx_IconizedContextMenu_item");
|
2022-08-03 17:19:24 +02:00
|
|
|
|
|
|
|
checkMenuLabels(items, [
|
|
|
|
"New room",
|
|
|
|
"Explore rooms",
|
|
|
|
"Add existing room",
|
|
|
|
// no Add space
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("disables Add Room when user does not have permission to add rooms", async () => {
|
2022-08-03 17:19:24 +02:00
|
|
|
// User does not have permission to add rooms
|
|
|
|
blockUIComponent(UIComponent.CreateRooms);
|
|
|
|
|
|
|
|
const testSpace = setupSpace(client);
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupPlusMenu(client, testSpace);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll<HTMLElement>(".mx_IconizedContextMenu_item");
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
checkMenuLabels(items, ["New room", "Explore rooms", "Add existing room", "Add space"]);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
|
|
|
// "Add existing room" is disabled
|
2022-10-10 17:29:10 +02:00
|
|
|
checkIsDisabled(items[2]);
|
2022-08-03 17:19:24 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("adding children to space", () => {
|
|
|
|
it("if user cannot add children to space, MainMenu adding buttons are hidden", async () => {
|
2022-08-03 17:19:24 +02:00
|
|
|
const testSpace = setupSpace(client);
|
|
|
|
mocked(testSpace.currentState.maySendStateEvent).mockImplementation(
|
2022-12-12 12:24:14 +01:00
|
|
|
(stateEventType, userId) => stateEventType !== EventType.SpaceChild,
|
|
|
|
);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupMainMenu(client, testSpace);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll(".mx_IconizedContextMenu_item");
|
2022-08-03 17:19:24 +02:00
|
|
|
checkMenuLabels(items, [
|
|
|
|
"Space home",
|
|
|
|
"Explore rooms", // not Manage & explore rooms
|
|
|
|
"Preferences",
|
|
|
|
"Settings",
|
|
|
|
// no add room
|
|
|
|
// no add space
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("if user cannot add children to space, PlusMenu add buttons are disabled", async () => {
|
2022-08-03 17:19:24 +02:00
|
|
|
const testSpace = setupSpace(client);
|
|
|
|
mocked(testSpace.currentState.maySendStateEvent).mockImplementation(
|
2022-12-12 12:24:14 +01:00
|
|
|
(stateEventType, userId) => stateEventType !== EventType.SpaceChild,
|
|
|
|
);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
await setupPlusMenu(client, testSpace);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-10-10 17:29:10 +02:00
|
|
|
const menu = screen.getByRole("menu");
|
|
|
|
const items = menu.querySelectorAll<HTMLElement>(".mx_IconizedContextMenu_item");
|
2022-08-03 17:19:24 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
checkMenuLabels(items, ["New room", "Explore rooms", "Add existing room", "Add space"]);
|
2022-08-03 17:19:24 +02:00
|
|
|
|
|
|
|
// "Add existing room" is disabled
|
2022-10-10 17:29:10 +02:00
|
|
|
checkIsDisabled(items[2]);
|
2022-08-03 17:19:24 +02:00
|
|
|
// "Add space" is disabled
|
2022-10-10 17:29:10 +02:00
|
|
|
checkIsDisabled(items[3]);
|
2022-08-03 17:19:24 +02:00
|
|
|
});
|
|
|
|
});
|
2022-04-12 16:23:04 +02:00
|
|
|
});
|