2022-10-24 06:50:21 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2022-10-24 06:50:21 +00:00
|
|
|
|
2024-09-09 13:57:16 +00: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 06:50:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import "jest-mock";
|
|
|
|
import { screen, act, render } from "@testing-library/react";
|
2023-08-09 07:18:41 +00:00
|
|
|
import {
|
|
|
|
MatrixEvent,
|
|
|
|
MsgType,
|
|
|
|
RelationType,
|
|
|
|
NotificationCountType,
|
|
|
|
Room,
|
|
|
|
EventStatus,
|
|
|
|
PendingEventOrdering,
|
2023-08-23 09:04:25 +00:00
|
|
|
ReceiptType,
|
2023-08-09 07:18:41 +00:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-18 14:40:52 +00:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2022-10-24 06:50:21 +00:00
|
|
|
|
2023-08-09 07:18:41 +00:00
|
|
|
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2023-01-11 11:49:03 +00:00
|
|
|
import { mkThread } from "../../../../test-utils/threads";
|
2022-10-24 06:50:21 +00:00
|
|
|
import { UnreadNotificationBadge } from "../../../../../src/components/views/rooms/NotificationBadge/UnreadNotificationBadge";
|
2023-01-31 09:58:17 +00:00
|
|
|
import { mkEvent, mkMessage, muteRoom, stubClient } from "../../../../test-utils/test-utils";
|
2022-10-24 06:50:21 +00:00
|
|
|
import * as RoomNotifs from "../../../../../src/RoomNotifs";
|
|
|
|
|
|
|
|
const ROOM_ID = "!roomId:example.org";
|
2023-01-11 11:49:03 +00:00
|
|
|
let THREAD_ID: string;
|
2022-10-24 06:50:21 +00:00
|
|
|
|
|
|
|
describe("UnreadNotificationBadge", () => {
|
2023-01-31 09:58:17 +00:00
|
|
|
let client: MatrixClient;
|
2022-10-24 06:50:21 +00:00
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
function getComponent(threadId?: string) {
|
|
|
|
return <UnreadNotificationBadge room={room} threadId={threadId} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-01-31 09:58:17 +00:00
|
|
|
client = stubClient();
|
|
|
|
client.supportsThreads = () => true;
|
2022-10-24 06:50:21 +00:00
|
|
|
|
2023-01-11 11:49:03 +00:00
|
|
|
room = new Room(ROOM_ID, client, client.getUserId()!, {
|
2022-10-24 06:50:21 +00:00
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
2023-01-11 11:49:03 +00:00
|
|
|
|
|
|
|
const receipt = new MatrixEvent({
|
|
|
|
type: "m.receipt",
|
|
|
|
room_id: room.roomId,
|
|
|
|
content: {
|
|
|
|
"$event0:localhost": {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[client.getUserId()!]: { ts: 1, thread_id: "$otherthread:localhost" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"$event1:localhost": {
|
|
|
|
[ReceiptType.Read]: {
|
|
|
|
[client.getUserId()!]: { ts: 1 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
room.addReceipt(receipt);
|
|
|
|
|
2022-10-24 06:50:21 +00:00
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Total, 1);
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 0);
|
|
|
|
|
2023-01-11 11:49:03 +00:00
|
|
|
const { rootEvent } = mkThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: client.getUserId()!,
|
|
|
|
participantUserIds: [client.getUserId()!],
|
|
|
|
});
|
|
|
|
THREAD_ID = rootEvent.getId()!;
|
|
|
|
|
2022-10-24 06:50:21 +00:00
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 1);
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 0);
|
|
|
|
|
|
|
|
jest.spyOn(RoomNotifs, "getRoomNotifsState").mockReturnValue(RoomNotifs.RoomNotifState.AllMessages);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders unread notification badge", () => {
|
|
|
|
const { container } = render(getComponent());
|
|
|
|
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_visible")).toBeTruthy();
|
2024-01-25 16:53:41 +00:00
|
|
|
expect(container.querySelector(".mx_NotificationBadge_level_highlight")).toBeFalsy();
|
2022-10-24 06:50:21 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 1);
|
|
|
|
});
|
|
|
|
|
2024-01-25 16:53:41 +00:00
|
|
|
expect(container.querySelector(".mx_NotificationBadge_level_highlight")).toBeTruthy();
|
2022-10-24 06:50:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("renders unread thread notification badge", () => {
|
|
|
|
const { container } = render(getComponent(THREAD_ID));
|
|
|
|
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_visible")).toBeTruthy();
|
2024-01-25 16:53:41 +00:00
|
|
|
expect(container.querySelector(".mx_NotificationBadge_level_highlight")).toBeFalsy();
|
2022-10-24 06:50:21 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 1);
|
|
|
|
});
|
|
|
|
|
2024-01-25 16:53:41 +00:00
|
|
|
expect(container.querySelector(".mx_NotificationBadge_level_highlight")).toBeTruthy();
|
2022-10-24 06:50:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("hides unread notification badge", () => {
|
|
|
|
act(() => {
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 0);
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 0);
|
|
|
|
const { container } = render(getComponent(THREAD_ID));
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_visible")).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adds a warning for unsent messages", () => {
|
|
|
|
const evt = mkMessage({
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@alice:example.org",
|
|
|
|
msg: "Hello world!",
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
evt.status = EventStatus.NOT_SENT;
|
|
|
|
|
|
|
|
room.addPendingEvent(evt, "123");
|
|
|
|
|
|
|
|
render(getComponent());
|
|
|
|
|
|
|
|
expect(screen.queryByText("!")).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adds a warning for invites", () => {
|
2024-03-12 14:52:54 +00:00
|
|
|
room.updateMyMembership(KnownMembership.Invite);
|
2022-10-24 06:50:21 +00:00
|
|
|
render(getComponent());
|
|
|
|
expect(screen.queryByText("!")).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("hides counter for muted rooms", () => {
|
2023-01-31 09:58:17 +00:00
|
|
|
muteRoom(room);
|
2022-10-24 06:50:21 +00:00
|
|
|
|
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge")).toBeNull();
|
|
|
|
});
|
2023-01-11 11:49:03 +00:00
|
|
|
|
|
|
|
it("activity renders unread notification badge", () => {
|
2023-01-31 09:58:17 +00:00
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 0);
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 0);
|
2023-01-11 11:49:03 +00:00
|
|
|
|
2023-01-31 09:58:17 +00:00
|
|
|
// Add another event on the thread which is not sent by us.
|
|
|
|
const event = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@alice:server.org",
|
|
|
|
room: room.roomId,
|
|
|
|
content: {
|
|
|
|
"msgtype": MsgType.Text,
|
|
|
|
"body": "Hello from Bob",
|
|
|
|
"m.relates_to": {
|
|
|
|
event_id: THREAD_ID,
|
|
|
|
rel_type: RelationType.Thread,
|
2023-01-11 11:49:03 +00:00
|
|
|
},
|
2023-01-31 09:58:17 +00:00
|
|
|
},
|
|
|
|
ts: 5,
|
2023-01-11 11:49:03 +00:00
|
|
|
});
|
2023-01-31 09:58:17 +00:00
|
|
|
room.addLiveEvents([event]);
|
2023-01-11 11:49:03 +00:00
|
|
|
|
|
|
|
const { container } = render(getComponent(THREAD_ID));
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_dot")).toBeTruthy();
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_visible")).toBeTruthy();
|
2024-01-25 16:53:41 +00:00
|
|
|
expect(container.querySelector(".mx_NotificationBadge_level_highlight")).toBeFalsy();
|
2023-01-11 11:49:03 +00:00
|
|
|
});
|
2022-10-24 06:50:21 +00:00
|
|
|
});
|