2022-07-20 15:26:25 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-02-02 18:22:19 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2022-07-20 15:26:25 +08:00
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-07-20 15:26:25 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { mocked } from "jest-mock";
|
2023-02-02 18:22:19 +08:00
|
|
|
import { Room, RoomMember, RoomType } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import { avatarUrlForRoom } from "../../src/Avatar";
|
|
|
|
import { Media, mediaFromMxc } from "../../src/customisations/Media";
|
|
|
|
import DMRoomMap from "../../src/utils/DMRoomMap";
|
2023-02-02 18:22:19 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../src/customisations/Media", () => ({
|
2023-02-02 18:22:19 +08:00
|
|
|
mediaFromMxc: jest.fn(),
|
|
|
|
}));
|
2022-07-20 15:26:25 +08:00
|
|
|
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
const avatarUrl1 = "https://example.com/avatar1";
|
|
|
|
const avatarUrl2 = "https://example.com/avatar2";
|
|
|
|
|
|
|
|
describe("avatarUrlForRoom", () => {
|
2023-02-02 18:22:19 +08:00
|
|
|
let getThumbnailOfSourceHttp: jest.Mock;
|
2022-07-20 15:26:25 +08:00
|
|
|
let room: Room;
|
|
|
|
let roomMember: RoomMember;
|
|
|
|
let dmRoomMap: DMRoomMap;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-02-02 18:22:19 +08:00
|
|
|
getThumbnailOfSourceHttp = jest.fn();
|
|
|
|
mocked(mediaFromMxc).mockImplementation((): Media => {
|
|
|
|
return {
|
|
|
|
getThumbnailOfSourceHttp,
|
|
|
|
} as unknown as Media;
|
|
|
|
});
|
2022-07-20 15:26:25 +08:00
|
|
|
room = {
|
|
|
|
roomId,
|
|
|
|
getMxcAvatarUrl: jest.fn(),
|
|
|
|
isSpaceRoom: jest.fn(),
|
2022-10-24 16:58:36 +08:00
|
|
|
getType: jest.fn(),
|
2022-07-20 15:26:25 +08:00
|
|
|
getAvatarFallbackMember: jest.fn(),
|
|
|
|
} as unknown as Room;
|
|
|
|
dmRoomMap = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap;
|
|
|
|
DMRoomMap.setShared(dmRoomMap);
|
|
|
|
roomMember = {
|
|
|
|
getMxcAvatarUrl: jest.fn(),
|
|
|
|
} as unknown as RoomMember;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null for a null room", () => {
|
2023-02-02 18:22:19 +08:00
|
|
|
expect(avatarUrlForRoom(null, 128, 128)).toBeNull();
|
2022-07-20 15:26:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the HTTP source if the room provides a MXC url", () => {
|
|
|
|
mocked(room.getMxcAvatarUrl).mockReturnValue(avatarUrl1);
|
2023-02-02 18:22:19 +08:00
|
|
|
getThumbnailOfSourceHttp.mockReturnValue(avatarUrl2);
|
|
|
|
expect(avatarUrlForRoom(room, 128, 256, "crop")).toEqual(avatarUrl2);
|
|
|
|
expect(getThumbnailOfSourceHttp).toHaveBeenCalledWith(128, 256, "crop");
|
2022-07-20 15:26:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null for a space room", () => {
|
|
|
|
mocked(room.isSpaceRoom).mockReturnValue(true);
|
2022-10-24 16:58:36 +08:00
|
|
|
mocked(room.getType).mockReturnValue(RoomType.Space);
|
2022-07-20 15:26:25 +08:00
|
|
|
expect(avatarUrlForRoom(room, 128, 128)).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null if the room is not a DM", () => {
|
|
|
|
mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue(null);
|
|
|
|
expect(avatarUrlForRoom(room, 128, 128)).toBeNull();
|
|
|
|
expect(dmRoomMap.getUserIdForRoomId).toHaveBeenCalledWith(roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null if there is no other member in the room", () => {
|
|
|
|
mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue("@user:example.com");
|
2023-02-14 01:01:43 +08:00
|
|
|
mocked(room.getAvatarFallbackMember).mockReturnValue(undefined);
|
2022-07-20 15:26:25 +08:00
|
|
|
expect(avatarUrlForRoom(room, 128, 128)).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return null if the other member has no avatar URL", () => {
|
|
|
|
mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue("@user:example.com");
|
|
|
|
mocked(room.getAvatarFallbackMember).mockReturnValue(roomMember);
|
|
|
|
expect(avatarUrlForRoom(room, 128, 128)).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the other member's avatar URL", () => {
|
|
|
|
mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue("@user:example.com");
|
|
|
|
mocked(room.getAvatarFallbackMember).mockReturnValue(roomMember);
|
|
|
|
mocked(roomMember.getMxcAvatarUrl).mockReturnValue(avatarUrl2);
|
2023-02-02 18:22:19 +08:00
|
|
|
getThumbnailOfSourceHttp.mockReturnValue(avatarUrl2);
|
|
|
|
expect(avatarUrlForRoom(room, 128, 256, "crop")).toEqual(avatarUrl2);
|
|
|
|
expect(getThumbnailOfSourceHttp).toHaveBeenCalledWith(128, 256, "crop");
|
2022-07-20 15:26:25 +08:00
|
|
|
});
|
|
|
|
});
|