Room List Store: Filter rooms by active space (#29399)
* Add method to await space store setup Otherwise, the room list store will get incorrect information about spaces and thus will produce an incorrect roomlist. * Implement a way to filter by active space Implement a way to filter by active space * Fix broken jest tests * Fix typo * Rename `isReady` to `storeReadyPromise` * Fix mock in test
This commit is contained in:
@@ -11,11 +11,13 @@ import { logger } from "matrix-js-sdk/src/logger";
|
||||
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 { mkEvent, mkMessage, stubClient, upsertRoomStateEvents } from "../../../test-utils";
|
||||
import { mkEvent, mkMessage, mkSpace, stubClient, upsertRoomStateEvents } from "../../../test-utils";
|
||||
import { getMockedRooms } from "./skip-list/getMockedRooms";
|
||||
import { AlphabeticSorter } from "../../../../src/stores/room-list-v3/skip-list/sorters/AlphabeticSorter";
|
||||
import { LISTS_UPDATE_EVENT } from "../../../../src/stores/room-list/RoomListStore";
|
||||
import dispatcher from "../../../../src/dispatcher/dispatcher";
|
||||
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
|
||||
import { MetaSpace, UPDATE_SELECTED_SPACE } from "../../../../src/stores/spaces";
|
||||
|
||||
describe("RoomListStoreV3", () => {
|
||||
async function getRoomListStore() {
|
||||
@@ -24,10 +26,16 @@ describe("RoomListStoreV3", () => {
|
||||
client.getVisibleRooms = jest.fn().mockReturnValue(rooms);
|
||||
jest.spyOn(AsyncStoreWithClient.prototype, "matrixClient", "get").mockReturnValue(client);
|
||||
const store = new RoomListStoreV3Class(dispatcher);
|
||||
store.start();
|
||||
await store.start();
|
||||
return { client, rooms, store, dispatcher };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(SpaceStore.instance, "isRoomInSpace").mockImplementation((space) => space === MetaSpace.Home);
|
||||
jest.spyOn(SpaceStore.instance, "activeSpace", "get").mockImplementation(() => MetaSpace.Home);
|
||||
jest.spyOn(SpaceStore.instance, "storeReadyPromise", "get").mockImplementation(() => Promise.resolve());
|
||||
});
|
||||
|
||||
it("Provides an unsorted list of rooms", async () => {
|
||||
const { store, rooms } = await getRoomListStore();
|
||||
expect(store.getRooms()).toEqual(rooms);
|
||||
@@ -264,5 +272,48 @@ describe("RoomListStoreV3", () => {
|
||||
expect(fn).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Spaces", () => {
|
||||
it("Filtering by spaces work", async () => {
|
||||
const client = stubClient();
|
||||
const rooms = getMockedRooms(client);
|
||||
|
||||
// Let's choose 5 rooms to put in space
|
||||
const indexes = [6, 8, 13, 27, 75];
|
||||
const roomIds = indexes.map((i) => rooms[i].roomId);
|
||||
const spaceRoom = mkSpace(client, "!space1:matrix.org", [], roomIds);
|
||||
rooms.push(spaceRoom);
|
||||
|
||||
client.getVisibleRooms = jest.fn().mockReturnValue(rooms);
|
||||
jest.spyOn(AsyncStoreWithClient.prototype, "matrixClient", "get").mockReturnValue(client);
|
||||
|
||||
// Mock the space store
|
||||
jest.spyOn(SpaceStore.instance, "isRoomInSpace").mockImplementation((space, id) => {
|
||||
if (space === MetaSpace.Home && !roomIds.includes(id)) return true;
|
||||
if (space === spaceRoom.roomId && roomIds.includes(id)) return true;
|
||||
return false;
|
||||
});
|
||||
|
||||
const store = new RoomListStoreV3Class(dispatcher);
|
||||
await store.start();
|
||||
const fn = jest.fn();
|
||||
store.on(LISTS_UPDATE_EVENT, fn);
|
||||
|
||||
// The rooms which belong to the space should not be shown
|
||||
const result = store.getSortedRoomsInActiveSpace().map((r) => r.roomId);
|
||||
for (const id of roomIds) {
|
||||
expect(result).not.toContain(id);
|
||||
}
|
||||
|
||||
// Lets switch to the space
|
||||
jest.spyOn(SpaceStore.instance, "activeSpace", "get").mockImplementation(() => spaceRoom.roomId);
|
||||
SpaceStore.instance.emit(UPDATE_SELECTED_SPACE);
|
||||
expect(fn).toHaveBeenCalled();
|
||||
const result2 = store.getSortedRoomsInActiveSpace().map((r) => r.roomId);
|
||||
for (const id of roomIds) {
|
||||
expect(result2).toContain(id);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,8 @@ import { RoomSkipList } from "../../../../../src/stores/room-list-v3/skip-list/R
|
||||
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";
|
||||
import SpaceStore from "../../../../../src/stores/spaces/SpaceStore";
|
||||
import { MetaSpace } from "../../../../../src/stores/spaces";
|
||||
|
||||
describe("RoomSkipList", () => {
|
||||
function generateSkipList(roomCount?: number): {
|
||||
@@ -30,6 +32,12 @@ describe("RoomSkipList", () => {
|
||||
return { skipList, rooms, totalRooms: rooms.length, sorter };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(SpaceStore.instance, "isRoomInSpace").mockImplementation((space) => space === MetaSpace.Home);
|
||||
jest.spyOn(SpaceStore.instance, "activeSpace", "get").mockImplementation(() => MetaSpace.Home);
|
||||
jest.spyOn(SpaceStore.instance, "storeReadyPromise", "get").mockImplementation(() => Promise.resolve());
|
||||
});
|
||||
|
||||
it("Rooms are in sorted order after initial seed", () => {
|
||||
const { skipList, totalRooms } = generateSkipList();
|
||||
expect(skipList.size).toEqual(totalRooms);
|
||||
|
||||
Reference in New Issue
Block a user