diff --git a/test/components/structures/SpaceHierarchy-test.tsx b/test/components/structures/SpaceHierarchy-test.tsx index 27e55e95c1..3d29b77646 100644 --- a/test/components/structures/SpaceHierarchy-test.tsx +++ b/test/components/structures/SpaceHierarchy-test.tsx @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +import React from "react"; +import { render } from "@testing-library/react"; 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"; @@ -22,8 +24,17 @@ import { IHierarchyRoom } from "matrix-js-sdk/src/@types/spaces"; import { MatrixClientPeg } from "../../../src/MatrixClientPeg"; import { mkStubRoom, stubClient } from "../../test-utils"; import dispatcher from "../../../src/dispatcher/dispatcher"; -import { showRoom, toLocalRoom } from "../../../src/components/structures/SpaceHierarchy"; +import { HierarchyLevel, showRoom, toLocalRoom } from "../../../src/components/structures/SpaceHierarchy"; import { Action } from "../../../src/dispatcher/actions"; +import MatrixClientContext from "../../../src/contexts/MatrixClientContext"; +import DMRoomMap from "../../../src/utils/DMRoomMap"; + +// Fake random strings to give a predictable snapshot for checkbox IDs +jest.mock("matrix-js-sdk/src/randomstring", () => { + return { + randomString: () => "abdefghi", + }; +}); describe("SpaceHierarchy", () => { describe("showRoom", () => { @@ -125,4 +136,77 @@ describe("SpaceHierarchy", () => { expect(localRoomV3.room_id).toEqual(roomV3.roomId); }); }); + + describe("", () => { + let client: MatrixClient; + let dmRoomMap: DMRoomMap; + + let root: Room; + let room1: Room; + let room2: Room; + + let hierarchyRoot: IHierarchyRoom + let hierarchyRoom1: IHierarchyRoom + let hierarchyRoom2: IHierarchyRoom + + let roomHierarchy: RoomHierarchy; + + beforeEach(() => { + stubClient(); + client = MatrixClientPeg.get(); + + dmRoomMap = { + getUserIdForRoomId: jest.fn() + } as unknown as DMRoomMap; + jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap); + + root = mkStubRoom("room-id-1", "Room 1", client); + room1 = mkStubRoom("room-id-2", "Room 2", client); + room2 = mkStubRoom("room-id-3", "Room 3", client); + + hierarchyRoot = { + 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 + hierarchyRoom1 = { room_id: room1.roomId, num_joined_members: 2 } as IHierarchyRoom + hierarchyRoom2 = { room_id: root.roomId, num_joined_members: 3 } as IHierarchyRoom + + roomHierarchy = { + roomMap: new Map([ + [root.roomId, hierarchyRoot], + [room1.roomId, hierarchyRoom1], + [room2.roomId, hierarchyRoom2], + ]), + isSuggested: jest.fn() + } as unknown as RoomHierarchy; + }); + + it("renders", () => { + const defaultProps = { + root: hierarchyRoot, + roomSet: new Set([hierarchyRoom1, hierarchyRoom2]), + hierarchy: roomHierarchy, + parents: new Set(), + selectedMap: new Map>(), + onViewRoomClick: jest.fn(), + onJoinRoomClick: jest.fn(), + onToggleClick: jest.fn(), + }; + const getComponent = (props = {}): React.ReactElement => ( + + ; + + ); + + const { container } = render(getComponent()); + expect(container).toMatchSnapshot(); + }); + }); }); diff --git a/test/components/structures/__snapshots__/SpaceHierarchy-test.tsx.snap b/test/components/structures/__snapshots__/SpaceHierarchy-test.tsx.snap new file mode 100644 index 0000000000..25340face1 --- /dev/null +++ b/test/components/structures/__snapshots__/SpaceHierarchy-test.tsx.snap @@ -0,0 +1,159 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`SpaceHierarchy renders 1`] = ` +
+
  • +
    +
    +
    + +
    +
    + Unnamed Room +
    + Joined +
    +
    +
    + 2 members + · + +
    +
    +
    +
    + View +
    + + +
    +
  • +
  • +
    +
    +
    + +
    +
    + Unnamed Room +
    + Joined +
    +
    +
    + 3 members + · + +
    +
    +
    +
    + View +
    + + +
    +
  • + ; +
    +`;