|
| 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 { mocked } from "jest-mock"; |
| 18 | +import { Room, RoomMember } from "matrix-js-sdk/src/matrix"; |
| 19 | + |
| 20 | +import { avatarUrlForRoom } from "../src/Avatar"; |
| 21 | +import { Media, mediaFromMxc } from "../src/customisations/Media"; |
| 22 | +import DMRoomMap from "../src/utils/DMRoomMap"; |
| 23 | + |
| 24 | +jest.mock("../src/customisations/Media", () => ({ |
| 25 | + mediaFromMxc: jest.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +const roomId = "!room:example.com"; |
| 29 | +const avatarUrl1 = "https://example.com/avatar1"; |
| 30 | +const avatarUrl2 = "https://example.com/avatar2"; |
| 31 | + |
| 32 | +describe("avatarUrlForRoom", () => { |
| 33 | + let getThumbnailOfSourceHttp: jest.Mock; |
| 34 | + let room: Room; |
| 35 | + let roomMember: RoomMember; |
| 36 | + let dmRoomMap: DMRoomMap; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + getThumbnailOfSourceHttp = jest.fn(); |
| 40 | + mocked(mediaFromMxc).mockImplementation((): Media => { |
| 41 | + return { |
| 42 | + getThumbnailOfSourceHttp, |
| 43 | + } as unknown as Media; |
| 44 | + }); |
| 45 | + room = { |
| 46 | + roomId, |
| 47 | + getMxcAvatarUrl: jest.fn(), |
| 48 | + isSpaceRoom: jest.fn(), |
| 49 | + getAvatarFallbackMember: jest.fn(), |
| 50 | + } as unknown as Room; |
| 51 | + dmRoomMap = { |
| 52 | + getUserIdForRoomId: jest.fn(), |
| 53 | + } as unknown as DMRoomMap; |
| 54 | + DMRoomMap.setShared(dmRoomMap); |
| 55 | + roomMember = { |
| 56 | + getMxcAvatarUrl: jest.fn(), |
| 57 | + } as unknown as RoomMember; |
| 58 | + }); |
| 59 | + |
| 60 | + it("should return null for a null room", () => { |
| 61 | + expect(avatarUrlForRoom(null, 128, 128)).toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should return the HTTP source if the room provides a MXC url", () => { |
| 65 | + mocked(room.getMxcAvatarUrl).mockReturnValue(avatarUrl1); |
| 66 | + getThumbnailOfSourceHttp.mockReturnValue(avatarUrl2); |
| 67 | + expect(avatarUrlForRoom(room, 128, 256, "crop")).toEqual(avatarUrl2); |
| 68 | + expect(getThumbnailOfSourceHttp).toHaveBeenCalledWith(128, 256, "crop"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should return null for a space room", () => { |
| 72 | + mocked(room.isSpaceRoom).mockReturnValue(true); |
| 73 | + expect(avatarUrlForRoom(room, 128, 128)).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should return null if the room is not a DM", () => { |
| 77 | + mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue(null); |
| 78 | + expect(avatarUrlForRoom(room, 128, 128)).toBeNull(); |
| 79 | + expect(dmRoomMap.getUserIdForRoomId).toHaveBeenCalledWith(roomId); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should return null if there is no other member in the room", () => { |
| 83 | + mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue("@user:example.com"); |
| 84 | + mocked(room.getAvatarFallbackMember).mockReturnValue(null); |
| 85 | + expect(avatarUrlForRoom(room, 128, 128)).toBeNull(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should return null if the other member has no avatar URL", () => { |
| 89 | + mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue("@user:example.com"); |
| 90 | + mocked(room.getAvatarFallbackMember).mockReturnValue(roomMember); |
| 91 | + expect(avatarUrlForRoom(room, 128, 128)).toBeNull(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should return the other member's avatar URL", () => { |
| 95 | + mocked(dmRoomMap).getUserIdForRoomId.mockReturnValue("@user:example.com"); |
| 96 | + mocked(room.getAvatarFallbackMember).mockReturnValue(roomMember); |
| 97 | + mocked(roomMember.getMxcAvatarUrl).mockReturnValue(avatarUrl2); |
| 98 | + getThumbnailOfSourceHttp.mockReturnValue(avatarUrl2); |
| 99 | + expect(avatarUrlForRoom(room, 128, 256, "crop")).toEqual(avatarUrl2); |
| 100 | + expect(getThumbnailOfSourceHttp).toHaveBeenCalledWith(128, 256, "crop"); |
| 101 | + }); |
| 102 | +}); |
0 commit comments