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

Commit

Permalink
Add MemberAvatar test
Browse files Browse the repository at this point in the history
  • Loading branch information
germain-gg committed Nov 10, 2022
1 parent 7e6150c commit b981760
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/components/views/avatars/BaseAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ interface IProps {
tabIndex?: number;
}

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

let _urls = [];
let _urls: string[] = [];
if (!lowBandwidth) {
_urls = urls || [];

Expand Down Expand Up @@ -145,7 +145,8 @@ const BaseAvatar = (props: IProps) => {
width: toPx(width),
height: toPx(height),
}}
aria-hidden="true" />
aria-hidden="true"
data-testid="avatar-img" />
);

if (onClick) {
Expand Down Expand Up @@ -193,6 +194,7 @@ const BaseAvatar = (props: IProps) => {
title={title}
alt={_t("Avatar")}
inputRef={inputRef}
data-testid="avatar-img"
{...otherProps} />
);
} else {
Expand All @@ -208,6 +210,7 @@ const BaseAvatar = (props: IProps) => {
title={title}
alt=""
ref={inputRef}
data-testid="avatar-img"
{...otherProps} />
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/avatars/MemberAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function MemberAvatar({
setTitle(title);
}, [member?.name, member?.roomId, member?.userId, props.fallbackUserId, props.title]);

return (
return member ? (
<BaseAvatar
{...props}
width={width}
Expand All @@ -109,5 +109,5 @@ export default function MemberAvatar({
});
} : props.onClick}
/>
);
) : null;

This comment has been minimized.

Copy link
@t3chguy

t3chguy Nov 10, 2022

Member

Seems like fallbackUserId is no longer used in this case? Seems like this majorly changes the behaviour in the case of null member

}
2 changes: 1 addition & 1 deletion src/hooks/room/useRoomMemberProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function useRoomMemberProfile({

useEffect(() => {
const threadContexts = [TimelineRenderingType.ThreadsList, TimelineRenderingType.Thread];
if ((propMember && !forceHistorical && useOnlyCurrentProfiles)
if ((!forceHistorical && useOnlyCurrentProfiles)
|| threadContexts.includes(context?.timelineRenderingType)) {
setMember(context?.room?.getMember(userId));
}
Expand Down
79 changes: 79 additions & 0 deletions test/components/views/avatars/MemberAvatar-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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.
*/

import { getByTestId, render, waitFor } from "@testing-library/react";
import { mocked } from "jest-mock";
import { MatrixClient, PendingEventOrdering } from "matrix-js-sdk/src/client";
import { Room } from "matrix-js-sdk/src/models/room";
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import React from "react";

import MemberAvatar from "../../../../src/components/views/avatars/MemberAvatar";
import RoomContext from "../../../../src/contexts/RoomContext";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { getRoomContext } from "../../../test-utils/room";
import { stubClient } from "../../../test-utils/test-utils";

describe("MemberAvatar", () => {
const ROOM_ID = "roomId";

let mockClient: MatrixClient;
let room: Room;
let member: RoomMember;

function getComponent(props) {
return <RoomContext.Provider value={getRoomContext(room, {})}>
<MemberAvatar
member={null}
width={35}
height={35}
{...props}
/>
</RoomContext.Provider>;
}

beforeEach(() => {
jest.clearAllMocks();

stubClient();
mockClient = mocked(MatrixClientPeg.get());

room = new Room(ROOM_ID, mockClient, mockClient.getUserId() ?? "", {
pendingEventOrdering: PendingEventOrdering.Detached,
});

member = new RoomMember(ROOM_ID, "@bob:example.org");
jest.spyOn(room, "getMember").mockReturnValue(member);
jest.spyOn(member, "getMxcAvatarUrl").mockReturnValue("http://placekitten.com/400/400");
});

it.only("shows an avatar for useOnlyCurrentProfiles", async () => {
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName: string) => {
return settingName === "useOnlyCurrentProfiles";
});

const { container } = render(getComponent({}));

let avatar: HTMLElement;
await waitFor(() => {
avatar = getByTestId(container, "avatar-img");
expect(avatar).toBeInTheDocument();
});

expect(avatar!.getAttribute("src")).not.toBe("");
});
});

0 comments on commit b981760

Please sign in to comment.