Skip to content

Commit 062b700

Browse files
committed
fix: Show group avatar icon for group chats in list view
1 parent cdeff25 commit 062b700

2 files changed

Lines changed: 70 additions & 10 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { render, screen } from "@testing-library/react";
2+
import GroupChatListItem from "features/messages/groupchats/GroupChatListItem";
3+
import { GroupChat } from "proto/conversations_pb";
4+
import { service } from "service";
5+
import groupChat from "test/fixtures/groupChat.json";
6+
import wrapper from "test/hookWrapper";
7+
import { getLiteUsers } from "test/serviceMockDefaults";
8+
import { addDefaultUser } from "test/utils";
9+
10+
const getLiteUsersMock = service.user.getLiteUsers as jest.Mock;
11+
12+
beforeEach(() => {
13+
addDefaultUser(1);
14+
getLiteUsersMock.mockImplementation(getLiteUsers);
15+
});
16+
17+
afterEach(() => jest.restoreAllMocks());
18+
19+
describe("GroupChatListItem avatar", () => {
20+
it("shows a group icon for non-DM group chats", async () => {
21+
render(<GroupChatListItem groupChat={groupChat} />, { wrapper });
22+
23+
expect(await screen.findByTestId("GroupsIcon")).toBeVisible();
24+
expect(screen.queryByText("FD")).not.toBeInTheDocument();
25+
});
26+
27+
it("shows the other member's avatar for DMs", async () => {
28+
const dmGroupChat: GroupChat.AsObject = {
29+
...groupChat,
30+
isDm: true,
31+
latestMessage: {
32+
...groupChat.latestMessage!,
33+
authorUserId: 2,
34+
text: { text: "Reply" },
35+
},
36+
};
37+
38+
render(<GroupChatListItem groupChat={dmGroupChat} />, { wrapper });
39+
40+
expect(await screen.findByText("FD")).toBeVisible();
41+
expect(screen.queryByTestId("GroupsIcon")).not.toBeInTheDocument();
42+
});
43+
44+
it("shows the other member's avatar for DMs even when current user sent the latest message", async () => {
45+
const dmGroupChat: GroupChat.AsObject = {
46+
...groupChat,
47+
isDm: true,
48+
latestMessage: {
49+
...groupChat.latestMessage!,
50+
authorUserId: 1,
51+
text: { text: "Hello there" },
52+
},
53+
};
54+
55+
render(<GroupChatListItem groupChat={dmGroupChat} />, { wrapper });
56+
57+
expect(await screen.findByText("FD")).toBeVisible();
58+
expect(screen.queryByTestId("GroupsIcon")).not.toBeInTheDocument();
59+
});
60+
});

app/web/features/messages/groupchats/GroupChatListItem.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArchiveOutlined, UnarchiveOutlined } from "@mui/icons-material";
1+
import { ArchiveOutlined, Groups, UnarchiveOutlined } from "@mui/icons-material";
22
import {
33
ListItemAvatar,
44
ListItemButton,
@@ -80,13 +80,9 @@ export default function GroupChatListItem({
8080
latestMessageAuthorId,
8181
]);
8282

83-
//the avatar is of the latest message author (if it's not the logged in user),
84-
//otherwise any user that's not the logged in user, otherwise logged in user
85-
const avatarUserId =
86-
latestMessageAuthorId !== null && latestMessageAuthorId !== currentUserId
87-
? latestMessageAuthorId
88-
: (groupChat.memberUserIdsList.find((id) => id !== currentUserId) ??
89-
currentUserId);
83+
const dmOtherMemberUserId = groupChat.memberUserIdsList.find(
84+
(id) => id !== currentUserId,
85+
);
9086
//title is the chat title, or all the member's names except current user joined together
9187
const title = groupChatTitleText(
9288
groupChat,
@@ -168,11 +164,15 @@ export default function GroupChatListItem({
168164
<ListItemAvatar>
169165
{groupChatMembersQuery.isLoading ? (
170166
<Skeleton />
171-
) : (
167+
) : groupChat.isDm ? (
172168
<Avatar
173-
user={groupChatMembersQuery.data?.get(avatarUserId)}
169+
user={groupChatMembersQuery.data?.get(dmOtherMemberUserId)}
174170
isProfileLink={false}
175171
/>
172+
) : (
173+
<Avatar isProfileLink={false}>
174+
<Groups />
175+
</Avatar>
176176
)}
177177
</ListItemAvatar>
178178
<ListItemText

0 commit comments

Comments
 (0)