2022-11-22 15:05:09 +00: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.
|
|
|
|
*/
|
|
|
|
|
2023-02-01 18:51:37 +00:00
|
|
|
import React from "react";
|
2023-03-10 09:41:14 +00:00
|
|
|
import { mocked } from "jest-mock";
|
2023-02-01 18:51:37 +00:00
|
|
|
import { render } from "@testing-library/react";
|
2022-11-22 15:05:09 +00:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { RoomHierarchy } from "matrix-js-sdk/src/room-hierarchy";
|
2023-01-27 19:20:01 +00:00
|
|
|
import { IHierarchyRoom } from "matrix-js-sdk/src/@types/spaces";
|
2022-11-22 15:05:09 +00:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
2023-01-27 19:20:01 +00:00
|
|
|
import { mkStubRoom, stubClient } from "../../test-utils";
|
2022-11-22 15:05:09 +00:00
|
|
|
import dispatcher from "../../../src/dispatcher/dispatcher";
|
2023-02-01 18:51:37 +00:00
|
|
|
import { HierarchyLevel, showRoom, toLocalRoom } from "../../../src/components/structures/SpaceHierarchy";
|
2022-11-22 15:05:09 +00:00
|
|
|
import { Action } from "../../../src/dispatcher/actions";
|
2023-02-01 18:51:37 +00:00
|
|
|
import MatrixClientContext from "../../../src/contexts/MatrixClientContext";
|
|
|
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
2023-03-10 09:41:14 +00:00
|
|
|
import SettingsStore from "../../../src/settings/SettingsStore";
|
2023-02-01 18:51:37 +00:00
|
|
|
|
|
|
|
// Fake random strings to give a predictable snapshot for checkbox IDs
|
|
|
|
jest.mock("matrix-js-sdk/src/randomstring", () => {
|
|
|
|
return {
|
|
|
|
randomString: () => "abdefghi",
|
|
|
|
};
|
|
|
|
});
|
2022-11-22 15:05:09 +00:00
|
|
|
|
|
|
|
describe("SpaceHierarchy", () => {
|
|
|
|
describe("showRoom", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let hierarchy: RoomHierarchy;
|
|
|
|
let room: Room;
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
client = MatrixClientPeg.get();
|
|
|
|
room = new Room("room-id", client, "@alice:example.com");
|
|
|
|
hierarchy = new RoomHierarchy(room);
|
|
|
|
|
|
|
|
jest.spyOn(client, "isGuest").mockReturnValue(false);
|
|
|
|
|
|
|
|
jest.spyOn(hierarchy.roomMap, "get").mockReturnValue({
|
|
|
|
children_state: [],
|
|
|
|
room_id: "room-id2",
|
|
|
|
canonical_alias: "canonical-alias",
|
|
|
|
aliases: ["uncanonical-alias", "canonical-alias"],
|
|
|
|
world_readable: true,
|
|
|
|
guest_can_join: false,
|
|
|
|
num_joined_members: 35,
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(dispatcher, "dispatch");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows room", () => {
|
|
|
|
showRoom(client, hierarchy, "room-id2");
|
|
|
|
expect(dispatcher.dispatch).toHaveBeenCalledWith({
|
2022-12-12 11:24:14 +00:00
|
|
|
action: Action.ViewRoom,
|
|
|
|
should_peek: true,
|
|
|
|
room_alias: "canonical-alias",
|
|
|
|
room_id: "room-id2",
|
|
|
|
via_servers: [],
|
|
|
|
oob_data: {
|
2022-11-22 15:05:09 +00:00
|
|
|
avatarUrl: undefined,
|
|
|
|
name: "canonical-alias",
|
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
roomType: undefined,
|
|
|
|
metricsTrigger: "RoomDirectory",
|
2022-11-22 15:05:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-01-27 19:20:01 +00:00
|
|
|
|
|
|
|
describe("toLocalRoom", () => {
|
2023-02-02 19:20:57 +00:00
|
|
|
stubClient();
|
2023-02-02 19:38:42 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const roomV1 = mkStubRoom("room-id-1", "Room V1", client);
|
|
|
|
const roomV2 = mkStubRoom("room-id-2", "Room V2", client);
|
|
|
|
const roomV3 = mkStubRoom("room-id-3", "Room V3", client);
|
2023-02-02 19:20:57 +00:00
|
|
|
jest.spyOn(client, "getRoomUpgradeHistory").mockReturnValue([roomV1, roomV2, roomV3]);
|
2023-01-27 19:20:01 +00:00
|
|
|
|
|
|
|
it("grabs last room that is in hierarchy when latest version is in hierarchy", () => {
|
2023-01-27 19:29:50 +00:00
|
|
|
const hierarchy = {
|
|
|
|
roomMap: new Map([
|
|
|
|
[roomV1.roomId, { room_id: roomV1.roomId } as IHierarchyRoom],
|
|
|
|
[roomV2.roomId, { room_id: roomV2.roomId } as IHierarchyRoom],
|
|
|
|
[roomV3.roomId, { room_id: roomV3.roomId } as IHierarchyRoom],
|
|
|
|
]),
|
|
|
|
} as RoomHierarchy;
|
2023-01-27 19:20:01 +00:00
|
|
|
const localRoomV1 = toLocalRoom(client, { room_id: roomV1.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV1.room_id).toEqual(roomV3.roomId);
|
|
|
|
const localRoomV2 = toLocalRoom(client, { room_id: roomV2.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV2.room_id).toEqual(roomV3.roomId);
|
|
|
|
const localRoomV3 = toLocalRoom(client, { room_id: roomV3.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV3.room_id).toEqual(roomV3.roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("grabs last room that is in hierarchy when latest version is *not* in hierarchy", () => {
|
2023-01-27 19:29:50 +00:00
|
|
|
const hierarchy = {
|
|
|
|
roomMap: new Map([
|
|
|
|
[roomV1.roomId, { room_id: roomV1.roomId } as IHierarchyRoom],
|
|
|
|
[roomV2.roomId, { room_id: roomV2.roomId } as IHierarchyRoom],
|
|
|
|
]),
|
|
|
|
} as RoomHierarchy;
|
2023-01-27 19:20:01 +00:00
|
|
|
const localRoomV1 = toLocalRoom(client, { room_id: roomV1.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV1.room_id).toEqual(roomV2.roomId);
|
|
|
|
const localRoomV2 = toLocalRoom(client, { room_id: roomV2.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV2.room_id).toEqual(roomV2.roomId);
|
|
|
|
const localRoomV3 = toLocalRoom(client, { room_id: roomV3.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV3.room_id).toEqual(roomV2.roomId);
|
|
|
|
});
|
2023-01-30 08:53:41 +00:00
|
|
|
|
|
|
|
it("returns specified room when none of the versions is in hierarchy", () => {
|
|
|
|
const hierarchy = { roomMap: new Map([]) } as RoomHierarchy;
|
|
|
|
const localRoomV1 = toLocalRoom(client, { room_id: roomV1.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV1.room_id).toEqual(roomV1.roomId);
|
|
|
|
const localRoomV2 = toLocalRoom(client, { room_id: roomV2.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV2.room_id).toEqual(roomV2.roomId);
|
|
|
|
const localRoomV3 = toLocalRoom(client, { room_id: roomV3.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(localRoomV3.room_id).toEqual(roomV3.roomId);
|
|
|
|
});
|
2023-03-10 09:41:14 +00:00
|
|
|
|
|
|
|
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
|
|
|
});
|
|
|
|
it("Passes through the dynamic predecessor setting", async () => {
|
|
|
|
mocked(client.getRoomUpgradeHistory).mockClear();
|
|
|
|
const hierarchy = { roomMap: new Map([]) } as RoomHierarchy;
|
|
|
|
toLocalRoom(client, { room_id: roomV1.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(client.getRoomUpgradeHistory).toHaveBeenCalledWith(roomV1.roomId, true, false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("If the feature_dynamic_room_predecessors is enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// Turn on feature_dynamic_room_predecessors setting
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(settingName) => settingName === "feature_dynamic_room_predecessors",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Passes through the dynamic predecessor setting", async () => {
|
|
|
|
mocked(client.getRoomUpgradeHistory).mockClear();
|
|
|
|
const hierarchy = { roomMap: new Map([]) } as RoomHierarchy;
|
|
|
|
toLocalRoom(client, { room_id: roomV1.roomId } as IHierarchyRoom, hierarchy);
|
|
|
|
expect(client.getRoomUpgradeHistory).toHaveBeenCalledWith(roomV1.roomId, true, true);
|
|
|
|
});
|
|
|
|
});
|
2023-01-27 19:20:01 +00:00
|
|
|
});
|
2023-02-01 18:51:37 +00:00
|
|
|
|
|
|
|
describe("<HierarchyLevel />", () => {
|
2023-02-02 19:20:57 +00:00
|
|
|
stubClient();
|
2023-02-02 19:38:42 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
2023-02-02 19:20:57 +00:00
|
|
|
|
2023-02-02 19:38:42 +00:00
|
|
|
const dmRoomMap = {
|
2023-02-02 19:20:57 +00:00
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
|
|
|
|
|
2023-02-02 19:38:42 +00:00
|
|
|
const root = mkStubRoom("room-id-1", "Room 1", client);
|
|
|
|
const room1 = mkStubRoom("room-id-2", "Room 2", client);
|
|
|
|
const room2 = mkStubRoom("room-id-3", "Room 3", client);
|
2023-02-02 19:20:57 +00:00
|
|
|
|
2023-02-02 19:38:42 +00:00
|
|
|
const hierarchyRoot = {
|
2023-02-02 19:20:57 +00:00
|
|
|
room_id: root.roomId,
|
|
|
|
num_joined_members: 1,
|
|
|
|
children_state: [
|
|
|
|
{
|
|
|
|
state_key: room1.roomId,
|
|
|
|
content: { order: "1" },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
state_key: room2.roomId,
|
|
|
|
content: { order: "2" },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
} as IHierarchyRoom;
|
2023-02-02 19:38:42 +00:00
|
|
|
const hierarchyRoom1 = { room_id: room1.roomId, num_joined_members: 2 } as IHierarchyRoom;
|
|
|
|
const hierarchyRoom2 = { room_id: root.roomId, num_joined_members: 3 } as IHierarchyRoom;
|
2023-02-02 19:20:57 +00:00
|
|
|
|
2023-02-02 19:38:42 +00:00
|
|
|
const roomHierarchy = {
|
2023-02-02 19:20:57 +00:00
|
|
|
roomMap: new Map([
|
|
|
|
[root.roomId, hierarchyRoot],
|
|
|
|
[room1.roomId, hierarchyRoom1],
|
|
|
|
[room2.roomId, hierarchyRoom2],
|
|
|
|
]),
|
|
|
|
isSuggested: jest.fn(),
|
|
|
|
} as unknown as RoomHierarchy;
|
2023-02-01 18:51:37 +00:00
|
|
|
|
|
|
|
it("renders", () => {
|
|
|
|
const defaultProps = {
|
|
|
|
root: hierarchyRoot,
|
|
|
|
roomSet: new Set([hierarchyRoom1, hierarchyRoom2]),
|
|
|
|
hierarchy: roomHierarchy,
|
|
|
|
parents: new Set<string>(),
|
|
|
|
selectedMap: new Map<string, Set<string>>(),
|
|
|
|
onViewRoomClick: jest.fn(),
|
|
|
|
onJoinRoomClick: jest.fn(),
|
|
|
|
onToggleClick: jest.fn(),
|
|
|
|
};
|
|
|
|
const getComponent = (props = {}): React.ReactElement => (
|
|
|
|
<MatrixClientContext.Provider value={client}>
|
|
|
|
<HierarchyLevel {...defaultProps} {...props} />;
|
|
|
|
</MatrixClientContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2022-11-22 15:05:09 +00:00
|
|
|
});
|