2022-10-24 06:50:21 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import "jest-mock";
|
|
|
|
import { screen, act, render } from "@testing-library/react";
|
2023-01-11 11:49:03 +00:00
|
|
|
import { MatrixEvent, MsgType, RelationType } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { PendingEventOrdering } from "matrix-js-sdk/src/client";
|
2022-10-24 06:50:21 +00:00
|
|
|
import { NotificationCountType, Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { EventStatus } from "matrix-js-sdk/src/models/event-status";
|
2023-01-11 11:49:03 +00:00
|
|
|
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";
|
2022-10-24 06:50:21 +00:00
|
|
|
|
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-11 11:49:03 +00:00
|
|
|
import { mkEvent, mkMessage, stubClient } from "../../../../test-utils/test-utils";
|
2022-10-24 06:50:21 +00:00
|
|
|
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
|
|
|
|
import * as RoomNotifs from "../../../../../src/RoomNotifs";
|
|
|
|
|
|
|
|
jest.mock("../../../../../src/RoomNotifs");
|
|
|
|
jest.mock("../../../../../src/RoomNotifs", () => ({
|
|
|
|
...(jest.requireActual("../../../../../src/RoomNotifs") as Object),
|
|
|
|
getRoomNotifsState: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
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-11 11:49:03 +00:00
|
|
|
stubClient();
|
|
|
|
const client = MatrixClientPeg.get();
|
2022-10-24 06:50:21 +00:00
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
function getComponent(threadId?: string) {
|
|
|
|
return <UnreadNotificationBadge room={room} threadId={threadId} />;
|
|
|
|
}
|
|
|
|
|
2023-01-11 11:49:03 +00:00
|
|
|
beforeAll(() => {
|
2023-01-30 12:20:11 +00:00
|
|
|
client.supportsThreads = () => true;
|
2023-01-11 11:49:03 +00:00
|
|
|
});
|
|
|
|
|
2022-10-24 06:50:21 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
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();
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_highlighted")).toBeFalsy();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
room.setUnreadNotificationCount(NotificationCountType.Highlight, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_highlighted")).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders unread thread notification badge", () => {
|
|
|
|
const { container } = render(getComponent(THREAD_ID));
|
|
|
|
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_visible")).toBeTruthy();
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_highlighted")).toBeFalsy();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_highlighted")).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
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", () => {
|
|
|
|
jest.spyOn(room, "getMyMembership").mockReturnValue("invite");
|
|
|
|
render(getComponent());
|
|
|
|
expect(screen.queryByText("!")).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("hides counter for muted rooms", () => {
|
|
|
|
jest.spyOn(RoomNotifs, "getRoomNotifsState").mockReset().mockReturnValue(RoomNotifs.RoomNotifState.Mute);
|
|
|
|
|
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge")).toBeNull();
|
|
|
|
});
|
2023-01-11 11:49:03 +00:00
|
|
|
|
|
|
|
it("activity renders unread notification badge", () => {
|
|
|
|
act(() => {
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 0);
|
|
|
|
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 0);
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ts: 5,
|
|
|
|
});
|
|
|
|
room.addLiveEvents([event]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const { container } = render(getComponent(THREAD_ID));
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_dot")).toBeTruthy();
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_visible")).toBeTruthy();
|
|
|
|
expect(container.querySelector(".mx_NotificationBadge_highlighted")).toBeFalsy();
|
|
|
|
});
|
2022-10-24 06:50:21 +00:00
|
|
|
});
|