Skip to content

Commit 86d32c6

Browse files
committed
test: add tests for room avatar view
1 parent 3931e45 commit 86d32c6

File tree

2 files changed

+485
-0
lines changed

2 files changed

+485
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
import React from "react";
9+
import { render, screen } from "jest-matrix-react";
10+
import { mocked } from "jest-mock";
11+
12+
import { RoomAvatarView } from "../../../../../src/components/views/avatars/RoomAvatarView";
13+
import { mkStubRoom, stubClient } from "../../../../test-utils";
14+
import {
15+
type Presence,
16+
type RoomAvatarViewState,
17+
useRoomAvatarViewModel,
18+
} from "../../../../../src/components/viewmodels/avatars/RoomAvatarViewModel";
19+
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
20+
21+
jest.mock("../../../../../src/components/viewmodels/avatars/RoomAvatarViewModel", () => ({
22+
useRoomAvatarViewModel: jest.fn(),
23+
}));
24+
25+
describe("<RoomAvatarView />", () => {
26+
const matrixClient = stubClient();
27+
const room = mkStubRoom("roomId", "roomName", matrixClient);
28+
29+
DMRoomMap.makeShared(matrixClient);
30+
jest.spyOn(DMRoomMap.shared(), "getUserIdForRoomId").mockReturnValue(null);
31+
32+
let defaultValue: RoomAvatarViewState;
33+
34+
beforeEach(() => {
35+
defaultValue = {
36+
hasDecoration: true,
37+
isPublic: true,
38+
isVideoRoom: true,
39+
presence: null,
40+
};
41+
42+
mocked(useRoomAvatarViewModel).mockReturnValue(defaultValue);
43+
});
44+
45+
it("should not render a decoration", () => {
46+
mocked(useRoomAvatarViewModel).mockReturnValue({ ...defaultValue, hasDecoration: false });
47+
const { asFragment } = render(<RoomAvatarView room={room} />);
48+
expect(asFragment()).toMatchSnapshot();
49+
});
50+
51+
it("should render a video room decoration", () => {
52+
mocked(useRoomAvatarViewModel).mockReturnValue({ ...defaultValue, hasDecoration: true, isVideoRoom: true });
53+
const { asFragment } = render(<RoomAvatarView room={room} />);
54+
55+
expect(screen.getByLabelText("This room is a video room")).toBeInTheDocument();
56+
expect(asFragment()).toMatchSnapshot();
57+
});
58+
59+
it("should render a public room decoration", () => {
60+
mocked(useRoomAvatarViewModel).mockReturnValue({
61+
...defaultValue,
62+
hasDecoration: true,
63+
isPublic: true,
64+
isVideoRoom: false,
65+
});
66+
const { asFragment } = render(<RoomAvatarView room={room} />);
67+
68+
expect(screen.getByLabelText("This room is public")).toBeInTheDocument();
69+
expect(asFragment()).toMatchSnapshot();
70+
});
71+
72+
it("should not render a public room decoration if the room is a video room", () => {
73+
mocked(useRoomAvatarViewModel).mockReturnValue({
74+
...defaultValue,
75+
hasDecoration: true,
76+
isPublic: true,
77+
isVideoRoom: true,
78+
});
79+
render(<RoomAvatarView room={room} />);
80+
81+
expect(screen.getByLabelText("This room is a video room")).toBeInTheDocument();
82+
expect(screen.queryByLabelText("This room is public")).toBeNull();
83+
});
84+
85+
it.each([
86+
{ presence: "online" as Presence, label: "Online" },
87+
{ presence: "offline" as Presence, label: "Offline" },
88+
{ presence: "busy" as Presence, label: "Busy" },
89+
{ presence: "unavailable" as Presence, label: "Away" },
90+
])("should render the $presence presence", ({ presence, label }) => {
91+
mocked(useRoomAvatarViewModel).mockReturnValue({
92+
...defaultValue,
93+
hasDecoration: true,
94+
presence,
95+
});
96+
const { asFragment } = render(<RoomAvatarView room={room} />);
97+
98+
expect(screen.getByLabelText(label)).toBeInTheDocument();
99+
expect(asFragment()).toMatchSnapshot();
100+
});
101+
});

0 commit comments

Comments
 (0)