diff --git a/test/unit-tests/stores/room-list-v3/RoomListStoreV3-test.ts b/test/unit-tests/stores/room-list-v3/RoomListStoreV3-test.ts new file mode 100644 index 0000000000..cd58c428e6 --- /dev/null +++ b/test/unit-tests/stores/room-list-v3/RoomListStoreV3-test.ts @@ -0,0 +1,38 @@ +/* +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 type { MatrixDispatcher } from "../../../../src/dispatcher/dispatcher"; +import { RoomListStoreV3Class } from "../../../../src/stores/room-list-v3/RoomListStoreV3"; +import { AsyncStoreWithClient } from "../../../../src/stores/AsyncStoreWithClient"; +import { RecencySorter } from "../../../../src/stores/room-list-v3/skip-list/sorters/RecencySorter"; +import { stubClient } from "../../../test-utils"; +import { getMockedRooms } from "./skip-list/getMockedRooms"; + +describe("RoomListStoreV3", () => { + async function getRoomListStore() { + const client = stubClient(); + const rooms = getMockedRooms(client); + client.getVisibleRooms = jest.fn().mockReturnValue(rooms); + jest.spyOn(AsyncStoreWithClient.prototype, "matrixClient", "get").mockReturnValue(client); + const fakeDispatcher = { register: jest.fn() } as unknown as MatrixDispatcher; + const store = new RoomListStoreV3Class(fakeDispatcher); + store.start(); + return { client, rooms, store }; + } + + it("Provides an unsorted list of rooms", async () => { + const { store, rooms } = await getRoomListStore(); + expect(store.getRooms()).toEqual(rooms); + }); + + it("Provides a sorted list of rooms", async () => { + const { store, rooms, client } = await getRoomListStore(); + const sorter = new RecencySorter(client.getSafeUserId()); + const sortedRooms = sorter.sort(rooms); + expect(store.getSortedRooms()).toEqual(sortedRooms); + }); +}); diff --git a/test/unit-tests/stores/room-list-v3/skip-list/RoomSkipList-test.ts b/test/unit-tests/stores/room-list-v3/skip-list/RoomSkipList-test.ts index 3172307a81..b644aa30e9 100644 --- a/test/unit-tests/stores/room-list-v3/skip-list/RoomSkipList-test.ts +++ b/test/unit-tests/stores/room-list-v3/skip-list/RoomSkipList-test.ts @@ -7,26 +7,15 @@ Please see LICENSE files in the repository root for full details. import { shuffle } from "lodash"; -import type { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; +import type { Room } from "matrix-js-sdk/src/matrix"; import type { Sorter } from "../../../../../src/stores/room-list-v3/skip-list/sorters"; -import { mkMessage, mkStubRoom, stubClient } from "../../../../test-utils"; +import { mkMessage, stubClient } from "../../../../test-utils"; import { RoomSkipList } from "../../../../../src/stores/room-list-v3/skip-list/RoomSkipList"; import { RecencySorter } from "../../../../../src/stores/room-list-v3/skip-list/sorters/RecencySorter"; import { AlphabeticSorter } from "../../../../../src/stores/room-list-v3/skip-list/sorters/AlphabeticSorter"; +import { getMockedRooms } from "./getMockedRooms"; describe("RoomSkipList", () => { - function getMockedRooms(client: MatrixClient, roomCount: number = 100): Room[] { - const rooms: Room[] = []; - for (let i = 0; i < roomCount; ++i) { - const roomId = `!foo${i}:matrix.org`; - const room = mkStubRoom(roomId, `Foo Room ${i}`, client); - const event = mkMessage({ room: roomId, user: `@foo${i}:matrix.org`, ts: i + 1, event: true }); - room.timeline.push(event); - rooms.push(room); - } - return rooms; - } - function generateSkipList(roomCount?: number): { skipList: RoomSkipList; rooms: Room[]; diff --git a/test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms.ts b/test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms.ts new file mode 100644 index 0000000000..d895ba944b --- /dev/null +++ b/test/unit-tests/stores/room-list-v3/skip-list/getMockedRooms.ts @@ -0,0 +1,21 @@ +/* +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 type { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; +import { mkMessage, mkStubRoom } from "../../../../test-utils"; + +export function getMockedRooms(client: MatrixClient, roomCount: number = 100): Room[] { + const rooms: Room[] = []; + for (let i = 0; i < roomCount; ++i) { + const roomId = `!foo${i}:matrix.org`; + const room = mkStubRoom(roomId, `Foo Room ${i}`, client); + const event = mkMessage({ room: roomId, user: `@foo${i}:matrix.org`, ts: i + 1, event: true }); + room.timeline.push(event); + rooms.push(room); + } + return rooms; +}