2021-07-27 19:15:40 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-07-27 19:15:40 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-07-27 19:15:40 +00:00
|
|
|
*/
|
2023-02-13 11:39:16 +00:00
|
|
|
|
2022-03-11 16:03:33 +00:00
|
|
|
import { mocked } from "jest-mock";
|
2023-08-04 07:36:16 +00:00
|
|
|
import { Room } from "matrix-js-sdk/src/matrix";
|
2021-07-27 19:15:40 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { SpaceWatcher } from "../../../../src/stores/room-list/SpaceWatcher";
|
|
|
|
import type { RoomListStoreClass } from "../../../../src/stores/room-list/RoomListStore";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";
|
|
|
|
import { MetaSpace, UPDATE_HOME_BEHAVIOUR } from "../../../../src/stores/spaces";
|
|
|
|
import { stubClient, mkSpace, emitPromise, setupAsyncStoreWithClient } from "../../../test-utils";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import { SpaceFilterCondition } from "../../../../src/stores/room-list/filters/SpaceFilterCondition";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2021-07-27 19:15:40 +00:00
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
let filter: SpaceFilterCondition | null = null;
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
const mockRoomListStore = {
|
2023-02-13 11:39:16 +00:00
|
|
|
addFilter: (f: SpaceFilterCondition) => (filter = f),
|
2023-02-16 17:21:44 +00:00
|
|
|
removeFilter: (): void => {
|
|
|
|
filter = null;
|
|
|
|
},
|
2021-07-27 19:15:40 +00:00
|
|
|
} as unknown as RoomListStoreClass;
|
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
const getUserIdForRoomId = jest.fn();
|
|
|
|
const getDMRoomsForUserId = jest.fn();
|
|
|
|
// @ts-ignore
|
|
|
|
DMRoomMap.sharedInstance = { getUserIdForRoomId, getDMRoomsForUserId };
|
|
|
|
|
|
|
|
const space1 = "!space1:server";
|
|
|
|
const space2 = "!space2:server";
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
describe("SpaceWatcher", () => {
|
|
|
|
stubClient();
|
|
|
|
const store = SpaceStore.instance;
|
2023-06-05 17:12:23 +00:00
|
|
|
const client = mocked(MatrixClientPeg.safeGet());
|
2021-07-27 19:15:40 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
let rooms: Room[] = [];
|
2022-02-23 11:21:11 +00:00
|
|
|
const mkSpaceForRooms = (spaceId: string, children: string[] = []) => mkSpace(client, spaceId, rooms, children);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
const setShowAllRooms = async (value: boolean) => {
|
|
|
|
if (store.allRoomsInHome === value) return;
|
2021-07-30 11:20:02 +00:00
|
|
|
await SettingsStore.setValue("Spaces.allRoomsInHome", null, SettingLevel.DEVICE, value);
|
2022-02-23 11:21:11 +00:00
|
|
|
await emitPromise(store, UPDATE_HOME_BEHAVIOUR);
|
2021-07-27 19:15:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
filter = null;
|
|
|
|
store.removeAllListeners();
|
2021-11-11 13:07:41 +00:00
|
|
|
store.setActiveSpace(MetaSpace.Home);
|
2021-07-27 19:15:40 +00:00
|
|
|
client.getVisibleRooms.mockReturnValue((rooms = []));
|
|
|
|
|
2022-02-23 11:21:11 +00:00
|
|
|
mkSpaceForRooms(space1);
|
|
|
|
mkSpaceForRooms(space2);
|
2021-11-11 13:07:41 +00:00
|
|
|
|
|
|
|
await SettingsStore.setValue("Spaces.enabledMetaSpaces", null, SettingLevel.DEVICE, {
|
|
|
|
[MetaSpace.Home]: true,
|
|
|
|
[MetaSpace.Favourites]: true,
|
|
|
|
[MetaSpace.People]: true,
|
|
|
|
[MetaSpace.Orphans]: true,
|
|
|
|
});
|
2021-07-27 19:15:40 +00:00
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
client.getRoom.mockImplementation((roomId) => rooms.find((room) => room.roomId === roomId) || null);
|
2021-07-27 19:15:40 +00:00
|
|
|
await setupAsyncStoreWithClient(store, client);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("initialises sanely with home behaviour", async () => {
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("initialises sanely with all behaviour", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
|
|
|
expect(filter).toBeNull();
|
|
|
|
});
|
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
it("sets space=Home filter for all -> home transition", async () => {
|
2021-07-27 19:15:40 +00:00
|
|
|
await setShowAllRooms(true);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.Home);
|
2021-07-27 19:15:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("sets filter correctly for all -> space transition", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("removes filter for home -> all transition", async () => {
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
|
|
|
|
expect(filter).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets filter correctly for home -> space transition", async () => {
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("removes filter for space -> all transition", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-11-11 13:07:41 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Home);
|
|
|
|
|
|
|
|
expect(filter).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes filter for favourites -> all transition", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Favourites);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.Favourites);
|
2021-11-11 13:07:41 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Home);
|
|
|
|
|
|
|
|
expect(filter).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes filter for people -> all transition", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.People);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.People);
|
2021-11-11 13:07:41 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Home);
|
|
|
|
|
|
|
|
expect(filter).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes filter for orphans -> all transition", async () => {
|
|
|
|
await setShowAllRooms(true);
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
|
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Orphans);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.Orphans);
|
2021-11-11 13:07:41 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Home);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
expect(filter).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates filter correctly for space -> home transition", async () => {
|
|
|
|
await setShowAllRooms(false);
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-11-11 13:07:41 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Home);
|
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.Home);
|
2021-11-11 13:07:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("updates filter correctly for space -> orphans transition", async () => {
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
|
|
|
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-11-11 13:07:41 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Orphans);
|
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.Orphans);
|
2021-11-11 13:07:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("updates filter correctly for orphans -> people transition", async () => {
|
|
|
|
await setShowAllRooms(false);
|
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.Orphans);
|
|
|
|
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.Orphans);
|
2021-11-11 13:07:41 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(MetaSpace.People);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(MetaSpace.People);
|
2021-07-27 19:15:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("updates filter correctly for space -> space transition", async () => {
|
|
|
|
await setShowAllRooms(false);
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space2);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space2);
|
2021-07-27 19:15:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't change filter when changing showAllRooms mode to true", async () => {
|
|
|
|
await setShowAllRooms(false);
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
await setShowAllRooms(true);
|
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't change filter when changing showAllRooms mode to false", async () => {
|
|
|
|
await setShowAllRooms(true);
|
2021-08-31 16:03:43 +00:00
|
|
|
SpaceStore.instance.setActiveSpace(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
|
|
|
|
new SpaceWatcher(mockRoomListStore);
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
await setShowAllRooms(false);
|
|
|
|
|
|
|
|
expect(filter).toBeInstanceOf(SpaceFilterCondition);
|
2023-02-16 17:21:44 +00:00
|
|
|
expect(filter!["space"]).toBe(space1);
|
2021-07-27 19:15:40 +00:00
|
|
|
});
|
|
|
|
});
|