New room list: add space menu in room header (#29352)

* feat(new room list): add space menu in view model

* test(new room list): add space menu in view model

* feat(new room list): add space menu in room list header

* chore: update i18n

* test(new room list): add tests for space menu

* test(new room list): update room list tests

* test(e2e): add tests for space menu
This commit is contained in:
Florian Duros
2025-02-25 15:06:01 +01:00
committed by GitHub
parent 62a287219d
commit f95218e2b7
11 changed files with 611 additions and 76 deletions

View File

@@ -6,24 +6,34 @@
*/
import { renderHook } from "jest-matrix-react";
import { type MatrixClient, type Room, RoomType } from "matrix-js-sdk/src/matrix";
import { JoinRule, type MatrixClient, type Room, RoomType } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { useRoomListHeaderViewModel } from "../../../../../src/components/viewmodels/roomlist/RoomListHeaderViewModel";
import SpaceStore from "../../../../../src/stores/spaces/SpaceStore";
import { mkStubRoom, stubClient } from "../../../../test-utils";
import { mkStubRoom, stubClient, withClientContextRenderOptions } from "../../../../test-utils";
import { shouldShowComponent } from "../../../../../src/customisations/helpers/UIComponents";
import SettingsStore from "../../../../../src/settings/SettingsStore";
import defaultDispatcher from "../../../../../src/dispatcher/dispatcher";
import { Action } from "../../../../../src/dispatcher/actions";
import { showCreateNewRoom } from "../../../../../src/utils/space";
import {
shouldShowSpaceSettings,
showCreateNewRoom,
showSpaceInvite,
showSpacePreferences,
showSpaceSettings,
} from "../../../../../src/utils/space";
jest.mock("../../../../../src/customisations/helpers/UIComponents", () => ({
shouldShowComponent: jest.fn(),
}));
jest.mock("../../../../../src/utils/space", () => ({
shouldShowSpaceSettings: jest.fn(),
showCreateNewRoom: jest.fn(),
showSpaceInvite: jest.fn(),
showSpacePreferences: jest.fn(),
showSpaceSettings: jest.fn(),
}));
describe("useRoomListHeaderViewModel", () => {
@@ -39,15 +49,19 @@ describe("useRoomListHeaderViewModel", () => {
jest.resetAllMocks();
});
function render() {
return renderHook(() => useRoomListHeaderViewModel(), withClientContextRenderOptions(matrixClient));
}
describe("title", () => {
it("should return Home as title", () => {
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
expect(result.current.title).toStrictEqual("Home");
});
it("should return the current space name as title", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
expect(result.current.title).toStrictEqual("spaceName");
});
@@ -55,7 +69,7 @@ describe("useRoomListHeaderViewModel", () => {
it("should be displayComposeMenu=true and canCreateRoom=true if the user can creates room", () => {
mocked(shouldShowComponent).mockReturnValue(false);
const { result, rerender } = renderHook(() => useRoomListHeaderViewModel());
const { result, rerender } = render();
expect(result.current.displayComposeMenu).toBe(false);
expect(result.current.canCreateRoom).toBe(false);
@@ -65,15 +79,45 @@ describe("useRoomListHeaderViewModel", () => {
expect(result.current.canCreateRoom).toBe(true);
});
it("should be displaySpaceMenu=true if the user is in a space", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const { result } = render();
expect(result.current.displaySpaceMenu).toBe(true);
});
it("should be canInviteInSpace=true if the space join rule is public", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
jest.spyOn(space, "getJoinRule").mockReturnValue(JoinRule.Public);
const { result } = render();
expect(result.current.displaySpaceMenu).toBe(true);
});
it("should be canInviteInSpace=true if the user has the right", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
jest.spyOn(space, "canInvite").mockReturnValue(true);
const { result } = render();
expect(result.current.displaySpaceMenu).toBe(true);
});
it("should be canAccessSpaceSettings=true if the user has the right", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
mocked(shouldShowSpaceSettings).mockReturnValue(true);
const { result } = render();
expect(result.current.canAccessSpaceSettings).toBe(true);
});
it("should be canCreateVideoRoom=true if feature_video_rooms is enabled", () => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
expect(result.current.canCreateVideoRoom).toBe(true);
});
it("should fire Action.CreateChat when createChatRoom is called", () => {
const spy = jest.spyOn(defaultDispatcher, "fire");
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
result.current.createChatRoom(new Event("click"));
expect(spy).toHaveBeenCalledWith(Action.CreateChat);
@@ -81,7 +125,7 @@ describe("useRoomListHeaderViewModel", () => {
it("should fire Action.CreateRoom when createRoom is called", () => {
const spy = jest.spyOn(defaultDispatcher, "fire");
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
result.current.createRoom(new Event("click"));
expect(spy).toHaveBeenCalledWith(Action.CreateRoom);
@@ -89,7 +133,7 @@ describe("useRoomListHeaderViewModel", () => {
it("should call showCreateNewRoom when createRoom is called in a space", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
result.current.createRoom(new Event("click"));
expect(showCreateNewRoom).toHaveBeenCalledWith(space);
@@ -98,7 +142,7 @@ describe("useRoomListHeaderViewModel", () => {
it("should fire Action.CreateRoom with RoomType.UnstableCall when createVideoRoom is called and feature_element_call_video_rooms is enabled", () => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
const spy = jest.spyOn(defaultDispatcher, "dispatch");
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
result.current.createVideoRoom();
expect(spy).toHaveBeenCalledWith({ action: Action.CreateRoom, type: RoomType.UnstableCall });
@@ -107,7 +151,7 @@ describe("useRoomListHeaderViewModel", () => {
it("should fire Action.CreateRoom with RoomType.ElementVideo when createVideoRoom is called and feature_element_call_video_rooms is disabled", () => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
const spy = jest.spyOn(defaultDispatcher, "dispatch");
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
result.current.createVideoRoom();
expect(spy).toHaveBeenCalledWith({ action: Action.CreateRoom, type: RoomType.ElementVideo });
@@ -115,9 +159,42 @@ describe("useRoomListHeaderViewModel", () => {
it("should call showCreateNewRoom when createVideoRoom is called in a space", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const { result } = renderHook(() => useRoomListHeaderViewModel());
const { result } = render();
result.current.createVideoRoom();
expect(showCreateNewRoom).toHaveBeenCalledWith(space, RoomType.ElementVideo);
});
it("should fire Action.ViewRoom when openSpaceHome is called in a space", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const spy = jest.spyOn(defaultDispatcher, "dispatch");
const { result } = render();
result.current.openSpaceHome();
expect(spy).toHaveBeenCalledWith({ action: Action.ViewRoom, room_id: space.roomId, metricsTrigger: undefined });
});
it("should call showSpaceInvite when inviteInSpace is called in a space", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const { result } = render();
result.current.inviteInSpace();
expect(showSpaceInvite).toHaveBeenCalledWith(space);
});
it("should call showSpacePreferences when openSpacePreferences is called in a space", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const { result } = render();
result.current.openSpacePreferences();
expect(showSpacePreferences).toHaveBeenCalledWith(space);
});
it("should call showSpaceSettings when openSpaceSettings is called in a space", () => {
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(space);
const { result } = render();
result.current.openSpaceSettings();
expect(showSpaceSettings).toHaveBeenCalledWith(space);
});
});