2023-01-27 11:00:06 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-01-27 11:00:06 +00:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2023-01-27 11:00:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { fireEvent, render, RenderResult, screen } from "jest-matrix-react";
|
2023-08-09 07:18:41 +00:00
|
|
|
import { MatrixClient, Room, EventType, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2023-01-27 11:00:06 +00:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import AdvancedRoomSettingsTab from "../../../../../../../src/components/views/settings/tabs/room/AdvancedRoomSettingsTab";
|
|
|
|
import { mkEvent, mkStubRoom, stubClient } from "../../../../../../test-utils";
|
|
|
|
import dis from "../../../../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../../../../src/dispatcher/actions";
|
|
|
|
import { MatrixClientPeg } from "../../../../../../../src/MatrixClientPeg";
|
|
|
|
import SettingsStore from "../../../../../../../src/settings/SettingsStore";
|
2023-01-27 11:00:06 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../../../src/dispatcher/dispatcher");
|
2023-01-27 11:00:06 +00:00
|
|
|
|
|
|
|
describe("AdvancedRoomSettingsTab", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let cli: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
const renderTab = (): RenderResult => {
|
2023-04-27 01:20:02 +00:00
|
|
|
return render(<AdvancedRoomSettingsTab room={room} closeSettingsFn={jest.fn()} />);
|
2023-01-27 11:00:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-05 17:12:23 +00:00
|
|
|
cli = MatrixClientPeg.safeGet();
|
2023-01-27 11:00:06 +00:00
|
|
|
room = mkStubRoom(roomId, "test room", cli);
|
|
|
|
mocked(cli.getRoom).mockReturnValue(room);
|
2023-01-27 14:07:05 +00:00
|
|
|
mocked(dis.dispatch).mockReset();
|
|
|
|
mocked(room.findPredecessor).mockImplementation((msc3946: boolean) =>
|
|
|
|
msc3946
|
2023-04-12 15:26:45 +00:00
|
|
|
? { roomId: "old_room_id_via_predecessor", viaServers: ["one.example.com", "two.example.com"] }
|
2023-01-27 14:07:05 +00:00
|
|
|
: { roomId: "old_room_id", eventId: "tombstone_event_id" },
|
|
|
|
);
|
2023-01-27 11:00:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should render as expected", () => {
|
|
|
|
const tab = renderTab();
|
|
|
|
expect(tab.asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should display room ID", () => {
|
|
|
|
const tab = renderTab();
|
|
|
|
tab.getByText(roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should display room version", () => {
|
|
|
|
mocked(room.getVersion).mockReturnValue("custom_room_version_1");
|
|
|
|
|
|
|
|
const tab = renderTab();
|
|
|
|
tab.getByText("custom_room_version_1");
|
|
|
|
});
|
|
|
|
|
2023-04-27 01:20:02 +00:00
|
|
|
it("displays message when room cannot federate", () => {
|
|
|
|
const createEvent = new MatrixEvent({
|
|
|
|
sender: "@a:b.com",
|
|
|
|
type: EventType.RoomCreate,
|
|
|
|
content: { "m.federate": false },
|
|
|
|
room_id: room.roomId,
|
|
|
|
state_key: "",
|
|
|
|
});
|
|
|
|
jest.spyOn(room.currentState, "getStateEvents").mockImplementation((type) =>
|
|
|
|
type === EventType.RoomCreate ? createEvent : null,
|
|
|
|
);
|
|
|
|
|
|
|
|
renderTab();
|
|
|
|
expect(screen.getByText("This room is not accessible by remote Matrix servers")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
2023-01-27 11:00:06 +00:00
|
|
|
function mockStateEvents(room: Room) {
|
|
|
|
const createEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
user: "@a:b.com",
|
|
|
|
type: EventType.RoomCreate,
|
|
|
|
content: { predecessor: { room_id: "old_room_id", event_id: "tombstone_event_id" } },
|
|
|
|
room: room.roomId,
|
|
|
|
});
|
|
|
|
|
2023-01-27 14:07:05 +00:00
|
|
|
// Because we're mocking Room.findPredecessor, it may not be necessary
|
|
|
|
// to provide the actual event here, but we do need the create event,
|
|
|
|
// and in future this may be needed, so included for symmetry.
|
|
|
|
const predecessorEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
user: "@a:b.com",
|
|
|
|
type: EventType.RoomPredecessor,
|
|
|
|
content: { predecessor_room_id: "old_room_id_via_predecessor" },
|
|
|
|
room: room.roomId,
|
|
|
|
});
|
|
|
|
|
2023-01-27 11:00:06 +00:00
|
|
|
type GetStateEvents2Args = (eventType: EventType | string, stateKey: string) => MatrixEvent | null;
|
|
|
|
|
|
|
|
const getStateEvents = jest.spyOn(
|
|
|
|
room.currentState,
|
|
|
|
"getStateEvents",
|
|
|
|
) as unknown as jest.MockedFunction<GetStateEvents2Args>;
|
|
|
|
|
|
|
|
getStateEvents.mockImplementation((eventType: string | null, _key: string) => {
|
|
|
|
switch (eventType) {
|
|
|
|
case EventType.RoomCreate:
|
|
|
|
return createEvent;
|
2023-01-27 14:07:05 +00:00
|
|
|
case EventType.RoomPredecessor:
|
|
|
|
return predecessorEvent;
|
2023-01-27 11:00:06 +00:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should link to predecessor room", async () => {
|
|
|
|
mockStateEvents(room);
|
|
|
|
const tab = renderTab();
|
|
|
|
const link = await tab.findByText("View older messages in test room.");
|
|
|
|
fireEvent.click(link);
|
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
event_id: "tombstone_event_id",
|
|
|
|
room_id: "old_room_id",
|
|
|
|
metricsTrigger: "WebPredecessorSettings",
|
|
|
|
metricsViaKeyboard: false,
|
|
|
|
});
|
|
|
|
});
|
2023-01-27 14:07:05 +00:00
|
|
|
|
|
|
|
describe("When MSC3946 support is enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue")
|
|
|
|
.mockReset()
|
|
|
|
.mockImplementation((settingName) => settingName === "feature_dynamic_room_predecessors");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should link to predecessor room via MSC3946 if enabled", async () => {
|
|
|
|
mockStateEvents(room);
|
|
|
|
const tab = renderTab();
|
|
|
|
const link = await tab.findByText("View older messages in test room.");
|
|
|
|
fireEvent.click(link);
|
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
2023-02-24 15:28:40 +00:00
|
|
|
event_id: undefined,
|
2023-01-27 14:07:05 +00:00
|
|
|
room_id: "old_room_id_via_predecessor",
|
2023-04-12 15:26:45 +00:00
|
|
|
via_servers: ["one.example.com", "two.example.com"],
|
2023-01-27 14:07:05 +00:00
|
|
|
metricsTrigger: "WebPredecessorSettings",
|
|
|
|
metricsViaKeyboard: false,
|
|
|
|
});
|
|
|
|
});
|
2023-04-27 01:20:02 +00:00
|
|
|
|
|
|
|
it("handles when room is a space", async () => {
|
|
|
|
mockStateEvents(room);
|
|
|
|
jest.spyOn(room, "isSpaceRoom").mockReturnValue(true);
|
|
|
|
|
|
|
|
mockStateEvents(room);
|
|
|
|
const tab = renderTab();
|
|
|
|
const link = await tab.findByText("View older version of test room.");
|
|
|
|
expect(link).toBeInTheDocument();
|
|
|
|
expect(screen.getByText("Space information")).toBeInTheDocument();
|
|
|
|
});
|
2023-01-27 14:07:05 +00:00
|
|
|
});
|
2023-01-27 11:00:06 +00:00
|
|
|
});
|