From 1ebead1c8ab947ce7b01c1400bae707823c2eb7f Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 17 Oct 2025 16:07:02 +0100 Subject: [PATCH] Add test for multiroomviewstore --- src/stores/MultiRoomViewStore.ts | 7 +- .../stores/MultiRoomViewStore-test.ts | 101 ++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 test/unit-tests/stores/MultiRoomViewStore-test.ts diff --git a/src/stores/MultiRoomViewStore.ts b/src/stores/MultiRoomViewStore.ts index ed763dcdf7..bc2f090e6f 100644 --- a/src/stores/MultiRoomViewStore.ts +++ b/src/stores/MultiRoomViewStore.ts @@ -4,6 +4,7 @@ Copyright 2025 New Vector Ltd. SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE files in the repository root for full details. */ + import { logger } from "matrix-js-sdk/src/logger"; import { RoomViewStore } from "./RoomViewStore"; @@ -11,6 +12,10 @@ import { type MatrixDispatcher } from "../dispatcher/dispatcher"; import { type SdkContextClass } from "../contexts/SDKContext"; import { Action } from "../dispatcher/actions"; +/** + * Acts as a cache of many RoomViewStore instances, creating them as necessary + * given a room ID. + */ export class MultiRoomViewStore { /** * Map from room-id to RVS instance. @@ -26,7 +31,6 @@ export class MultiRoomViewStore { * Get a RVS instance for the room identified by the given roomId. */ public getRoomViewStoreForRoom(roomId: string): RoomViewStore { - console.log(`Create RVS: ${roomId}`); // Get existing store / create new store const store = this.stores.has(roomId) ? this.stores.get(roomId)! @@ -49,7 +53,6 @@ export class MultiRoomViewStore { * Remove a RVS instance that was created by {@link getRoomViewStoreForRoom}. */ public removeRoomViewStore(roomId: string): void { - console.log(`Remove RVS: ${roomId}`); const didRemove = this.stores.delete(roomId); if (!didRemove) { logger.warn(`removeRoomViewStore called with ${roomId} but no store exists for this room.`); diff --git a/test/unit-tests/stores/MultiRoomViewStore-test.ts b/test/unit-tests/stores/MultiRoomViewStore-test.ts new file mode 100644 index 0000000000..429ec3c08a --- /dev/null +++ b/test/unit-tests/stores/MultiRoomViewStore-test.ts @@ -0,0 +1,101 @@ +/* +Copyright 2025 New Vector Ltd. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import { MultiRoomViewStore } from "../../../src/stores/MultiRoomViewStore"; +import { RoomViewStore } from "../../../src/stores/RoomViewStore"; +import { Action } from "../../../src/dispatcher/actions"; +import type { MatrixDispatcher } from "../../../src/dispatcher/dispatcher"; +import { TestSdkContext } from "../TestSdkContext"; + +jest.mock("../../../src/stores/RoomViewStore"); + +describe("MultiRoomViewStore", () => { + let multiRoomViewStore: MultiRoomViewStore; + let mockDispatcher: MatrixDispatcher; + let mockSdkContext: TestSdkContext; + let mockRoomViewStore: jest.Mocked; + + beforeEach(() => { + jest.clearAllMocks(); + + // Create mock dispatcher + mockDispatcher = { + dispatch: jest.fn(), + register: jest.fn(), + unregister: jest.fn(), + } as unknown as MatrixDispatcher; + + // Create mock SDK context + mockSdkContext = new TestSdkContext(); + + // Create mock RoomViewStore instance + mockRoomViewStore = { + viewRoom: jest.fn(), + dispose: jest.fn(), + } as any; + + (RoomViewStore as jest.MockedClass).mockImplementation(() => mockRoomViewStore as any); + + // Create the MultiRoomViewStore instance + multiRoomViewStore = new MultiRoomViewStore(mockDispatcher, mockSdkContext); + }); + + describe("getRoomViewStoreForRoom", () => { + it("should create a new RoomViewStore for a room that doesn't exist in cache", () => { + const roomId = "!room1:example.com"; + + const result = multiRoomViewStore.getRoomViewStoreForRoom(roomId); + + expect(RoomViewStore).toHaveBeenCalledWith(mockDispatcher, mockSdkContext, roomId); + expect(mockRoomViewStore.viewRoom).toHaveBeenCalledWith({ + action: Action.ViewRoom, + room_id: roomId, + metricsTrigger: undefined, + }); + expect(result).toBe(mockRoomViewStore); + }); + + it("should return existing RoomViewStore for a room that exists in cache", () => { + const roomId = "!room1:example.com"; + + // First call creates the store + const firstResult = multiRoomViewStore.getRoomViewStoreForRoom(roomId); + + jest.clearAllMocks(); + + // Should return the same store + const secondResult = multiRoomViewStore.getRoomViewStoreForRoom(roomId); + + expect(RoomViewStore).not.toHaveBeenCalled(); + expect(mockRoomViewStore.viewRoom).toHaveBeenCalledWith({ + action: Action.ViewRoom, + room_id: roomId, + metricsTrigger: undefined, + }); + expect(secondResult).toBe(firstResult); + expect(secondResult).toBe(mockRoomViewStore); + }); + }); + + describe("removeRoomViewStore", () => { + it("should remove an existing RoomViewStore from cache", () => { + const roomId = "!room1:example.com"; + + multiRoomViewStore.getRoomViewStoreForRoom(roomId); + multiRoomViewStore.removeRoomViewStore(roomId); + + // New store should be created now + jest.clearAllMocks(); + (RoomViewStore as jest.MockedClass).mockImplementation( + () => mockRoomViewStore as any, + ); + + multiRoomViewStore.getRoomViewStoreForRoom(roomId); + expect(RoomViewStore).toHaveBeenCalledWith(mockDispatcher, mockSdkContext, roomId); + }); + }); +});