element-web-Github/test/unit-tests/stores/RoomNotificationStateStore-test.ts
Michael Telatynski c05c429803
Absorb the matrix-react-sdk repository (#28192)
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com>
Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Florian Duros <florian.duros@ormaz.fr>
Co-authored-by: Kim Brose <kim.brose@nordeck.net>
Co-authored-by: Florian Duros <florianduros@element.io>
Co-authored-by: R Midhun Suresh <hi@midhun.dev>
Co-authored-by: dbkr <986903+dbkr@users.noreply.github.com>
Co-authored-by: ElementRobot <releases@riot.im>
Co-authored-by: dbkr <dbkr@users.noreply.github.com>
Co-authored-by: David Baker <dbkr@users.noreply.github.com>
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Co-authored-by: David Langley <davidl@element.io>
Co-authored-by: Michael Weimann <michaelw@matrix.org>
Co-authored-by: Timshel <Timshel@users.noreply.github.com>
Co-authored-by: Sahil Silare <32628578+sahil9001@users.noreply.github.com>
Co-authored-by: Will Hunt <will@half-shot.uk>
Co-authored-by: Hubert Chathi <hubert@uhoreg.ca>
Co-authored-by: Andrew Ferrazzutti <andrewf@element.io>
Co-authored-by: Robin <robin@robin.town>
Co-authored-by: Tulir Asokan <tulir@maunium.net>
2024-10-16 13:31:55 +01:00

124 lines
4.9 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { mocked } from "jest-mock";
import { ClientEvent, MatrixClient, Room, SyncState } from "matrix-js-sdk/src/matrix";
import { createTestClient, setupAsyncStoreWithClient } from "../../test-utils";
import {
RoomNotificationStateStore,
UPDATE_STATUS_INDICATOR,
} from "../../../src/stores/notifications/RoomNotificationStateStore";
import SettingsStore from "../../../src/settings/SettingsStore";
import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher";
describe("RoomNotificationStateStore", function () {
let store: RoomNotificationStateStore;
let client: MatrixClient;
let dis: MatrixDispatcher;
beforeEach(() => {
client = createTestClient();
dis = new MatrixDispatcher();
jest.resetAllMocks();
store = RoomNotificationStateStore.testInstance(dis);
store.emit = jest.fn();
setupAsyncStoreWithClient(store, client);
});
it("Emits no event when a room has no unreads", async () => {
// Given a room with 0 unread messages
const room = fakeRoom(0);
// When we sync and the room is visible
mocked(client.getVisibleRooms).mockReturnValue([room]);
client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing);
// Then we emit an event from the store
expect(store.emit).not.toHaveBeenCalled();
});
it("Emits an event when a room has unreads", async () => {
// Given a room with 2 unread messages
const room = fakeRoom(2);
// When we sync and the room is visible
mocked(client.getVisibleRooms).mockReturnValue([room]);
client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing);
// Then we emit an event from the store
expect(store.emit).toHaveBeenCalledWith(UPDATE_STATUS_INDICATOR, expect.anything(), "SYNCING");
});
it("Emits an event when a feature flag changes notification state", async () => {
// Given we have synced already
let room = fakeRoom(0);
mocked(store.emit).mockReset();
mocked(client.getVisibleRooms).mockReturnValue([room]);
client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing);
expect(store.emit).not.toHaveBeenCalled();
// When we update the feature flag and it makes us have a notification
room = fakeRoom(2);
mocked(client.getVisibleRooms).mockReturnValue([room]);
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
store.emitUpdateIfStateChanged(SyncState.Syncing, false);
// Then we get notified
expect(store.emit).toHaveBeenCalledWith(UPDATE_STATUS_INDICATOR, expect.anything(), "SYNCING");
});
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
beforeEach(() => {
// Turn off feature_dynamic_room_predecessors setting
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
});
it("Passes the dynamic predecessor flag to getVisibleRooms", async () => {
// When we sync
mocked(client.getVisibleRooms).mockReturnValue([]);
client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing);
// Then we check visible rooms, using the dynamic predecessor flag
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
expect(client.getVisibleRooms).not.toHaveBeenCalledWith(true);
});
});
describe("If the feature_dynamic_room_predecessors is enabled", () => {
beforeEach(() => {
// Turn on feature_dynamic_room_predecessors setting
jest.spyOn(SettingsStore, "getValue").mockImplementation(
(settingName) => settingName === "feature_dynamic_room_predecessors",
);
});
it("Passes the dynamic predecessor flag to getVisibleRooms", async () => {
// When we sync
mocked(client.getVisibleRooms).mockReturnValue([]);
client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing);
// Then we check visible rooms, using the dynamic predecessor flag
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
expect(client.getVisibleRooms).not.toHaveBeenCalledWith(false);
});
});
let roomIdx = 0;
function fakeRoom(numUnreads: number): Room {
roomIdx++;
const ret = new Room(`room${roomIdx}`, client, "@user:example.com");
ret.getPendingEvents = jest.fn().mockReturnValue([]);
ret.isSpaceRoom = jest.fn().mockReturnValue(false);
ret.getUnreadNotificationCount = jest.fn().mockReturnValue(numUnreads);
ret.getRoomUnreadNotificationCount = jest.fn().mockReturnValue(numUnreads);
return ret;
}
});