Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit b981760

Browse files
committed
Add MemberAvatar test
1 parent 7e6150c commit b981760

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

src/components/views/avatars/BaseAvatar.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ interface IProps {
4848
tabIndex?: number;
4949
}
5050

51-
const calculateUrls = (url, urls, lowBandwidth) => {
51+
const calculateUrls = (url: string, urls: string[], lowBandwidth: boolean): string[] => {
5252
// work out the full set of urls to try to load. This is formed like so:
5353
// imageUrls: [ props.url, ...props.urls ]
5454

55-
let _urls = [];
55+
let _urls: string[] = [];
5656
if (!lowBandwidth) {
5757
_urls = urls || [];
5858

@@ -145,7 +145,8 @@ const BaseAvatar = (props: IProps) => {
145145
width: toPx(width),
146146
height: toPx(height),
147147
}}
148-
aria-hidden="true" />
148+
aria-hidden="true"
149+
data-testid="avatar-img" />
149150
);
150151

151152
if (onClick) {
@@ -193,6 +194,7 @@ const BaseAvatar = (props: IProps) => {
193194
title={title}
194195
alt={_t("Avatar")}
195196
inputRef={inputRef}
197+
data-testid="avatar-img"
196198
{...otherProps} />
197199
);
198200
} else {
@@ -208,6 +210,7 @@ const BaseAvatar = (props: IProps) => {
208210
title={title}
209211
alt=""
210212
ref={inputRef}
213+
data-testid="avatar-img"
211214
{...otherProps} />
212215
);
213216
}

src/components/views/avatars/MemberAvatar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default function MemberAvatar({
9191
setTitle(title);
9292
}, [member?.name, member?.roomId, member?.userId, props.fallbackUserId, props.title]);
9393

94-
return (
94+
return member ? (
9595
<BaseAvatar
9696
{...props}
9797
width={width}
@@ -109,5 +109,5 @@ export default function MemberAvatar({
109109
});
110110
} : props.onClick}
111111
/>
112-
);
112+
) : null;
113113
}

src/hooks/room/useRoomMemberProfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function useRoomMemberProfile({
3636

3737
useEffect(() => {
3838
const threadContexts = [TimelineRenderingType.ThreadsList, TimelineRenderingType.Thread];
39-
if ((propMember && !forceHistorical && useOnlyCurrentProfiles)
39+
if ((!forceHistorical && useOnlyCurrentProfiles)
4040
|| threadContexts.includes(context?.timelineRenderingType)) {
4141
setMember(context?.room?.getMember(userId));
4242
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { getByTestId, render, waitFor } from "@testing-library/react";
18+
import { mocked } from "jest-mock";
19+
import { MatrixClient, PendingEventOrdering } from "matrix-js-sdk/src/client";
20+
import { Room } from "matrix-js-sdk/src/models/room";
21+
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
22+
import React from "react";
23+
24+
import MemberAvatar from "../../../../src/components/views/avatars/MemberAvatar";
25+
import RoomContext from "../../../../src/contexts/RoomContext";
26+
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
27+
import SettingsStore from "../../../../src/settings/SettingsStore";
28+
import { getRoomContext } from "../../../test-utils/room";
29+
import { stubClient } from "../../../test-utils/test-utils";
30+
31+
describe("MemberAvatar", () => {
32+
const ROOM_ID = "roomId";
33+
34+
let mockClient: MatrixClient;
35+
let room: Room;
36+
let member: RoomMember;
37+
38+
function getComponent(props) {
39+
return <RoomContext.Provider value={getRoomContext(room, {})}>
40+
<MemberAvatar
41+
member={null}
42+
width={35}
43+
height={35}
44+
{...props}
45+
/>
46+
</RoomContext.Provider>;
47+
}
48+
49+
beforeEach(() => {
50+
jest.clearAllMocks();
51+
52+
stubClient();
53+
mockClient = mocked(MatrixClientPeg.get());
54+
55+
room = new Room(ROOM_ID, mockClient, mockClient.getUserId() ?? "", {
56+
pendingEventOrdering: PendingEventOrdering.Detached,
57+
});
58+
59+
member = new RoomMember(ROOM_ID, "@bob:example.org");
60+
jest.spyOn(room, "getMember").mockReturnValue(member);
61+
jest.spyOn(member, "getMxcAvatarUrl").mockReturnValue("http://placekitten.com/400/400");
62+
});
63+
64+
it.only("shows an avatar for useOnlyCurrentProfiles", async () => {
65+
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName: string) => {
66+
return settingName === "useOnlyCurrentProfiles";
67+
});
68+
69+
const { container } = render(getComponent({}));
70+
71+
let avatar: HTMLElement;
72+
await waitFor(() => {
73+
avatar = getByTestId(container, "avatar-img");
74+
expect(avatar).toBeInTheDocument();
75+
});
76+
77+
expect(avatar!.getAttribute("src")).not.toBe("");
78+
});
79+
});

0 commit comments

Comments
 (0)