element-web/test/unit-tests/stores/MultiRoomViewStore-test.ts
David Baker 42f8247c2e
Experimental Module API Additions (#30863)
* Module API experiments

* Move ResizerNotifier into SDKContext

so we don't have to pass it into RoomView

* Add the MultiRoomViewStore

* Make RoomViewStore able to take a roomId prop

* Different interface to add space panel items

A bit less flexible but probably simpler and will help keep things
actually consistent rather than just allowing modules to stick any
JSX into the space panel (which means they also have to worry about
styling if they *do* want it to be consistent).

* Allow space panel items to be updated

and manage which one is selected, allowing module "spaces" to be
considered spaces

* Remove fetchRoomFn from SpaceNotificationStore

which didn't really seem to have any point as it was only called from
one place

* Switch to using module api via .instance

* Fairly awful workaround

to actually break the dependency nightmare

* Add test for multiroomviewstore

* add test

* Make room names deterministic

So the tests don't fail if you add other tests or run them individually

* Add test for builtinsapi

* Update module api

* RVS is not needed as prop anymore

Since it's passed through context

* Add roomId to prop

* Remove RoomViewStore from state

This is now accessed through class field

* Fix test

* No need to pass RVS from LoggedInView

* Add RoomContextType

* Implement new builtins api

* Add tests

* Fix import

* Fix circular dependency issue

* Fix import

* Add more tests

* Improve comment

* room-id is optional

* Update license

* Add implementation for AccountDataApi

* Add implementation for Room

* Add implementation for ClientApi

* Create ClientApi in Api.ts

* Write tests

* Use nullish coalescing assignment

* Implement openRoom in NavigationApi

* Write tests

* Add implementation for StoresApi

* Write tests

* Fix circular dependency

* Add comments in lieu of type

and fix else block

* Change to class field

---------

Co-authored-by: R Midhun Suresh <hi@midhun.dev>
2025-11-05 07:24:26 +00:00

102 lines
3.7 KiB
TypeScript

/*
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<RoomViewStore>;
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<typeof RoomViewStore>).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<typeof RoomViewStore>).mockImplementation(
() => mockRoomViewStore as any,
);
multiRoomViewStore.getRoomViewStoreForRoom(roomId);
expect(RoomViewStore).toHaveBeenCalledWith(mockDispatcher, mockSdkContext, roomId);
});
});
});