2022-05-06 17:09:28 +08:00
|
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-05-06 17:09:28 +08:00
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
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-05-06 17:09:28 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2024-05-22 21:56:01 +08:00
|
|
|
|
import React, { ComponentProps } from "react";
|
2024-10-15 00:11:58 +08:00
|
|
|
|
import { render, screen, waitFor } from "jest-matrix-react";
|
2024-05-22 21:07:57 +08:00
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
determineAvatarPosition,
|
|
|
|
|
ReadReceiptPerson,
|
|
|
|
|
readReceiptTooltip,
|
|
|
|
|
} from "../../../../src/components/views/rooms/ReadReceiptGroup";
|
2023-10-04 02:17:26 +08:00
|
|
|
|
import * as languageHandler from "../../../../src/languageHandler";
|
2024-05-22 21:07:57 +08:00
|
|
|
|
import { stubClient } from "../../../test-utils";
|
2024-05-22 21:56:01 +08:00
|
|
|
|
import dispatcher from "../../../../src/dispatcher/dispatcher";
|
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
2022-05-02 17:46:11 +08:00
|
|
|
|
|
|
|
|
|
describe("ReadReceiptGroup", () => {
|
|
|
|
|
describe("TooltipText", () => {
|
|
|
|
|
it("returns '...and more' with hasMore", () => {
|
2023-10-04 02:17:26 +08:00
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan", "Eve", "Fox"], 5)).toEqual(
|
|
|
|
|
"Alice, Bob, Charlie, Dan, Eve and one other",
|
2022-05-02 17:46:11 +08:00
|
|
|
|
);
|
2023-10-04 02:17:26 +08:00
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan", "Eve", "Fox"], 4)).toEqual(
|
|
|
|
|
"Alice, Bob, Charlie, Dan and 2 others",
|
2022-05-02 17:46:11 +08:00
|
|
|
|
);
|
2023-10-04 02:17:26 +08:00
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan"], 3)).toEqual(
|
|
|
|
|
"Alice, Bob, Charlie and one other",
|
|
|
|
|
);
|
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan", "Eve", "Fox"], 2)).toEqual(
|
|
|
|
|
"Alice, Bob and 4 others",
|
|
|
|
|
);
|
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan", "Eve", "Fox"], 1)).toEqual(
|
|
|
|
|
"Alice and 5 others",
|
|
|
|
|
);
|
|
|
|
|
expect(readReceiptTooltip([], 1)).toBe("");
|
2022-05-02 17:46:11 +08:00
|
|
|
|
});
|
|
|
|
|
it("returns a pretty list without hasMore", () => {
|
2023-10-04 02:17:26 +08:00
|
|
|
|
jest.spyOn(languageHandler, "getUserLanguage").mockReturnValue("en-GB");
|
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan", "Eve"], 5)).toEqual(
|
2022-05-02 17:46:11 +08:00
|
|
|
|
"Alice, Bob, Charlie, Dan and Eve",
|
|
|
|
|
);
|
2023-10-04 02:17:26 +08:00
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan"], 4)).toEqual("Alice, Bob, Charlie and Dan");
|
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob", "Charlie"], 5)).toEqual("Alice, Bob and Charlie");
|
|
|
|
|
expect(readReceiptTooltip(["Alice", "Bob"], 5)).toEqual("Alice and Bob");
|
|
|
|
|
expect(readReceiptTooltip(["Alice"], 5)).toEqual("Alice");
|
|
|
|
|
expect(readReceiptTooltip([], 5)).toBe("");
|
2022-05-02 17:46:11 +08:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
describe("AvatarPosition", () => {
|
|
|
|
|
// The avatar slots are numbered from right to left
|
|
|
|
|
// That means currently, we’ve got the slots | 3 | 2 | 1 | 0 | each with 10px distance to the next one.
|
2022-05-05 20:10:48 +08:00
|
|
|
|
// We want to fill slots so the first avatar is in the right-most slot without leaving any slots at the left
|
2022-05-02 17:46:11 +08:00
|
|
|
|
// unoccupied.
|
|
|
|
|
it("to handle the non-overflowing case correctly", () => {
|
|
|
|
|
expect(determineAvatarPosition(0, 4)).toEqual({ hidden: false, position: 0 });
|
|
|
|
|
|
|
|
|
|
expect(determineAvatarPosition(0, 4)).toEqual({ hidden: false, position: 0 });
|
|
|
|
|
expect(determineAvatarPosition(1, 4)).toEqual({ hidden: false, position: 1 });
|
|
|
|
|
|
2022-05-05 20:10:48 +08:00
|
|
|
|
expect(determineAvatarPosition(0, 4)).toEqual({ hidden: false, position: 0 });
|
2022-05-02 17:46:11 +08:00
|
|
|
|
expect(determineAvatarPosition(1, 4)).toEqual({ hidden: false, position: 1 });
|
2022-05-05 20:10:48 +08:00
|
|
|
|
expect(determineAvatarPosition(2, 4)).toEqual({ hidden: false, position: 2 });
|
|
|
|
|
|
2022-05-02 17:46:11 +08:00
|
|
|
|
expect(determineAvatarPosition(0, 4)).toEqual({ hidden: false, position: 0 });
|
2022-05-05 20:10:48 +08:00
|
|
|
|
expect(determineAvatarPosition(1, 4)).toEqual({ hidden: false, position: 1 });
|
|
|
|
|
expect(determineAvatarPosition(2, 4)).toEqual({ hidden: false, position: 2 });
|
|
|
|
|
expect(determineAvatarPosition(3, 4)).toEqual({ hidden: false, position: 3 });
|
2022-05-02 17:46:11 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("to handle the overflowing case correctly", () => {
|
|
|
|
|
expect(determineAvatarPosition(0, 4)).toEqual({ hidden: false, position: 0 });
|
2022-05-05 20:10:48 +08:00
|
|
|
|
expect(determineAvatarPosition(1, 4)).toEqual({ hidden: false, position: 1 });
|
|
|
|
|
expect(determineAvatarPosition(2, 4)).toEqual({ hidden: false, position: 2 });
|
|
|
|
|
expect(determineAvatarPosition(3, 4)).toEqual({ hidden: false, position: 3 });
|
2022-05-02 17:46:11 +08:00
|
|
|
|
expect(determineAvatarPosition(4, 4)).toEqual({ hidden: true, position: 0 });
|
|
|
|
|
expect(determineAvatarPosition(5, 4)).toEqual({ hidden: true, position: 0 });
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-05-22 21:07:57 +08:00
|
|
|
|
|
|
|
|
|
describe("<ReadReceiptPerson />", () => {
|
|
|
|
|
stubClient();
|
|
|
|
|
|
|
|
|
|
const ROOM_ID = "roomId";
|
|
|
|
|
const USER_ID = "@alice:example.org";
|
|
|
|
|
|
|
|
|
|
const member = new RoomMember(ROOM_ID, USER_ID);
|
|
|
|
|
member.rawDisplayName = "Alice";
|
|
|
|
|
member.getMxcAvatarUrl = () => "http://placekitten.com/400/400";
|
|
|
|
|
|
2024-05-22 21:56:01 +08:00
|
|
|
|
const renderReadReceipt = (props?: Partial<ComponentProps<typeof ReadReceiptPerson>>) => {
|
2024-05-22 21:07:57 +08:00
|
|
|
|
const currentDate = new Date(2024, 4, 15).getTime();
|
2024-05-22 21:56:01 +08:00
|
|
|
|
return render(<ReadReceiptPerson userId={USER_ID} roomMember={member} ts={currentDate} {...props} />);
|
2024-05-22 21:07:57 +08:00
|
|
|
|
};
|
|
|
|
|
|
2024-05-22 21:56:01 +08:00
|
|
|
|
beforeEach(() => {
|
|
|
|
|
jest.spyOn(dispatcher, "dispatch");
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-22 21:07:57 +08:00
|
|
|
|
it("should render", () => {
|
|
|
|
|
const { container } = renderReadReceipt();
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should display a tooltip", async () => {
|
|
|
|
|
renderReadReceipt();
|
|
|
|
|
|
|
|
|
|
await userEvent.hover(screen.getByRole("menuitem"));
|
|
|
|
|
await waitFor(() => {
|
2024-10-15 00:11:58 +08:00
|
|
|
|
const tooltip = screen.getByRole("tooltip");
|
|
|
|
|
expect(tooltip.textContent).toMatch(new RegExp(member.rawDisplayName));
|
2024-05-22 21:07:57 +08:00
|
|
|
|
expect(tooltip).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-05-22 21:56:01 +08:00
|
|
|
|
|
|
|
|
|
it("should send an event when clicked", async () => {
|
|
|
|
|
const onAfterClick = jest.fn();
|
|
|
|
|
renderReadReceipt({ onAfterClick });
|
|
|
|
|
|
|
|
|
|
screen.getByRole("menuitem").click();
|
|
|
|
|
|
|
|
|
|
expect(onAfterClick).toHaveBeenCalled();
|
|
|
|
|
expect(dispatcher.dispatch).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
action: Action.ViewUser,
|
|
|
|
|
member,
|
|
|
|
|
push: false,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
2024-05-22 21:07:57 +08:00
|
|
|
|
});
|
2022-05-02 17:46:11 +08:00
|
|
|
|
});
|