Room notification state: add clearer methods, documentation and deprecation (#29564)
* feat(notification state): add clearer methods, documentation and deprecation * test(room notification state): add tests for new attributes * doc: more explicit documentation for `hasUnreadCount` * doc: add link to `RoomNotificationState.isMention` in `hasMentions` doc * refactor: change `isSilent` to `hasAnyNotificationOrActivity` * refactor: add `invited` to `determineUnreadState` and use it in `NotificationState` & `RoomNotificationState` * test: update `RoomNotificationState` test to use `invited` * test: update other tests to add `invited` * refactor: remove count check in `isNotification`
This commit is contained in:
@@ -24,6 +24,9 @@ import { RoomNotificationState } from "../../../../src/stores/notifications/Room
|
||||
import { NotificationStateEvents } from "../../../../src/stores/notifications/NotificationState";
|
||||
import { NotificationLevel } from "../../../../src/stores/notifications/NotificationLevel";
|
||||
import { createMessageEventContent } from "../../../test-utils/events";
|
||||
import SettingsStore from "../../../../src/settings/SettingsStore";
|
||||
import * as RoomStatusBarModule from "../../../../src/components/structures/RoomStatusBar";
|
||||
import * as UnreadModule from "../../../../src/Unread";
|
||||
|
||||
describe("RoomNotificationState", () => {
|
||||
let room: Room;
|
||||
@@ -36,6 +39,10 @@ describe("RoomNotificationState", () => {
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
function addThread(room: Room): void {
|
||||
const threadId = "thread_id";
|
||||
jest.spyOn(room, "eventShouldLiveIn").mockReturnValue({
|
||||
@@ -200,4 +207,85 @@ describe("RoomNotificationState", () => {
|
||||
expect(roomNotifState.level).toBe(NotificationLevel.Activity);
|
||||
expect(roomNotifState.symbol).toBe(null);
|
||||
});
|
||||
|
||||
describe("computed attributes", () => {
|
||||
beforeEach(() => {
|
||||
jest.spyOn(RoomStatusBarModule, "getUnsentMessages").mockReturnValue([]);
|
||||
jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(false);
|
||||
});
|
||||
|
||||
it("should has invited at true", () => {
|
||||
room.updateMyMembership(KnownMembership.Invite);
|
||||
const roomNotifState = new RoomNotificationState(room, false);
|
||||
expect(roomNotifState.invited).toBe(true);
|
||||
});
|
||||
|
||||
it("should has isUnsetMessage at true", () => {
|
||||
jest.spyOn(RoomStatusBarModule, "getUnsentMessages").mockReturnValue([{} as MatrixEvent]);
|
||||
const roomNotifState = new RoomNotificationState(room, false);
|
||||
expect(roomNotifState.isUnsetMessage).toBe(true);
|
||||
});
|
||||
|
||||
it("should has isMention at false if the notification is invitation, an unset message or a knock", () => {
|
||||
setUnreads(room, 0, 2);
|
||||
|
||||
const roomNotifState = new RoomNotificationState(room, false);
|
||||
expect(roomNotifState.isMention).toBe(true);
|
||||
|
||||
room.updateMyMembership(KnownMembership.Invite);
|
||||
expect(roomNotifState.isMention).toBe(false);
|
||||
|
||||
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
|
||||
room.updateMyMembership(KnownMembership.Knock);
|
||||
expect(roomNotifState.isMention).toBe(false);
|
||||
|
||||
jest.spyOn(RoomStatusBarModule, "getUnsentMessages").mockReturnValue([{} as MatrixEvent]);
|
||||
room.updateMyMembership(KnownMembership.Join);
|
||||
expect(roomNotifState.isMention).toBe(false);
|
||||
});
|
||||
|
||||
it("should has isNotification at true", () => {
|
||||
setUnreads(room, 1, 0);
|
||||
|
||||
const roomNotifState = new RoomNotificationState(room, false);
|
||||
expect(roomNotifState.isNotification).toBe(true);
|
||||
});
|
||||
|
||||
it("should has isActivityNotification at true", () => {
|
||||
jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(true);
|
||||
|
||||
const roomNotifState = new RoomNotificationState(room, false);
|
||||
expect(roomNotifState.isActivityNotification).toBe(true);
|
||||
});
|
||||
|
||||
it("should has hasAnyNotificationOrActivity at true", () => {
|
||||
// Hidebold is disabled
|
||||
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
||||
// Unread message, generate activity notification
|
||||
jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(true);
|
||||
// Highlight notification
|
||||
setUnreads(room, 0, 1);
|
||||
|
||||
// There is one highlight notification
|
||||
const roomNotifState = new RoomNotificationState(room, false);
|
||||
expect(roomNotifState.hasAnyNotificationOrActivity).toBe(true);
|
||||
|
||||
// Activity notification
|
||||
setUnreads(room, 0, 0);
|
||||
// Trigger update
|
||||
room.updateMyMembership(KnownMembership.Join);
|
||||
// hidebold is disabled and we have an activity notification
|
||||
expect(roomNotifState.hasAnyNotificationOrActivity).toBe(true);
|
||||
|
||||
// hidebold is enabled and we have an activity notification
|
||||
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
|
||||
room.updateMyMembership(KnownMembership.Join);
|
||||
expect(roomNotifState.hasAnyNotificationOrActivity).toBe(false);
|
||||
|
||||
// No unread
|
||||
jest.spyOn(UnreadModule, "doesRoomHaveUnreadMessages").mockReturnValue(false);
|
||||
room.updateMyMembership(KnownMembership.Join);
|
||||
expect(roomNotifState.hasAnyNotificationOrActivity).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -67,9 +67,9 @@ describe("ImportanceAlgorithm", () => {
|
||||
};
|
||||
|
||||
const unreadStates: Record<string, ReturnType<(typeof RoomNotifs)["determineUnreadState"]>> = {
|
||||
red: { symbol: null, count: 1, level: NotificationLevel.Highlight },
|
||||
grey: { symbol: null, count: 1, level: NotificationLevel.Notification },
|
||||
none: { symbol: null, count: 0, level: NotificationLevel.None },
|
||||
red: { symbol: null, count: 1, level: NotificationLevel.Highlight, invited: false },
|
||||
grey: { symbol: null, count: 1, level: NotificationLevel.Notification, invited: false },
|
||||
none: { symbol: null, count: 0, level: NotificationLevel.None, invited: false },
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -77,6 +77,7 @@ describe("ImportanceAlgorithm", () => {
|
||||
symbol: null,
|
||||
count: 0,
|
||||
level: NotificationLevel.None,
|
||||
invited: false,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -183,6 +184,7 @@ describe("ImportanceAlgorithm", () => {
|
||||
symbol: null,
|
||||
count: 0,
|
||||
level: NotificationLevel.None,
|
||||
invited: false,
|
||||
});
|
||||
const algorithm = setupAlgorithm(sortAlgorithm);
|
||||
|
||||
@@ -353,6 +355,7 @@ describe("ImportanceAlgorithm", () => {
|
||||
symbol: null,
|
||||
count: 0,
|
||||
level: NotificationLevel.None,
|
||||
invited: false,
|
||||
});
|
||||
const algorithm = setupAlgorithm(sortAlgorithm);
|
||||
|
||||
|
||||
@@ -190,6 +190,7 @@ describe("NaturalAlgorithm", () => {
|
||||
symbol: null,
|
||||
count: 0,
|
||||
level: NotificationLevel.None,
|
||||
invited: false,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user