2022-05-09 15:14:45 +02:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2017-2022 The Matrix.org Foundation C.I.C.
|
2022-05-09 15:14:45 +02:00
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-05-09 15:14:45 +02:00
|
|
|
*/
|
|
|
|
|
2023-02-24 16:59:30 +01:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixError, Room } from "matrix-js-sdk/src/matrix";
|
2023-01-12 15:52:52 +01:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
2023-11-01 13:03:10 +01:00
|
|
|
import { RoomViewLifecycle, ViewRoomOpts } from "@matrix-org/react-sdk-module-api/lib/lifecycles/RoomViewLifecycle";
|
2022-12-12 12:24:14 +01:00
|
|
|
|
2024-10-15 15:57:26 +02:00
|
|
|
import { RoomViewStore } from "../../../src/stores/RoomViewStore";
|
|
|
|
import { Action } from "../../../src/dispatcher/actions";
|
|
|
|
import { getMockClientWithEventEmitter, untilDispatch, untilEmission } from "../../test-utils";
|
|
|
|
import SettingsStore from "../../../src/settings/SettingsStore";
|
|
|
|
import { SlidingSyncManager } from "../../../src/SlidingSyncManager";
|
|
|
|
import { PosthogAnalytics } from "../../../src/PosthogAnalytics";
|
|
|
|
import { TimelineRenderingType } from "../../../src/contexts/RoomContext";
|
|
|
|
import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher";
|
|
|
|
import { UPDATE_EVENT } from "../../../src/stores/AsyncStore";
|
|
|
|
import { ActiveRoomChangedPayload } from "../../../src/dispatcher/payloads/ActiveRoomChangedPayload";
|
|
|
|
import { SpaceStoreClass } from "../../../src/stores/spaces/SpaceStore";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { TestSdkContext } from "../TestSdkContext";
|
2024-10-15 15:57:26 +02:00
|
|
|
import { ViewRoomPayload } from "../../../src/dispatcher/payloads/ViewRoomPayload";
|
|
|
|
import Modal from "../../../src/Modal";
|
|
|
|
import ErrorDialog from "../../../src/components/views/dialogs/ErrorDialog";
|
|
|
|
import { CancelAskToJoinPayload } from "../../../src/dispatcher/payloads/CancelAskToJoinPayload";
|
|
|
|
import { JoinRoomErrorPayload } from "../../../src/dispatcher/payloads/JoinRoomErrorPayload";
|
|
|
|
import { SubmitAskToJoinPayload } from "../../../src/dispatcher/payloads/SubmitAskToJoinPayload";
|
|
|
|
import { ModuleRunner } from "../../../src/modules/ModuleRunner";
|
2022-12-19 09:44:19 +01:00
|
|
|
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../src/Modal");
|
2022-10-19 14:07:03 +02:00
|
|
|
|
|
|
|
// mock out the injected classes
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../src/PosthogAnalytics");
|
2022-12-12 12:24:14 +01:00
|
|
|
const MockPosthogAnalytics = <jest.Mock<PosthogAnalytics>>(<unknown>PosthogAnalytics);
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../src/SlidingSyncManager");
|
2022-12-12 12:24:14 +01:00
|
|
|
const MockSlidingSyncManager = <jest.Mock<SlidingSyncManager>>(<unknown>SlidingSyncManager);
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../src/stores/spaces/SpaceStore");
|
2022-12-12 12:24:14 +01:00
|
|
|
const MockSpaceStore = <jest.Mock<SpaceStoreClass>>(<unknown>SpaceStoreClass);
|
|
|
|
|
2022-12-19 09:44:19 +01:00
|
|
|
// mock VoiceRecording because it contains all the audio APIs
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../src/audio/VoiceRecording", () => ({
|
2022-12-19 09:44:19 +01:00
|
|
|
VoiceRecording: jest.fn().mockReturnValue({
|
|
|
|
disableMaxLength: jest.fn(),
|
|
|
|
liveData: {
|
|
|
|
onUpdate: jest.fn(),
|
|
|
|
},
|
|
|
|
off: jest.fn(),
|
|
|
|
on: jest.fn(),
|
|
|
|
start: jest.fn(),
|
|
|
|
stop: jest.fn(),
|
|
|
|
destroy: jest.fn(),
|
|
|
|
contentType: "audio/ogg",
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../src/utils/DMRoomMap", () => {
|
2022-05-09 15:14:45 +02:00
|
|
|
const mock = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
getDMRoomsForUserId: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
shared: jest.fn().mockReturnValue(mock),
|
|
|
|
sharedInstance: mock,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-10-15 15:57:26 +02:00
|
|
|
jest.mock("../../../src/stores/WidgetStore");
|
|
|
|
jest.mock("../../../src/stores/widgets/WidgetLayoutStore");
|
2024-01-22 11:53:27 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("RoomViewStore", function () {
|
|
|
|
const userId = "@alice:server";
|
2022-09-20 17:32:39 +02:00
|
|
|
const roomId = "!randomcharacters:aser.ver";
|
2023-01-02 12:05:51 +01:00
|
|
|
const roomId2 = "!room2:example.com";
|
2022-09-20 17:32:39 +02:00
|
|
|
// we need to change the alias to ensure cache misses as the cache exists
|
|
|
|
// through all tests.
|
|
|
|
let alias = "#somealias2:aser.ver";
|
2022-05-09 15:14:45 +02:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
joinRoom: jest.fn(),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getRoomIdForAlias: jest.fn(),
|
2022-09-07 17:42:39 +02:00
|
|
|
isGuest: jest.fn(),
|
2023-01-02 12:05:51 +01:00
|
|
|
getUserId: jest.fn().mockReturnValue(userId),
|
|
|
|
getSafeUserId: jest.fn().mockReturnValue(userId),
|
|
|
|
getDeviceId: jest.fn().mockReturnValue("ABC123"),
|
|
|
|
sendStateEvent: jest.fn().mockResolvedValue({}),
|
2023-01-30 13:20:11 +01:00
|
|
|
supportsThreads: jest.fn(),
|
2023-02-09 13:46:17 +01:00
|
|
|
isInitialSyncComplete: jest.fn().mockResolvedValue(false),
|
2023-02-09 12:36:02 +01:00
|
|
|
relations: jest.fn(),
|
2023-08-07 08:27:09 +02:00
|
|
|
knockRoom: jest.fn(),
|
|
|
|
leave: jest.fn(),
|
2024-03-19 14:28:20 +01:00
|
|
|
setRoomAccountData: jest.fn(),
|
2022-05-09 15:14:45 +02:00
|
|
|
});
|
2022-09-20 17:32:39 +02:00
|
|
|
const room = new Room(roomId, mockClient, userId);
|
2023-01-02 12:05:51 +01:00
|
|
|
const room2 = new Room(roomId2, mockClient, userId);
|
2022-10-19 14:07:03 +02:00
|
|
|
|
2022-12-19 09:44:19 +01:00
|
|
|
const viewCall = async (): Promise<void> => {
|
|
|
|
dis.dispatch<ViewRoomPayload>({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: roomId,
|
|
|
|
view_call: true,
|
|
|
|
metricsTrigger: undefined,
|
|
|
|
});
|
|
|
|
await untilDispatch(Action.ViewRoom, dis);
|
|
|
|
};
|
|
|
|
|
2023-08-07 08:27:09 +02:00
|
|
|
const dispatchPromptAskToJoin = async () => {
|
|
|
|
dis.dispatch({ action: Action.PromptAskToJoin });
|
|
|
|
await untilDispatch(Action.PromptAskToJoin, dis);
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchSubmitAskToJoin = async (roomId: string, reason?: string) => {
|
|
|
|
dis.dispatch<SubmitAskToJoinPayload>({ action: Action.SubmitAskToJoin, roomId, opts: { reason } });
|
|
|
|
await untilDispatch(Action.SubmitAskToJoin, dis);
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchCancelAskToJoin = async (roomId: string) => {
|
|
|
|
dis.dispatch<CancelAskToJoinPayload>({ action: Action.CancelAskToJoin, roomId });
|
|
|
|
await untilDispatch(Action.CancelAskToJoin, dis);
|
|
|
|
};
|
|
|
|
|
2024-02-27 13:02:16 +01:00
|
|
|
const dispatchRoomLoaded = async () => {
|
|
|
|
dis.dispatch({ action: Action.RoomLoaded });
|
|
|
|
await untilDispatch(Action.RoomLoaded, dis);
|
|
|
|
};
|
|
|
|
|
2022-10-19 14:07:03 +02:00
|
|
|
let roomViewStore: RoomViewStore;
|
|
|
|
let slidingSyncManager: SlidingSyncManager;
|
2022-09-20 17:32:39 +02:00
|
|
|
let dis: MatrixDispatcher;
|
2022-12-19 09:44:19 +01:00
|
|
|
let stores: TestSdkContext;
|
2022-05-09 15:14:45 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
beforeEach(function () {
|
2022-05-09 15:14:45 +02:00
|
|
|
jest.clearAllMocks();
|
2022-09-12 12:55:46 +02:00
|
|
|
mockClient.credentials = { userId: userId };
|
2022-05-09 15:14:45 +02:00
|
|
|
mockClient.joinRoom.mockResolvedValue(room);
|
2023-01-02 12:05:51 +01:00
|
|
|
mockClient.getRoom.mockImplementation((roomId: string): Room | null => {
|
|
|
|
if (roomId === room.roomId) return room;
|
|
|
|
if (roomId === room2.roomId) return room2;
|
|
|
|
return null;
|
|
|
|
});
|
2022-09-07 17:42:39 +02:00
|
|
|
mockClient.isGuest.mockReturnValue(false);
|
2022-12-19 09:44:19 +01:00
|
|
|
mockClient.getSafeUserId.mockReturnValue(userId);
|
2022-05-09 15:14:45 +02:00
|
|
|
|
2022-10-19 14:07:03 +02:00
|
|
|
// Make the RVS to test
|
2022-09-20 17:32:39 +02:00
|
|
|
dis = new MatrixDispatcher();
|
2022-10-19 14:07:03 +02:00
|
|
|
slidingSyncManager = new MockSlidingSyncManager();
|
2022-12-19 09:44:19 +01:00
|
|
|
stores = new TestSdkContext();
|
2023-01-02 12:05:51 +01:00
|
|
|
stores.client = mockClient;
|
2022-10-19 14:07:03 +02:00
|
|
|
stores._SlidingSyncManager = slidingSyncManager;
|
|
|
|
stores._PosthogAnalytics = new MockPosthogAnalytics();
|
|
|
|
stores._SpaceStore = new MockSpaceStore();
|
2022-12-12 12:24:14 +01:00
|
|
|
roomViewStore = new RoomViewStore(dis, stores);
|
2022-10-19 14:07:03 +02:00
|
|
|
stores._RoomViewStore = roomViewStore;
|
2022-05-09 15:14:45 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("can be used to view a room by ID and join", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
dis.dispatch({ action: Action.JoinRoom });
|
|
|
|
await untilDispatch(Action.JoinRoomReady, dis);
|
|
|
|
expect(mockClient.joinRoom).toHaveBeenCalledWith(roomId, { viaServers: [] });
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.isJoining()).toBe(true);
|
2022-05-09 15:14:45 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("can auto-join a room", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId, auto_join: true });
|
|
|
|
await untilDispatch(Action.JoinRoomReady, dis);
|
|
|
|
expect(mockClient.joinRoom).toHaveBeenCalledWith(roomId, { viaServers: [] });
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.isJoining()).toBe(true);
|
2022-09-20 17:32:39 +02:00
|
|
|
});
|
2022-05-09 15:14:45 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("emits ActiveRoomChanged when the viewed room changes", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
2022-12-12 12:24:14 +01:00
|
|
|
let payload = (await untilDispatch(Action.ActiveRoomChanged, dis)) as ActiveRoomChangedPayload;
|
2022-09-20 17:32:39 +02:00
|
|
|
expect(payload.newRoomId).toEqual(roomId);
|
|
|
|
expect(payload.oldRoomId).toEqual(null);
|
|
|
|
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId2 });
|
2022-12-12 12:24:14 +01:00
|
|
|
payload = (await untilDispatch(Action.ActiveRoomChanged, dis)) as ActiveRoomChangedPayload;
|
2022-09-20 17:32:39 +02:00
|
|
|
expect(payload.newRoomId).toEqual(roomId2);
|
|
|
|
expect(payload.oldRoomId).toEqual(roomId);
|
|
|
|
});
|
2022-05-09 15:14:45 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("invokes room activity listeners when the viewed room changes", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
const callback = jest.fn();
|
2022-10-19 14:07:03 +02:00
|
|
|
roomViewStore.addRoomListener(roomId, callback);
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
2022-12-12 12:24:14 +01:00
|
|
|
(await untilDispatch(Action.ActiveRoomChanged, dis)) as ActiveRoomChangedPayload;
|
2022-09-20 17:32:39 +02:00
|
|
|
expect(callback).toHaveBeenCalledWith(true);
|
|
|
|
expect(callback).not.toHaveBeenCalledWith(false);
|
|
|
|
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId2 });
|
2022-12-12 12:24:14 +01:00
|
|
|
(await untilDispatch(Action.ActiveRoomChanged, dis)) as ActiveRoomChangedPayload;
|
2022-09-20 17:32:39 +02:00
|
|
|
expect(callback).toHaveBeenCalledWith(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("can be used to view a room by alias and join", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
mockClient.getRoomIdForAlias.mockResolvedValue({ room_id: roomId, servers: [] });
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_alias: alias });
|
2022-12-12 12:24:14 +01:00
|
|
|
await untilDispatch((p) => {
|
|
|
|
// wait for the re-dispatch with the room ID
|
2022-09-20 17:32:39 +02:00
|
|
|
return p.action === Action.ViewRoom && p.room_id === roomId;
|
|
|
|
}, dis);
|
2022-05-09 15:14:45 +02:00
|
|
|
|
|
|
|
// roomId is set to id of the room alias
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.getRoomId()).toBe(roomId);
|
2022-05-09 15:14:45 +02:00
|
|
|
|
|
|
|
// join the room
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.JoinRoom }, true);
|
2022-05-09 15:14:45 +02:00
|
|
|
|
2022-09-20 17:32:39 +02:00
|
|
|
await untilDispatch(Action.JoinRoomReady, dis);
|
2022-05-09 15:14:45 +02:00
|
|
|
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.isJoining()).toBeTruthy();
|
2022-05-09 15:14:45 +02:00
|
|
|
expect(mockClient.joinRoom).toHaveBeenCalledWith(alias, { viaServers: [] });
|
|
|
|
});
|
2022-09-12 12:55:46 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("emits ViewRoomError if the alias lookup fails", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
alias = "#something-different:to-ensure-cache-miss";
|
|
|
|
mockClient.getRoomIdForAlias.mockRejectedValue(new Error("network error or something"));
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_alias: alias });
|
|
|
|
const payload = await untilDispatch(Action.ViewRoomError, dis);
|
|
|
|
expect(payload.room_id).toBeNull();
|
|
|
|
expect(payload.room_alias).toEqual(alias);
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.getRoomAlias()).toEqual(alias);
|
2022-09-20 17:32:39 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("emits JoinRoomError if joining the room fails", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
const joinErr = new Error("network error or something");
|
|
|
|
mockClient.joinRoom.mockRejectedValue(joinErr);
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
dis.dispatch({ action: Action.JoinRoom });
|
|
|
|
await untilDispatch(Action.JoinRoomError, dis);
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.isJoining()).toBe(false);
|
|
|
|
expect(roomViewStore.getJoinError()).toEqual(joinErr);
|
2022-09-20 17:32:39 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("remembers the event being replied to when swapping rooms", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
2022-09-15 14:09:16 +02:00
|
|
|
const replyToEvent = {
|
2022-09-20 17:32:39 +02:00
|
|
|
getRoomId: () => roomId,
|
2022-09-15 14:09:16 +02:00
|
|
|
};
|
2022-12-12 12:24:14 +01:00
|
|
|
dis.dispatch({ action: "reply_to_event", event: replyToEvent, context: TimelineRenderingType.Room });
|
2022-10-19 14:07:03 +02:00
|
|
|
await untilEmission(roomViewStore, UPDATE_EVENT);
|
|
|
|
expect(roomViewStore.getQuotingEvent()).toEqual(replyToEvent);
|
2022-09-15 14:09:16 +02:00
|
|
|
// view the same room, should remember the event.
|
2022-09-20 17:32:39 +02:00
|
|
|
// set the highlighed flag to make sure there is a state change so we get an update event
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId, highlighted: true });
|
2022-10-19 14:07:03 +02:00
|
|
|
await untilEmission(roomViewStore, UPDATE_EVENT);
|
|
|
|
expect(roomViewStore.getQuotingEvent()).toEqual(replyToEvent);
|
2022-09-15 14:09:16 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("swaps to the replied event room if it is not the current room", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
|
|
|
const replyToEvent = {
|
|
|
|
getRoomId: () => roomId2,
|
|
|
|
};
|
2022-12-12 12:24:14 +01:00
|
|
|
dis.dispatch({ action: "reply_to_event", event: replyToEvent, context: TimelineRenderingType.Room });
|
2022-09-20 17:32:39 +02:00
|
|
|
await untilDispatch(Action.ViewRoom, dis);
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.getQuotingEvent()).toEqual(replyToEvent);
|
|
|
|
expect(roomViewStore.getRoomId()).toEqual(roomId2);
|
2022-09-20 17:32:39 +02:00
|
|
|
});
|
|
|
|
|
2023-01-12 15:52:52 +01:00
|
|
|
it("should ignore reply_to_event for Thread panels", async () => {
|
|
|
|
expect(roomViewStore.getQuotingEvent()).toBeFalsy();
|
|
|
|
const replyToEvent = {
|
|
|
|
getRoomId: () => roomId2,
|
|
|
|
};
|
|
|
|
dis.dispatch({ action: "reply_to_event", event: replyToEvent, context: TimelineRenderingType.Thread });
|
|
|
|
await sleep(100);
|
|
|
|
expect(roomViewStore.getQuotingEvent()).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([TimelineRenderingType.Room, TimelineRenderingType.File, TimelineRenderingType.Notification])(
|
|
|
|
"Should respect reply_to_event for %s rendering context",
|
|
|
|
async (context) => {
|
|
|
|
const replyToEvent = {
|
|
|
|
getRoomId: () => roomId,
|
|
|
|
};
|
|
|
|
dis.dispatch({ action: "reply_to_event", event: replyToEvent, context });
|
|
|
|
await untilDispatch(Action.ViewRoom, dis);
|
|
|
|
expect(roomViewStore.getQuotingEvent()).toEqual(replyToEvent);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("removes the roomId on ViewHomePage", async () => {
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.getRoomId()).toEqual(roomId);
|
2022-09-20 17:32:39 +02:00
|
|
|
|
|
|
|
dis.dispatch({ action: Action.ViewHomePage });
|
2022-10-19 14:07:03 +02:00
|
|
|
await untilEmission(roomViewStore, UPDATE_EVENT);
|
|
|
|
expect(roomViewStore.getRoomId()).toBeNull();
|
2022-09-20 17:32:39 +02:00
|
|
|
});
|
|
|
|
|
2022-12-19 09:44:19 +01:00
|
|
|
it("when viewing a call without a broadcast, it should not raise an error", async () => {
|
|
|
|
await viewCall();
|
|
|
|
});
|
|
|
|
|
2023-02-24 16:59:30 +01:00
|
|
|
it("should display an error message when the room is unreachable via the roomId", async () => {
|
|
|
|
// When
|
|
|
|
// View and wait for the room
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
|
|
|
// Generate error to display the expected error message
|
|
|
|
const error = new MatrixError(undefined, 404);
|
|
|
|
roomViewStore.showJoinRoomError(error, roomId);
|
|
|
|
|
|
|
|
// Check the modal props
|
|
|
|
expect(mocked(Modal).createDialog.mock.calls[0][1]).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should display the generic error message when the roomId doesnt match", async () => {
|
|
|
|
// When
|
|
|
|
// Generate error to display the expected error message
|
|
|
|
const error = new MatrixError({ error: "my 404 error" }, 404);
|
|
|
|
roomViewStore.showJoinRoomError(error, roomId);
|
|
|
|
|
|
|
|
// Check the modal props
|
|
|
|
expect(mocked(Modal).createDialog.mock.calls[0][1]).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2024-03-19 14:28:20 +01:00
|
|
|
it("clears the unread flag when viewing a room", async () => {
|
|
|
|
room.getAccountData = jest.fn().mockReturnValue({
|
|
|
|
getContent: jest.fn().mockReturnValue({ unread: true }),
|
|
|
|
});
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: roomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
2024-11-18 11:25:36 +01:00
|
|
|
expect(mockClient.setRoomAccountData).toHaveBeenCalledWith(roomId, "m.marked_unread", {
|
2024-03-19 14:28:20 +01:00
|
|
|
unread: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Sliding Sync", function () {
|
2022-09-12 12:55:46 +02:00
|
|
|
beforeEach(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName, roomId, value) => {
|
2022-09-12 12:55:46 +02:00
|
|
|
return settingName === "feature_sliding_sync"; // this is enabled, everything else is disabled.
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("subscribes to the room", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const setRoomVisible = jest
|
|
|
|
.spyOn(slidingSyncManager, "setRoomVisible")
|
|
|
|
.mockReturnValue(Promise.resolve(""));
|
2022-09-12 12:55:46 +02:00
|
|
|
const subscribedRoomId = "!sub1:localhost";
|
2022-09-20 17:32:39 +02:00
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: subscribedRoomId });
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
2022-10-19 14:07:03 +02:00
|
|
|
expect(roomViewStore.getRoomId()).toBe(subscribedRoomId);
|
2022-09-12 12:55:46 +02:00
|
|
|
expect(setRoomVisible).toHaveBeenCalledWith(subscribedRoomId, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Regression test for an in-the-wild bug where rooms would rapidly switch forever in sliding sync mode
|
|
|
|
it("doesn't get stuck in a loop if you view rooms quickly", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const setRoomVisible = jest
|
|
|
|
.spyOn(slidingSyncManager, "setRoomVisible")
|
|
|
|
.mockReturnValue(Promise.resolve(""));
|
2022-09-20 17:32:39 +02:00
|
|
|
const subscribedRoomId = "!sub1:localhost";
|
|
|
|
const subscribedRoomId2 = "!sub2:localhost";
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: subscribedRoomId }, true);
|
|
|
|
dis.dispatch({ action: Action.ViewRoom, room_id: subscribedRoomId2 }, true);
|
|
|
|
await untilDispatch(Action.ActiveRoomChanged, dis);
|
|
|
|
// sub(1) then unsub(1) sub(2), unsub(1)
|
|
|
|
const wantCalls = [
|
|
|
|
[subscribedRoomId, true],
|
|
|
|
[subscribedRoomId, false],
|
|
|
|
[subscribedRoomId2, true],
|
|
|
|
[subscribedRoomId, false],
|
|
|
|
];
|
|
|
|
expect(setRoomVisible).toHaveBeenCalledTimes(wantCalls.length);
|
|
|
|
wantCalls.forEach((v, i) => {
|
|
|
|
try {
|
|
|
|
expect(setRoomVisible.mock.calls[i][0]).toEqual(v[0]);
|
|
|
|
expect(setRoomVisible.mock.calls[i][1]).toEqual(v[1]);
|
2024-10-16 17:43:07 +02:00
|
|
|
} catch {
|
2022-09-20 17:32:39 +02:00
|
|
|
throw new Error(`i=${i} got ${setRoomVisible.mock.calls[i]} want ${v}`);
|
|
|
|
}
|
|
|
|
});
|
2022-09-12 12:55:46 +02:00
|
|
|
});
|
|
|
|
});
|
2023-08-07 08:27:09 +02:00
|
|
|
|
|
|
|
describe("Action.JoinRoom", () => {
|
|
|
|
it("dispatches Action.JoinRoomError and Action.AskToJoin when the join fails", async () => {
|
|
|
|
const err = new MatrixError();
|
|
|
|
|
|
|
|
jest.spyOn(dis, "dispatch");
|
|
|
|
jest.spyOn(mockClient, "joinRoom").mockRejectedValueOnce(err);
|
|
|
|
|
|
|
|
dis.dispatch({ action: Action.JoinRoom, canAskToJoin: true });
|
|
|
|
await untilDispatch(Action.PromptAskToJoin, dis);
|
|
|
|
|
|
|
|
expect(mocked(dis.dispatch).mock.calls[0][0]).toEqual({ action: "join_room", canAskToJoin: true });
|
|
|
|
expect(mocked(dis.dispatch).mock.calls[1][0]).toEqual({
|
|
|
|
action: "join_room_error",
|
|
|
|
roomId: null,
|
|
|
|
err,
|
|
|
|
canAskToJoin: true,
|
|
|
|
});
|
|
|
|
expect(mocked(dis.dispatch).mock.calls[2][0]).toEqual({ action: "prompt_ask_to_join" });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Action.JoinRoomError", () => {
|
|
|
|
const err = new MatrixError();
|
|
|
|
beforeEach(() => jest.spyOn(roomViewStore, "showJoinRoomError"));
|
|
|
|
|
|
|
|
it("calls showJoinRoomError()", async () => {
|
|
|
|
dis.dispatch<JoinRoomErrorPayload>({ action: Action.JoinRoomError, roomId, err });
|
|
|
|
await untilDispatch(Action.JoinRoomError, dis);
|
|
|
|
expect(roomViewStore.showJoinRoomError).toHaveBeenCalledWith(err, roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not call showJoinRoomError() when canAskToJoin is true", async () => {
|
|
|
|
dis.dispatch<JoinRoomErrorPayload>({ action: Action.JoinRoomError, roomId, err, canAskToJoin: true });
|
|
|
|
await untilDispatch(Action.JoinRoomError, dis);
|
|
|
|
expect(roomViewStore.showJoinRoomError).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("askToJoin()", () => {
|
|
|
|
it("returns false", () => {
|
|
|
|
expect(roomViewStore.promptAskToJoin()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns true", async () => {
|
|
|
|
await dispatchPromptAskToJoin();
|
|
|
|
expect(roomViewStore.promptAskToJoin()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Action.SubmitAskToJoin", () => {
|
|
|
|
const reason = "some reason";
|
|
|
|
beforeEach(async () => await dispatchPromptAskToJoin());
|
|
|
|
|
2023-09-11 18:33:40 +02:00
|
|
|
it("calls knockRoom() and sets promptAskToJoin state to false", async () => {
|
2023-08-07 08:27:09 +02:00
|
|
|
jest.spyOn(mockClient, "knockRoom").mockResolvedValue({ room_id: roomId });
|
|
|
|
await dispatchSubmitAskToJoin(roomId, reason);
|
|
|
|
|
|
|
|
expect(mockClient.knockRoom).toHaveBeenCalledWith(roomId, { reason, viaServers: [] });
|
|
|
|
expect(roomViewStore.promptAskToJoin()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2023-09-11 18:33:40 +02:00
|
|
|
it("calls knockRoom(), sets promptAskToJoin state to false and shows an error dialog", async () => {
|
2023-08-07 08:27:09 +02:00
|
|
|
const error = new MatrixError(undefined, 403);
|
|
|
|
jest.spyOn(mockClient, "knockRoom").mockRejectedValue(error);
|
|
|
|
await dispatchSubmitAskToJoin(roomId, reason);
|
|
|
|
|
|
|
|
expect(mockClient.knockRoom).toHaveBeenCalledWith(roomId, { reason, viaServers: [] });
|
|
|
|
expect(roomViewStore.promptAskToJoin()).toBe(false);
|
|
|
|
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, {
|
|
|
|
description: "You need an invite to access this room.",
|
|
|
|
title: "Failed to join",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows an error dialog with a generic error message", async () => {
|
|
|
|
const error = new MatrixError();
|
|
|
|
jest.spyOn(mockClient, "knockRoom").mockRejectedValue(error);
|
|
|
|
await dispatchSubmitAskToJoin(roomId);
|
2023-09-11 18:33:40 +02:00
|
|
|
|
2023-08-07 08:27:09 +02:00
|
|
|
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, {
|
|
|
|
description: error.message,
|
|
|
|
title: "Failed to join",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Action.CancelAskToJoin", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
jest.spyOn(mockClient, "knockRoom").mockResolvedValue({ room_id: roomId });
|
|
|
|
await dispatchSubmitAskToJoin(roomId);
|
|
|
|
});
|
|
|
|
|
2023-09-11 18:33:40 +02:00
|
|
|
it("calls leave()", async () => {
|
2023-08-07 08:27:09 +02:00
|
|
|
jest.spyOn(mockClient, "leave").mockResolvedValue({});
|
|
|
|
await dispatchCancelAskToJoin(roomId);
|
|
|
|
|
|
|
|
expect(mockClient.leave).toHaveBeenCalledWith(roomId);
|
|
|
|
});
|
|
|
|
|
2023-09-11 18:33:40 +02:00
|
|
|
it("calls leave() and shows an error dialog", async () => {
|
2023-08-07 08:27:09 +02:00
|
|
|
const error = new MatrixError();
|
|
|
|
jest.spyOn(mockClient, "leave").mockRejectedValue(error);
|
|
|
|
await dispatchCancelAskToJoin(roomId);
|
|
|
|
|
|
|
|
expect(mockClient.leave).toHaveBeenCalledWith(roomId);
|
|
|
|
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, {
|
|
|
|
description: error.message,
|
|
|
|
title: "Failed to cancel",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-11-01 13:03:10 +01:00
|
|
|
|
|
|
|
describe("getViewRoomOpts", () => {
|
|
|
|
it("returns viewRoomOpts", () => {
|
|
|
|
expect(roomViewStore.getViewRoomOpts()).toEqual({ buttons: [] });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Action.RoomLoaded", () => {
|
|
|
|
it("updates viewRoomOpts", async () => {
|
|
|
|
const buttons: ViewRoomOpts["buttons"] = [
|
|
|
|
{
|
|
|
|
icon: "test-icon",
|
|
|
|
id: "test-id",
|
|
|
|
label: () => "test-label",
|
|
|
|
onClick: () => {},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
jest.spyOn(ModuleRunner.instance, "invoke").mockImplementation((lifecycleEvent, opts) => {
|
|
|
|
if (lifecycleEvent === RoomViewLifecycle.ViewRoom) {
|
|
|
|
opts.buttons = buttons;
|
|
|
|
}
|
|
|
|
});
|
2024-02-27 13:02:16 +01:00
|
|
|
await dispatchRoomLoaded();
|
2023-11-01 13:03:10 +01:00
|
|
|
expect(roomViewStore.getViewRoomOpts()).toEqual({ buttons });
|
|
|
|
});
|
|
|
|
});
|
2022-05-09 15:14:45 +02:00
|
|
|
});
|