|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { mocked } from "jest-mock"; |
| 18 | +import { ClientEvent, MatrixClient, Room } from "matrix-js-sdk/src/matrix"; |
| 19 | +import { SyncState } from "matrix-js-sdk/src/sync"; |
| 20 | + |
| 21 | +import { createTestClient, setupAsyncStoreWithClient } from "../test-utils"; |
| 22 | +import { |
| 23 | + RoomNotificationStateStore, |
| 24 | + UPDATE_STATUS_INDICATOR, |
| 25 | +} from "../../src/stores/notifications/RoomNotificationStateStore"; |
| 26 | +import SettingsStore from "../../src/settings/SettingsStore"; |
| 27 | +import { MatrixDispatcher } from "../../src/dispatcher/dispatcher"; |
| 28 | + |
| 29 | +describe("RoomNotificationStateStore", function () { |
| 30 | + let store: RoomNotificationStateStore; |
| 31 | + let client: MatrixClient; |
| 32 | + let dis: MatrixDispatcher; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + client = createTestClient(); |
| 36 | + dis = new MatrixDispatcher(); |
| 37 | + jest.resetAllMocks(); |
| 38 | + store = RoomNotificationStateStore.testInstance(dis); |
| 39 | + store.emit = jest.fn(); |
| 40 | + setupAsyncStoreWithClient(store, client); |
| 41 | + }); |
| 42 | + |
| 43 | + it("Emits no event when a room has no unreads", async () => { |
| 44 | + // Given a room with 0 unread messages |
| 45 | + const room = fakeRoom(0); |
| 46 | + |
| 47 | + // When we sync and the room is visible |
| 48 | + mocked(client.getVisibleRooms).mockReturnValue([room]); |
| 49 | + client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing); |
| 50 | + |
| 51 | + // Then we emit an event from the store |
| 52 | + expect(store.emit).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("Emits an event when a room has unreads", async () => { |
| 56 | + // Given a room with 2 unread messages |
| 57 | + const room = fakeRoom(2); |
| 58 | + |
| 59 | + // When we sync and the room is visible |
| 60 | + mocked(client.getVisibleRooms).mockReturnValue([room]); |
| 61 | + client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing); |
| 62 | + |
| 63 | + // Then we emit an event from the store |
| 64 | + expect(store.emit).toHaveBeenCalledWith(UPDATE_STATUS_INDICATOR, expect.anything(), "SYNCING"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("Emits an event when a feature flag changes notification state", async () => { |
| 68 | + // Given we have synced already |
| 69 | + let room = fakeRoom(0); |
| 70 | + mocked(store.emit).mockReset(); |
| 71 | + mocked(client.getVisibleRooms).mockReturnValue([room]); |
| 72 | + client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing); |
| 73 | + expect(store.emit).not.toHaveBeenCalled(); |
| 74 | + |
| 75 | + // When we update the feature flag and it makes us have a notification |
| 76 | + room = fakeRoom(2); |
| 77 | + mocked(client.getVisibleRooms).mockReturnValue([room]); |
| 78 | + jest.spyOn(SettingsStore, "getValue").mockReturnValue(true); |
| 79 | + store.emitUpdateIfStateChanged(SyncState.Syncing, false); |
| 80 | + |
| 81 | + // Then we get notified |
| 82 | + expect(store.emit).toHaveBeenCalledWith(UPDATE_STATUS_INDICATOR, expect.anything(), "SYNCING"); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("If the feature_dynamic_room_predecessors is not enabled", () => { |
| 86 | + beforeEach(() => { |
| 87 | + // Turn off feature_dynamic_room_predecessors setting |
| 88 | + jest.spyOn(SettingsStore, "getValue").mockReturnValue(false); |
| 89 | + }); |
| 90 | + |
| 91 | + it("Passes the dynamic predecessor flag to getVisibleRooms", async () => { |
| 92 | + // When we sync |
| 93 | + mocked(client.getVisibleRooms).mockReturnValue([]); |
| 94 | + client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing); |
| 95 | + |
| 96 | + // Then we check visible rooms, using the dynamic predecessor flag |
| 97 | + expect(client.getVisibleRooms).toHaveBeenCalledWith(false); |
| 98 | + expect(client.getVisibleRooms).not.toHaveBeenCalledWith(true); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("If the feature_dynamic_room_predecessors is enabled", () => { |
| 103 | + beforeEach(() => { |
| 104 | + // Turn on feature_dynamic_room_predecessors setting |
| 105 | + jest.spyOn(SettingsStore, "getValue").mockImplementation( |
| 106 | + (settingName) => settingName === "feature_dynamic_room_predecessors", |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it("Passes the dynamic predecessor flag to getVisibleRooms", async () => { |
| 111 | + // When we sync |
| 112 | + mocked(client.getVisibleRooms).mockReturnValue([]); |
| 113 | + client.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing); |
| 114 | + |
| 115 | + // Then we check visible rooms, using the dynamic predecessor flag |
| 116 | + expect(client.getVisibleRooms).toHaveBeenCalledWith(true); |
| 117 | + expect(client.getVisibleRooms).not.toHaveBeenCalledWith(false); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + let roomIdx = 0; |
| 122 | + |
| 123 | + function fakeRoom(numUnreads: number): Room { |
| 124 | + roomIdx++; |
| 125 | + const ret = new Room(`room${roomIdx}`, client, "@user:example.com"); |
| 126 | + ret.getPendingEvents = jest.fn().mockReturnValue([]); |
| 127 | + ret.isSpaceRoom = jest.fn().mockReturnValue(false); |
| 128 | + ret.getUnreadNotificationCount = jest.fn().mockReturnValue(numUnreads); |
| 129 | + return ret; |
| 130 | + } |
| 131 | +}); |
0 commit comments