Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

TAC: Utils tests #12200

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Unread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export function doesRoomHaveUnreadThreads(room: Room): boolean {
return false;
}

for (const withTimeline of room.getThreads()) {
if (doesTimelineHaveUnreadMessages(room, withTimeline.timeline)) {
for (const thread of room.getThreads()) {
if (doesTimelineHaveUnreadMessages(room, thread.timeline)) {
// We found an unread, so the room is unread
return true;
}
Expand Down
109 changes: 109 additions & 0 deletions test/Unread-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { makeBeaconEvent, mkEvent, stubClient } from "./test-utils";
import { makeThreadEvents, mkThread, populateThread } from "./test-utils/threads";
import {
doesRoomHaveUnreadMessages,
doesRoomHaveUnreadThreads,
doesRoomOrThreadHaveUnreadMessages,
eventTriggersUnreadCount,
} from "../src/Unread";
Expand Down Expand Up @@ -528,4 +529,112 @@ describe("Unread", () => {
});
});
});

describe("doesRoomHaveUnreadThreads()", () => {
let room: Room;
const roomId = "!abc:server.org";
const myId = client.getSafeUserId();

beforeAll(() => {
client.supportsThreads = () => true;
});

beforeEach(async () => {
room = new Room(roomId, client, myId);
jest.spyOn(logger, "warn");

// Don't care about the code path of hidden events.
mocked(haveRendererForEvent).mockClear().mockReturnValue(true);
});

it("returns false when no threads", () => {
expect(doesRoomHaveUnreadThreads(room)).toBe(false);

// Add event to the room
const event = mkEvent({
event: true,
type: "m.room.message",
user: aliceId,
room: roomId,
content: {},
});
room.addLiveEvents([event]);

// It still returns false
expect(doesRoomHaveUnreadThreads(room)).toBe(false);
});

it("return true when we don't have any receipt for the thread", async () => {
await populateThread({
room,
client,
authorId: myId,
participantUserIds: [aliceId],
});

// There is no receipt for the thread, it should be unread
expect(doesRoomHaveUnreadThreads(room)).toBe(true);
});

it("return false when we have a receipt for the thread", async () => {
const { events, rootEvent } = await populateThread({
room,
client,
authorId: myId,
participantUserIds: [aliceId],
});

// Mark the thread as read.
const receipt = new MatrixEvent({
type: "m.receipt",
room_id: "!foo:bar",
content: {
[events[events.length - 1].getId()!]: {
[ReceiptType.Read]: {
[myId]: { ts: 1, thread_id: rootEvent.getId()! },
},
},
},
});
room.addReceipt(receipt);

// There is a receipt for the thread, it should be read
expect(doesRoomHaveUnreadThreads(room)).toBe(false);
});

it("return true when only of the threads has a receipt", async () => {
// Create a first thread
await populateThread({
room,
client,
authorId: myId,
participantUserIds: [aliceId],
});

// Create a second thread
const { events, rootEvent } = await populateThread({
room,
client,
authorId: myId,
participantUserIds: [aliceId],
});

// Mark the thread as read.
const receipt = new MatrixEvent({
type: "m.receipt",
room_id: "!foo:bar",
content: {
[events[events.length - 1].getId()!]: {
[ReceiptType.Read]: {
[myId]: { ts: 1, thread_id: rootEvent.getId()! },
},
},
},
});
room.addReceipt(receipt);

// The first thread doesn't have a receipt, it should be unread
expect(doesRoomHaveUnreadThreads(room)).toBe(true);
});
});
});
24 changes: 24 additions & 0 deletions test/utils/notifications-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
clearAllNotifications,
clearRoomNotification,
notificationLevelToIndicator,
getThreadNotificationLevel,
} from "../../src/utils/notifications";
import SettingsStore from "../../src/settings/SettingsStore";
import { getMockClientWithEventEmitter } from "../test-utils/client";
Expand Down Expand Up @@ -235,4 +236,27 @@ describe("notifications", () => {
expect(notificationLevelToIndicator(NotificationLevel.Highlight)).toEqual("critical");
});
});

describe("getThreadNotificationLevel", () => {
let room: Room;

const ROOM_ID = "123";
const USER_ID = "@bob:example.org";

beforeEach(() => {
room = new Room(ROOM_ID, MatrixClientPeg.safeGet(), USER_ID);
});

it.each([
{ notificationCountType: NotificationCountType.Highlight, expected: NotificationLevel.Highlight },
{ notificationCountType: NotificationCountType.Total, expected: NotificationLevel.Notification },
{ notificationCountType: null, expected: NotificationLevel.Activity },
])(
"returns NotificationLevel $expected when notificationCountType is $expected",
({ notificationCountType, expected }) => {
jest.spyOn(room, "threadsAggregateNotificationType", "get").mockReturnValue(notificationCountType);
expect(getThreadNotificationLevel(room)).toEqual(expected);
},
);
});
});
Loading