2022-10-24 09:50:21 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2022-10-24 09:50:21 +03:00
|
|
|
|
2024-09-09 16:57:16 +03: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-10-24 09:50:21 +03:00
|
|
|
*/
|
|
|
|
|
2024-08-06 20:22:02 +03:00
|
|
|
import { render, waitFor } from "@testing-library/react";
|
2023-08-09 10:18:41 +03:00
|
|
|
import {
|
|
|
|
MatrixEvent,
|
|
|
|
MsgType,
|
|
|
|
RelationType,
|
|
|
|
NotificationCountType,
|
|
|
|
Room,
|
|
|
|
MatrixClient,
|
|
|
|
PendingEventOrdering,
|
2023-08-23 12:04:25 +03:00
|
|
|
ReceiptType,
|
2023-08-09 10:18:41 +03:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-10-24 09:50:21 +03:00
|
|
|
import React from "react";
|
|
|
|
|
2023-08-01 10:32:53 +03:00
|
|
|
import LegacyRoomHeaderButtons from "../../../../src/components/views/right_panel/LegacyRoomHeaderButtons";
|
2022-10-24 09:50:21 +03:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2023-01-11 14:49:03 +03:00
|
|
|
import { mkEvent, stubClient } from "../../../test-utils";
|
|
|
|
import { mkThread } from "../../../test-utils/threads";
|
2022-10-24 09:50:21 +03:00
|
|
|
|
2023-08-01 10:32:53 +03:00
|
|
|
describe("LegacyRoomHeaderButtons-test.tsx", function () {
|
2022-10-24 09:50:21 +03:00
|
|
|
const ROOM_ID = "!roomId:example.org";
|
|
|
|
let room: Room;
|
|
|
|
let client: MatrixClient;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
|
|
|
stubClient();
|
2023-06-05 20:12:23 +03:00
|
|
|
client = MatrixClientPeg.safeGet();
|
2023-01-30 15:20:11 +03:00
|
|
|
client.supportsThreads = () => true;
|
2022-11-10 21:01:19 +03:00
|
|
|
room = new Room(ROOM_ID, client, client.getUserId() ?? "", {
|
2022-10-24 09:50:21 +03:00
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-10 21:01:19 +03:00
|
|
|
function getComponent(room?: Room) {
|
2023-08-01 10:32:53 +03:00
|
|
|
return render(<LegacyRoomHeaderButtons room={room} excludedRightPanelPhaseButtons={[]} />);
|
2022-10-24 09:50:21 +03:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:49:03 +03:00
|
|
|
function getThreadButton(container: HTMLElement) {
|
2022-10-24 09:50:21 +03:00
|
|
|
return container.querySelector(".mx_RightPanel_threadsButton");
|
|
|
|
}
|
|
|
|
|
2024-01-15 18:25:48 +03:00
|
|
|
function isIndicatorOfType(container: HTMLElement, type: "highlight" | "notification" | "activity") {
|
2023-01-11 14:49:03 +03:00
|
|
|
return container.querySelector(".mx_RightPanel_threadsButton .mx_Indicator")!.className.includes(type);
|
2022-10-24 09:50:21 +03:00
|
|
|
}
|
|
|
|
|
2023-05-12 22:49:07 +03:00
|
|
|
it("should render", () => {
|
|
|
|
const { asFragment } = getComponent(room);
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-10-24 09:50:21 +03:00
|
|
|
it("shows the thread button", () => {
|
|
|
|
const { container } = getComponent(room);
|
|
|
|
expect(getThreadButton(container)).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("room wide notification does not change the thread button", () => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 1);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 1);
|
|
|
|
|
|
|
|
const { container } = getComponent(room);
|
|
|
|
|
|
|
|
expect(container.querySelector(".mx_RightPanel_threadsButton .mx_Indicator")).toBeNull();
|
|
|
|
});
|
|
|
|
|
2024-08-06 20:22:02 +03:00
|
|
|
it("thread notification does change the thread button", async () => {
|
2022-10-24 09:50:21 +03:00
|
|
|
const { container } = getComponent(room);
|
2023-08-01 10:32:53 +03:00
|
|
|
expect(getThreadButton(container)!.className.includes("mx_LegacyRoomHeader_button--unread")).toBeFalsy();
|
2022-10-24 09:50:21 +03:00
|
|
|
|
|
|
|
room.setThreadUnreadNotificationCount("$123", NotificationCountType.Total, 1);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(getThreadButton(container)!.className.includes("mx_LegacyRoomHeader_button--unread")).toBeTruthy();
|
|
|
|
expect(isIndicatorOfType(container, "notification")).toBe(true);
|
|
|
|
});
|
2022-10-24 09:50:21 +03:00
|
|
|
|
|
|
|
room.setThreadUnreadNotificationCount("$123", NotificationCountType.Highlight, 1);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => expect(isIndicatorOfType(container, "highlight")).toBe(true));
|
2022-10-24 09:50:21 +03:00
|
|
|
|
|
|
|
room.setThreadUnreadNotificationCount("$123", NotificationCountType.Total, 0);
|
|
|
|
room.setThreadUnreadNotificationCount("$123", NotificationCountType.Highlight, 0);
|
|
|
|
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => expect(container.querySelector(".mx_RightPanel_threadsButton .mx_Indicator")).toBeNull());
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|
2022-11-10 21:01:19 +03:00
|
|
|
|
2023-01-11 14:49:03 +03:00
|
|
|
it("thread activity does change the thread button", async () => {
|
|
|
|
const { container } = getComponent(room);
|
|
|
|
|
|
|
|
// Thread activity should appear on the icon.
|
|
|
|
const { rootEvent, events } = mkThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: client.getUserId()!,
|
|
|
|
participantUserIds: ["@alice:example.org"],
|
2023-11-29 16:36:52 +03:00
|
|
|
length: 5,
|
2023-01-11 14:49:03 +03:00
|
|
|
});
|
2023-11-28 16:59:03 +03:00
|
|
|
// We need some receipt, otherwise we treat this thread as
|
|
|
|
// "older than all threaded receipts" and consider it read.
|
|
|
|
let receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: room.roomId,
|
|
|
|
content: {
|
2023-11-29 16:36:52 +03:00
|
|
|
[events[1].getId()!]: {
|
2023-11-28 16:59:03 +03:00
|
|
|
// Receipt for the first event in the thread
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[client.getUserId()!]: { ts: 1, thread_id: rootEvent.getId() },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => expect(isIndicatorOfType(container, "activity")).toBe(true));
|
2023-01-11 14:49:03 +03:00
|
|
|
|
|
|
|
// Sending the last event should clear the notification.
|
|
|
|
let event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: client.getUserId()!,
|
|
|
|
room: room.roomId,
|
|
|
|
content: {
|
|
|
|
"msgtype": MsgType.Text,
|
|
|
|
"body": "Test",
|
|
|
|
"m.relates_to": {
|
|
|
|
event_id: rootEvent.getId(),
|
|
|
|
rel_type: RelationType.Thread,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event]);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => expect(container.querySelector(".mx_RightPanel_threadsButton .mx_Indicator")).toBeNull());
|
2023-01-11 14:49:03 +03:00
|
|
|
|
|
|
|
// Mark it as unread again.
|
|
|
|
event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
room: room.roomId,
|
|
|
|
content: {
|
|
|
|
"msgtype": MsgType.Text,
|
|
|
|
"body": "Test",
|
|
|
|
"m.relates_to": {
|
|
|
|
event_id: rootEvent.getId(),
|
|
|
|
rel_type: RelationType.Thread,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event]);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => expect(isIndicatorOfType(container, "activity")).toBe(true));
|
2023-01-11 14:49:03 +03:00
|
|
|
|
|
|
|
// Sending a read receipt on an earlier event shouldn't do anything.
|
2023-11-28 16:59:03 +03:00
|
|
|
receipt = new MatrixEvent({
|
2023-01-11 14:49:03 +03:00
|
|
|
type: "m.receipt",
|
|
|
|
room_id: room.roomId,
|
|
|
|
content: {
|
|
|
|
[events.at(-1)!.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[client.getUserId()!]: { ts: 1, thread_id: rootEvent.getId() },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => expect(isIndicatorOfType(container, "activity")).toBe(true));
|
2023-01-11 14:49:03 +03:00
|
|
|
|
|
|
|
// Sending a receipt on the latest event should clear the notification.
|
|
|
|
receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: room.roomId,
|
|
|
|
content: {
|
|
|
|
[event.getId()!]: {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[client.getUserId()!]: { ts: 1, thread_id: rootEvent.getId() },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitFor(() => expect(container.querySelector(".mx_RightPanel_threadsButton .mx_Indicator")).toBeNull());
|
2023-01-11 14:49:03 +03:00
|
|
|
});
|
2022-10-24 09:50:21 +03:00
|
|
|
});
|