From 7788d50b02456d34d12d5f94aed4db7b18cf225a Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Fri, 27 Jan 2023 20:20:01 +0100 Subject: [PATCH] Add tests --- src/components/structures/SpaceHierarchy.tsx | 2 +- .../structures/SpaceHierarchy-test.tsx | 48 ++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/components/structures/SpaceHierarchy.tsx b/src/components/structures/SpaceHierarchy.tsx index f358e37889..7d6888a197 100644 --- a/src/components/structures/SpaceHierarchy.tsx +++ b/src/components/structures/SpaceHierarchy.tsx @@ -413,7 +413,7 @@ interface IHierarchyLevelProps { onToggleClick?(parentId: string, childId: string): void; } -const toLocalRoom = (cli: MatrixClient, room: IHierarchyRoom, hierarchy: RoomHierarchy): IHierarchyRoom => { +export const toLocalRoom = (cli: MatrixClient, room: IHierarchyRoom, hierarchy: RoomHierarchy): IHierarchyRoom => { const history = cli.getRoomUpgradeHistory(room.room_id, true); // Pick latest room that is actually part of the hierarchy diff --git a/test/components/structures/SpaceHierarchy-test.tsx b/test/components/structures/SpaceHierarchy-test.tsx index 9181802814..a2a63ae0b8 100644 --- a/test/components/structures/SpaceHierarchy-test.tsx +++ b/test/components/structures/SpaceHierarchy-test.tsx @@ -17,11 +17,12 @@ limitations under the License. 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"; +import { IHierarchyRoom } from "matrix-js-sdk/src/@types/spaces"; import { MatrixClientPeg } from "../../../src/MatrixClientPeg"; -import { stubClient } from "../../test-utils"; +import { mkStubRoom, stubClient } from "../../test-utils"; import dispatcher from "../../../src/dispatcher/dispatcher"; -import { showRoom } from "../../../src/components/structures/SpaceHierarchy"; +import { showRoom, toLocalRoom } from "../../../src/components/structures/SpaceHierarchy"; import { Action } from "../../../src/dispatcher/actions"; describe("SpaceHierarchy", () => { @@ -67,4 +68,47 @@ describe("SpaceHierarchy", () => { }); }); }); + + describe("toLocalRoom", () => { + let client: MatrixClient; + let roomV1: Room; + let roomV2: Room; + let roomV3: Room; + + beforeEach(() => { + stubClient(); + client = MatrixClientPeg.get(); + roomV1 = mkStubRoom("room-id-1", "Room V1", client); + roomV2 = mkStubRoom("room-id-2", "Room V2", client); + roomV3 = mkStubRoom("room-id-3", "Room V3", client); + jest.spyOn(client, "getRoomUpgradeHistory").mockReturnValue([roomV1, roomV2, roomV3]); + }); + + it("grabs last room that is in hierarchy when latest version is in hierarchy", () => { + 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; + 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", () => { + const hierarchy = { roomMap: new Map([ + [roomV1.roomId, { room_id: roomV1.roomId } as IHierarchyRoom], + [roomV2.roomId, { room_id: roomV2.roomId } as IHierarchyRoom] + ]) } as RoomHierarchy; + 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); + }); + }); });