2022-02-09 17:21:40 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-02-09 17:21:40 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-02-09 17:21:40 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import React from "react";
|
2024-10-14 18:11:58 +02:00
|
|
|
import { render, screen, fireEvent, act, cleanup } from "jest-matrix-react";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { mocked } from "jest-mock";
|
2023-06-28 15:05:36 +02:00
|
|
|
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
2022-02-09 17:21:40 +01:00
|
|
|
|
2024-10-15 15:57:26 +02:00
|
|
|
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
|
|
|
|
import { MetaSpace, SpaceKey } from "../../../../../src/stores/spaces";
|
|
|
|
import { shouldShowComponent } from "../../../../../src/customisations/helpers/UIComponents";
|
|
|
|
import { UIComponent } from "../../../../../src/settings/UIFeature";
|
|
|
|
import { mkStubRoom, wrapInMatrixClientContext, wrapInSdkContext } from "../../../../test-utils";
|
|
|
|
import { SdkContextClass } from "../../../../../src/contexts/SDKContext";
|
|
|
|
import SpaceStore from "../../../../../src/stores/spaces/SpaceStore";
|
|
|
|
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
|
|
|
|
import { SpaceNotificationState } from "../../../../../src/stores/notifications/SpaceNotificationState";
|
|
|
|
import SettingsStore from "../../../../../src/settings/SettingsStore";
|
|
|
|
import UnwrappedSpacePanel from "../../../../../src/components/views/spaces/SpacePanel";
|
2023-05-17 16:18:21 +02:00
|
|
|
|
|
|
|
// DND test utilities based on
|
|
|
|
// https://github.com/colinrobertbrooks/react-beautiful-dnd-test-utils/issues/18#issuecomment-1373388693
|
|
|
|
enum Keys {
|
|
|
|
SPACE = 32,
|
|
|
|
ARROW_LEFT = 37,
|
|
|
|
ARROW_UP = 38,
|
|
|
|
ARROW_RIGHT = 39,
|
|
|
|
ARROW_DOWN = 40,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DragDirection {
|
|
|
|
LEFT = Keys.ARROW_LEFT,
|
|
|
|
UP = Keys.ARROW_UP,
|
|
|
|
RIGHT = Keys.ARROW_RIGHT,
|
|
|
|
DOWN = Keys.ARROW_DOWN,
|
|
|
|
}
|
|
|
|
|
|
|
|
// taken from https://github.com/hello-pangea/dnd/blob/main/test/unit/integration/util/controls.ts#L20
|
|
|
|
const createTransitionEndEvent = (): Event => {
|
|
|
|
const event = new Event("transitionend", {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
}) as TransitionEvent;
|
|
|
|
|
|
|
|
// cheating and adding property to event as
|
|
|
|
// TransitionEvent constructor does not exist.
|
|
|
|
// This is needed because of the following check
|
|
|
|
// https://github.com/atlassian/react-beautiful-dnd/blob/master/src/view/draggable/draggable.jsx#L130
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(event as any).propertyName = "transform";
|
|
|
|
|
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
|
|
|
const pickUp = async (element: HTMLElement) => {
|
|
|
|
fireEvent.keyDown(element, {
|
|
|
|
keyCode: Keys.SPACE,
|
|
|
|
});
|
|
|
|
await screen.findByText(/You have lifted an item/i);
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const move = async (element: HTMLElement, direction: DragDirection) => {
|
|
|
|
fireEvent.keyDown(element, {
|
|
|
|
keyCode: direction,
|
|
|
|
});
|
|
|
|
await screen.findByText(/(You have moved the item | has been combined with)/i);
|
|
|
|
};
|
|
|
|
|
|
|
|
const drop = async (element: HTMLElement) => {
|
|
|
|
fireEvent.keyDown(element, {
|
|
|
|
keyCode: Keys.SPACE,
|
|
|
|
});
|
|
|
|
fireEvent(element.parentElement!, createTransitionEndEvent());
|
|
|
|
|
|
|
|
await screen.findByText(/You have dropped the item/i);
|
|
|
|
};
|
2022-02-09 17:21:40 +01:00
|
|
|
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../../../src/stores/spaces/SpaceStore", () => {
|
2024-10-16 17:43:07 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
2022-02-09 17:21:40 +01:00
|
|
|
const EventEmitter = require("events");
|
|
|
|
class MockSpaceStore extends EventEmitter {
|
2023-02-13 12:39:16 +01:00
|
|
|
invitedSpaces: SpaceKey[] = [];
|
|
|
|
enabledMetaSpaces: MetaSpace[] = [];
|
|
|
|
spacePanelSpaces: string[] = [];
|
2022-12-12 12:24:14 +01:00
|
|
|
activeSpace: SpaceKey = "!space1";
|
2023-06-28 15:05:36 +02:00
|
|
|
getChildSpaces = () => [] as Room[];
|
|
|
|
getNotificationState = () => null as SpaceNotificationState | null;
|
2023-05-17 16:18:21 +02:00
|
|
|
setActiveSpace = jest.fn();
|
|
|
|
moveRootSpace = jest.fn();
|
2022-02-09 17:21:40 +01:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
instance: new MockSpaceStore(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../../../src/customisations/helpers/UIComponents", () => ({
|
2022-02-09 17:21:40 +01:00
|
|
|
shouldShowComponent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("<SpacePanel />", () => {
|
2022-02-09 17:21:40 +01:00
|
|
|
const mockClient = {
|
2022-12-12 12:24:14 +01:00
|
|
|
getUserId: jest.fn().mockReturnValue("@test:test"),
|
2023-02-28 09:55:59 +01:00
|
|
|
getSafeUserId: jest.fn().mockReturnValue("@test:test"),
|
2023-05-17 16:18:21 +02:00
|
|
|
mxcUrlToHttp: jest.fn(),
|
|
|
|
getRoom: jest.fn(),
|
2022-02-09 17:21:40 +01:00
|
|
|
isGuest: jest.fn(),
|
|
|
|
getAccountData: jest.fn(),
|
2023-05-17 16:18:21 +02:00
|
|
|
on: jest.fn(),
|
2023-08-21 11:39:20 +02:00
|
|
|
off: jest.fn(),
|
2023-05-17 16:18:21 +02:00
|
|
|
removeListener: jest.fn(),
|
2023-08-21 11:39:20 +02:00
|
|
|
isVersionSupported: jest.fn().mockResolvedValue(true),
|
|
|
|
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(false),
|
2022-02-09 17:21:40 +01:00
|
|
|
} as unknown as MatrixClient;
|
2023-08-21 11:39:20 +02:00
|
|
|
const SpacePanel = wrapInSdkContext(wrapInMatrixClientContext(UnwrappedSpacePanel), SdkContextClass.instance);
|
2022-02-09 17:21:40 +01:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
2023-06-15 09:46:19 +02:00
|
|
|
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(mockClient);
|
2022-02-09 17:21:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-03-25 19:35:31 +01:00
|
|
|
SpaceStore.instance.enabledMetaSpaces.push(
|
|
|
|
MetaSpace.Home,
|
|
|
|
MetaSpace.Favourites,
|
|
|
|
MetaSpace.People,
|
|
|
|
MetaSpace.Orphans,
|
|
|
|
MetaSpace.VideoRooms,
|
|
|
|
);
|
2022-02-09 17:21:40 +01:00
|
|
|
mocked(shouldShowComponent).mockClear().mockReturnValue(true);
|
|
|
|
});
|
2024-03-25 19:35:31 +01:00
|
|
|
afterEach(() => {
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show all activated MetaSpaces in the correct order", async () => {
|
|
|
|
const originalGetValue = SettingsStore.getValue;
|
|
|
|
const spySettingsStore = jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => {
|
|
|
|
return setting === "feature_video_rooms" ? true : originalGetValue(setting);
|
|
|
|
});
|
|
|
|
const renderResult = render(<SpacePanel />);
|
|
|
|
expect(renderResult.asFragment()).toMatchSnapshot();
|
|
|
|
spySettingsStore.mockRestore();
|
|
|
|
});
|
2022-02-09 17:21:40 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("create new space button", () => {
|
|
|
|
it("renders create space button when UIComponent.CreateSpaces component should be shown", () => {
|
2022-10-17 18:42:04 +02:00
|
|
|
render(<SpacePanel />);
|
|
|
|
screen.getByTestId("create-space-button");
|
2022-02-09 17:21:40 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not render create space button when UIComponent.CreateSpaces component should not be shown", () => {
|
2022-02-09 17:21:40 +01:00
|
|
|
mocked(shouldShowComponent).mockReturnValue(false);
|
2022-10-17 18:42:04 +02:00
|
|
|
render(<SpacePanel />);
|
2022-02-09 17:21:40 +01:00
|
|
|
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.CreateSpaces);
|
2022-10-17 18:42:04 +02:00
|
|
|
expect(screen.queryByTestId("create-space-button")).toBeFalsy();
|
2022-02-09 17:21:40 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("opens context menu on create space button click", () => {
|
2022-10-17 18:42:04 +02:00
|
|
|
render(<SpacePanel />);
|
|
|
|
fireEvent.click(screen.getByTestId("create-space-button"));
|
|
|
|
screen.getByTestId("create-space-button");
|
2022-02-09 17:21:40 +01:00
|
|
|
});
|
|
|
|
});
|
2023-05-17 16:18:21 +02:00
|
|
|
|
|
|
|
it("should allow rearranging via drag and drop", async () => {
|
|
|
|
(SpaceStore.instance.spacePanelSpaces as any) = [
|
|
|
|
mkStubRoom("!room1:server", "Room 1", mockClient),
|
|
|
|
mkStubRoom("!room2:server", "Room 2", mockClient),
|
|
|
|
mkStubRoom("!room3:server", "Room 3", mockClient),
|
|
|
|
];
|
2023-05-23 17:24:12 +02:00
|
|
|
DMRoomMap.makeShared(mockClient);
|
2023-05-17 16:18:21 +02:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
const { getByLabelText } = render(<SpacePanel />);
|
|
|
|
|
|
|
|
const room1 = getByLabelText("Room 1");
|
|
|
|
await pickUp(room1);
|
|
|
|
await move(room1, DragDirection.DOWN);
|
|
|
|
await drop(room1);
|
|
|
|
|
|
|
|
expect(SpaceStore.instance.moveRootSpace).toHaveBeenCalledWith(0, 1);
|
|
|
|
});
|
2022-02-09 17:21:40 +01:00
|
|
|
});
|