2022-02-16 12:19:28 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-02-16 12:19:28 +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-16 12:19:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2022-12-29 09:40:42 +01:00
|
|
|
import { render, screen, waitFor } from "@testing-library/react";
|
2022-02-16 12:19:28 +01:00
|
|
|
import { jest } from "@jest/globals";
|
2022-06-17 22:57:40 +02:00
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
2023-08-09 09:18:41 +02:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2022-02-16 12:19:28 +01:00
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
import _RightPanel from "../../../src/components/structures/RightPanel";
|
2022-02-16 12:19:28 +01:00
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
|
|
|
import ResizeNotifier from "../../../src/utils/ResizeNotifier";
|
2022-11-18 20:05:00 +01:00
|
|
|
import { stubClient, wrapInMatrixClientContext, mkRoom, wrapInSdkContext } from "../../test-utils";
|
2022-02-16 12:19:28 +01:00
|
|
|
import { Action } from "../../../src/dispatcher/actions";
|
|
|
|
import dis from "../../../src/dispatcher/dispatcher";
|
|
|
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
|
|
|
import SettingsStore from "../../../src/settings/SettingsStore";
|
|
|
|
import { RightPanelPhases } from "../../../src/stores/right-panel/RightPanelStorePhases";
|
|
|
|
import RightPanelStore from "../../../src/stores/right-panel/RightPanelStore";
|
|
|
|
import { UPDATE_EVENT } from "../../../src/stores/AsyncStore";
|
2022-02-17 17:30:36 +01:00
|
|
|
import { WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
2022-11-18 20:05:00 +01:00
|
|
|
import { SdkContextClass } from "../../../src/contexts/SDKContext";
|
2023-03-16 12:07:29 +01:00
|
|
|
import { RoomPermalinkCreator } from "../../../src/utils/permalinks/Permalinks";
|
2022-06-17 22:57:40 +02:00
|
|
|
|
2022-11-18 20:05:00 +01:00
|
|
|
const RightPanelBase = wrapInMatrixClientContext(_RightPanel);
|
2022-02-16 12:19:28 +01:00
|
|
|
|
|
|
|
describe("RightPanel", () => {
|
2022-06-17 22:57:40 +02:00
|
|
|
const resizeNotifier = new ResizeNotifier();
|
2022-02-16 12:19:28 +01:00
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
let cli: MockedObject<MatrixClient>;
|
2022-11-18 20:05:00 +01:00
|
|
|
let context: SdkContextClass;
|
|
|
|
let RightPanel: React.ComponentType<React.ComponentProps<typeof RightPanelBase>>;
|
2022-06-17 22:57:40 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-05 19:12:23 +02:00
|
|
|
cli = mocked(MatrixClientPeg.safeGet());
|
2023-05-23 17:24:12 +02:00
|
|
|
DMRoomMap.makeShared(cli);
|
2022-11-18 20:05:00 +01:00
|
|
|
context = new SdkContextClass();
|
|
|
|
context.client = cli;
|
|
|
|
RightPanel = wrapInSdkContext(RightPanelBase, context);
|
2022-06-17 22:57:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const roomChanged = new Promise<void>((resolve) => {
|
|
|
|
const ref = dis.register((payload) => {
|
2022-06-17 22:57:40 +02:00
|
|
|
if (payload.action === Action.ActiveRoomChanged) {
|
|
|
|
dis.unregister(ref);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
dis.fire(Action.ViewHomePage); // Stop viewing any rooms
|
|
|
|
await roomChanged;
|
|
|
|
|
|
|
|
dis.fire(Action.OnLoggedOut, true); // Shut down the stores
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
const spinUpStores = async () => {
|
|
|
|
// Selectively spin up the stores we need
|
|
|
|
WidgetLayoutStore.instance.useUnitTestClient(cli);
|
|
|
|
// @ts-ignore
|
|
|
|
// This is private but it's the only way to selectively enable stores
|
|
|
|
await WidgetLayoutStore.instance.onReady();
|
|
|
|
|
|
|
|
// Make sure we start with a clean store
|
|
|
|
RightPanelStore.instance.reset();
|
|
|
|
RightPanelStore.instance.useUnitTestClient(cli);
|
|
|
|
// @ts-ignore
|
|
|
|
await RightPanelStore.instance.onReady();
|
|
|
|
};
|
2022-02-16 12:19:28 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const waitForRpsUpdate = () => new Promise<void>((resolve) => RightPanelStore.instance.once(UPDATE_EVENT, resolve));
|
2022-02-16 12:19:28 +01:00
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
it("renders info from only one room during room changes", async () => {
|
|
|
|
const r1 = mkRoom(cli, "r1");
|
|
|
|
const r2 = mkRoom(cli, "r2");
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
cli.getRoom.mockImplementation((roomId) => {
|
2022-02-16 12:19:28 +01:00
|
|
|
if (roomId === "r1") return r1;
|
|
|
|
if (roomId === "r2") return r2;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set up right panel state
|
|
|
|
const realGetValue = SettingsStore.getValue;
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name, roomId) => {
|
|
|
|
if (name !== "RightPanel.phases") return realGetValue(name, roomId);
|
|
|
|
if (roomId === "r1") {
|
|
|
|
return {
|
|
|
|
history: [{ phase: RightPanelPhases.RoomMemberList }],
|
|
|
|
isOpen: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (roomId === "r2") {
|
|
|
|
return {
|
|
|
|
history: [{ phase: RightPanelPhases.RoomSummary }],
|
|
|
|
isOpen: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
await spinUpStores();
|
2022-02-16 12:19:28 +01:00
|
|
|
|
|
|
|
// Run initial render with room 1, and also running lifecycle methods
|
2023-03-16 12:07:29 +01:00
|
|
|
const { container, rerender } = render(
|
|
|
|
<RightPanel
|
|
|
|
room={r1}
|
|
|
|
resizeNotifier={resizeNotifier}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(r1, r1.roomId)}
|
|
|
|
/>,
|
|
|
|
);
|
2022-02-16 12:19:28 +01:00
|
|
|
// Wait for RPS room 1 updates to fire
|
2022-06-17 22:57:40 +02:00
|
|
|
const rpsUpdated = waitForRpsUpdate();
|
2022-02-16 12:19:28 +01:00
|
|
|
dis.dispatch({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: "r1",
|
|
|
|
});
|
|
|
|
await rpsUpdated;
|
2022-12-29 09:40:42 +01:00
|
|
|
await waitFor(() => expect(screen.queryByTestId("spinner")).not.toBeInTheDocument());
|
2022-02-16 12:19:28 +01:00
|
|
|
|
2022-12-29 09:40:42 +01:00
|
|
|
// room one will be in the RoomMemberList phase - confirm this is rendered
|
|
|
|
expect(container.getElementsByClassName("mx_MemberList")).toHaveLength(1);
|
2022-02-16 12:19:28 +01:00
|
|
|
|
2022-12-29 09:40:42 +01:00
|
|
|
// wait for RPS room 2 updates to fire, then rerender
|
|
|
|
const _rpsUpdated = waitForRpsUpdate();
|
2022-02-16 12:19:28 +01:00
|
|
|
dis.dispatch({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: "r2",
|
|
|
|
});
|
2022-12-29 09:40:42 +01:00
|
|
|
await _rpsUpdated;
|
2023-03-16 12:07:29 +01:00
|
|
|
rerender(
|
|
|
|
<RightPanel
|
|
|
|
room={r2}
|
|
|
|
resizeNotifier={resizeNotifier}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(r2, r2.roomId)}
|
|
|
|
/>,
|
|
|
|
);
|
2022-02-16 12:19:28 +01:00
|
|
|
|
2022-12-29 09:40:42 +01:00
|
|
|
// After all that setup, now to the interesting part...
|
|
|
|
// We want to verify that as we change to room 2, we should always have
|
|
|
|
// the correct right panel state for whichever room we are showing, so we
|
|
|
|
// confirm we do not have the MemberList class on the page and that we have
|
|
|
|
// the expected room title
|
|
|
|
expect(container.getElementsByClassName("mx_MemberList")).toHaveLength(0);
|
|
|
|
expect(screen.getByRole("heading", { name: "r2" })).toBeInTheDocument();
|
2022-02-16 12:19:28 +01:00
|
|
|
});
|
|
|
|
});
|