2022-04-08 17:32:35 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-08 17:32:35 +00:00
|
|
|
Copyright 2022 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.
|
2022-04-08 17:32:35 +00:00
|
|
|
*/
|
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
import React, { createRef, RefObject } from "react";
|
2022-06-10 12:59:07 +00:00
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
2023-08-07 08:24:58 +00:00
|
|
|
import {
|
2023-08-09 07:18:41 +00:00
|
|
|
ClientEvent,
|
|
|
|
MatrixClient,
|
2023-08-07 08:24:58 +00:00
|
|
|
Room,
|
|
|
|
RoomEvent,
|
|
|
|
EventType,
|
|
|
|
JoinRule,
|
|
|
|
MatrixError,
|
|
|
|
RoomStateEvent,
|
|
|
|
MatrixEvent,
|
2023-12-19 16:01:17 +00:00
|
|
|
SearchResult,
|
|
|
|
IEvent,
|
2023-08-07 08:24:58 +00:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-11-19 10:09:25 +00:00
|
|
|
import { CryptoApi, UserVerificationStatus } from "matrix-js-sdk/src/crypto-api";
|
2024-03-18 14:40:52 +00:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { fireEvent, render, screen, RenderResult, waitForElementToBeRemoved, waitFor } from "jest-matrix-react";
|
2023-12-19 16:01:17 +00:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-12-08 21:37:25 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
stubClient,
|
|
|
|
mockPlatformPeg,
|
|
|
|
unmockPlatformPeg,
|
|
|
|
wrapInMatrixClientContext,
|
|
|
|
flushPromises,
|
2023-02-22 12:59:42 +00:00
|
|
|
mkEvent,
|
|
|
|
setupAsyncStoreWithClient,
|
|
|
|
filterConsole,
|
2023-03-06 11:08:04 +00:00
|
|
|
mkRoomMemberJoinEvent,
|
|
|
|
mkThirdPartyInviteEvent,
|
2023-06-19 16:08:16 +00:00
|
|
|
emitPromise,
|
2023-08-07 06:27:09 +00:00
|
|
|
createTestClient,
|
|
|
|
untilDispatch,
|
2024-10-15 13:57:26 +00:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
2024-11-05 19:12:43 +00:00
|
|
|
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
2024-10-15 13:57:26 +00:00
|
|
|
import { ViewRoomPayload } from "../../../../src/dispatcher/payloads/ViewRoomPayload";
|
|
|
|
import { RoomView as _RoomView } from "../../../../src/components/structures/RoomView";
|
|
|
|
import ResizeNotifier from "../../../../src/utils/ResizeNotifier";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
|
|
|
import { NotificationState } from "../../../../src/stores/notifications/NotificationState";
|
|
|
|
import { RightPanelPhases } from "../../../../src/stores/right-panel/RightPanelStorePhases";
|
|
|
|
import { LocalRoom, LocalRoomState } from "../../../../src/models/LocalRoom";
|
|
|
|
import { DirectoryMember } from "../../../../src/utils/direct-messages";
|
|
|
|
import { createDmLocalRoom } from "../../../../src/utils/dm/createDmLocalRoom";
|
|
|
|
import { UPDATE_EVENT } from "../../../../src/stores/AsyncStore";
|
|
|
|
import { SDKContext, SdkContextClass } from "../../../../src/contexts/SDKContext";
|
|
|
|
import VoipUserMapper from "../../../../src/VoipUserMapper";
|
|
|
|
import WidgetUtils from "../../../../src/utils/WidgetUtils";
|
|
|
|
import { WidgetType } from "../../../../src/widgets/WidgetType";
|
|
|
|
import WidgetStore from "../../../../src/stores/WidgetStore";
|
|
|
|
import { ViewRoomErrorPayload } from "../../../../src/dispatcher/payloads/ViewRoomErrorPayload";
|
|
|
|
import { SearchScope } from "../../../../src/Searching";
|
|
|
|
import { MEGOLM_ENCRYPTION_ALGORITHM } from "../../../../src/utils/crypto";
|
2022-04-08 17:32:35 +00:00
|
|
|
|
2022-06-10 12:59:07 +00:00
|
|
|
const RoomView = wrapInMatrixClientContext(_RoomView);
|
|
|
|
|
2022-04-08 17:32:35 +00:00
|
|
|
describe("RoomView", () => {
|
2022-06-10 12:59:07 +00:00
|
|
|
let cli: MockedObject<MatrixClient>;
|
|
|
|
let room: Room;
|
2023-02-10 07:40:38 +00:00
|
|
|
let rooms: Map<string, Room>;
|
2022-06-17 20:57:40 +00:00
|
|
|
let roomCount = 0;
|
2022-10-19 12:07:03 +00:00
|
|
|
let stores: SdkContextClass;
|
2024-11-19 10:09:25 +00:00
|
|
|
let crypto: CryptoApi;
|
2022-08-04 06:19:52 +00:00
|
|
|
|
2023-02-22 12:59:42 +00:00
|
|
|
// mute some noise
|
|
|
|
filterConsole("RVS update", "does not have an m.room.create event", "Current version: 1", "Version capability");
|
|
|
|
|
2023-03-06 11:08:04 +00:00
|
|
|
beforeEach(() => {
|
2022-06-17 20:57:40 +00:00
|
|
|
mockPlatformPeg({ reload: () => {} });
|
2022-04-08 17:32:35 +00:00
|
|
|
stubClient();
|
2023-06-05 17:12:23 +00:00
|
|
|
cli = mocked(MatrixClientPeg.safeGet());
|
2022-04-08 17:32:35 +00:00
|
|
|
|
2022-06-17 20:57:40 +00:00
|
|
|
room = new Room(`!${roomCount++}:example.org`, cli, "@alice:example.org");
|
2023-02-10 07:40:38 +00:00
|
|
|
jest.spyOn(room, "findPredecessor");
|
2022-06-10 12:59:07 +00:00
|
|
|
room.getPendingEvents = () => [];
|
2023-02-10 07:40:38 +00:00
|
|
|
rooms = new Map();
|
|
|
|
rooms.set(room.roomId, room);
|
|
|
|
cli.getRoom.mockImplementation((roomId: string | undefined) => rooms.get(roomId || "") || null);
|
2022-06-10 12:59:07 +00:00
|
|
|
// Re-emit certain events on the mocked client
|
|
|
|
room.on(RoomEvent.Timeline, (...args) => cli.emit(RoomEvent.Timeline, ...args));
|
|
|
|
room.on(RoomEvent.TimelineReset, (...args) => cli.emit(RoomEvent.TimelineReset, ...args));
|
2022-04-08 17:32:35 +00:00
|
|
|
|
2023-05-23 15:24:12 +00:00
|
|
|
DMRoomMap.makeShared(cli);
|
2022-10-19 12:07:03 +00:00
|
|
|
stores = new SdkContextClass();
|
|
|
|
stores.client = cli;
|
|
|
|
stores.rightPanelStore.useUnitTestClient(cli);
|
2022-12-08 21:37:25 +00:00
|
|
|
|
2023-02-16 09:38:44 +00:00
|
|
|
jest.spyOn(VoipUserMapper.sharedInstance(), "getVirtualRoomForRoom").mockResolvedValue(undefined);
|
2024-11-19 10:09:25 +00:00
|
|
|
crypto = cli.getCrypto()!;
|
2024-10-15 14:24:58 +00:00
|
|
|
jest.spyOn(cli, "getCrypto").mockReturnValue(undefined);
|
2022-06-17 20:57:40 +00:00
|
|
|
});
|
|
|
|
|
2023-03-06 11:08:04 +00:00
|
|
|
afterEach(() => {
|
2022-06-17 20:57:40 +00:00
|
|
|
unmockPlatformPeg();
|
2023-08-16 10:20:48 +00:00
|
|
|
jest.clearAllMocks();
|
2022-06-10 12:59:07 +00:00
|
|
|
});
|
2022-04-08 17:32:35 +00:00
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
const mountRoomView = async (ref?: RefObject<_RoomView>): Promise<RenderResult> => {
|
2022-10-19 12:07:03 +00:00
|
|
|
if (stores.roomViewStore.getRoomId() !== room.roomId) {
|
2022-06-17 20:57:40 +00:00
|
|
|
const switchedRoom = new Promise<void>((resolve) => {
|
2022-09-20 15:32:39 +00:00
|
|
|
const subFn = () => {
|
2022-10-19 12:07:03 +00:00
|
|
|
if (stores.roomViewStore.getRoomId()) {
|
|
|
|
stores.roomViewStore.off(UPDATE_EVENT, subFn);
|
2022-06-10 12:59:07 +00:00
|
|
|
resolve();
|
|
|
|
}
|
2022-09-20 15:32:39 +00:00
|
|
|
};
|
2022-10-19 12:07:03 +00:00
|
|
|
stores.roomViewStore.on(UPDATE_EVENT, subFn);
|
2022-04-08 17:32:35 +00:00
|
|
|
});
|
|
|
|
|
2022-08-04 06:19:52 +00:00
|
|
|
defaultDispatcher.dispatch<ViewRoomPayload>({
|
2022-06-10 12:59:07 +00:00
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
2022-12-08 21:37:25 +00:00
|
|
|
metricsTrigger: undefined,
|
2022-06-10 12:59:07 +00:00
|
|
|
});
|
2022-04-08 17:32:35 +00:00
|
|
|
|
2022-06-17 20:57:40 +00:00
|
|
|
await switchedRoom;
|
2022-06-10 12:59:07 +00:00
|
|
|
}
|
2022-04-08 17:32:35 +00:00
|
|
|
|
2023-03-06 12:13:17 +00:00
|
|
|
const roomView = render(
|
2022-10-19 12:07:03 +00:00
|
|
|
<SDKContext.Provider value={stores}>
|
|
|
|
<RoomView
|
2022-12-08 21:37:25 +00:00
|
|
|
// threepidInvite should be optional on RoomView props
|
|
|
|
// it is treated as optional in RoomView
|
|
|
|
threepidInvite={undefined as any}
|
2022-10-19 12:07:03 +00:00
|
|
|
resizeNotifier={new ResizeNotifier()}
|
|
|
|
forceTimeline={false}
|
2023-03-06 12:13:17 +00:00
|
|
|
wrappedRef={ref as any}
|
2022-10-19 12:07:03 +00:00
|
|
|
/>
|
|
|
|
</SDKContext.Provider>,
|
2022-06-10 12:59:07 +00:00
|
|
|
);
|
2022-12-08 21:37:25 +00:00
|
|
|
await flushPromises();
|
|
|
|
return roomView;
|
|
|
|
};
|
|
|
|
|
2023-06-19 16:08:16 +00:00
|
|
|
const renderRoomView = async (switchRoom = true): Promise<ReturnType<typeof render>> => {
|
|
|
|
if (switchRoom && stores.roomViewStore.getRoomId() !== room.roomId) {
|
2022-12-08 21:37:25 +00:00
|
|
|
const switchedRoom = new Promise<void>((resolve) => {
|
|
|
|
const subFn = () => {
|
|
|
|
if (stores.roomViewStore.getRoomId()) {
|
|
|
|
stores.roomViewStore.off(UPDATE_EVENT, subFn);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
stores.roomViewStore.on(UPDATE_EVENT, subFn);
|
|
|
|
});
|
|
|
|
|
|
|
|
defaultDispatcher.dispatch<ViewRoomPayload>({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
metricsTrigger: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
await switchedRoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
const roomView = render(
|
|
|
|
<SDKContext.Provider value={stores}>
|
|
|
|
<RoomView
|
|
|
|
// threepidInvite should be optional on RoomView props
|
|
|
|
// it is treated as optional in RoomView
|
|
|
|
threepidInvite={undefined as any}
|
|
|
|
resizeNotifier={new ResizeNotifier()}
|
|
|
|
forceTimeline={false}
|
|
|
|
onRegistered={jest.fn()}
|
|
|
|
/>
|
|
|
|
</SDKContext.Provider>,
|
|
|
|
);
|
|
|
|
await flushPromises();
|
2022-06-17 20:57:40 +00:00
|
|
|
return roomView;
|
2022-06-10 12:59:07 +00:00
|
|
|
};
|
2023-03-06 12:13:17 +00:00
|
|
|
const getRoomViewInstance = async (): Promise<_RoomView> => {
|
|
|
|
const ref = createRef<_RoomView>();
|
|
|
|
await mountRoomView(ref);
|
|
|
|
return ref.current!;
|
|
|
|
};
|
2022-06-10 12:59:07 +00:00
|
|
|
|
2023-02-10 07:40:38 +00:00
|
|
|
it("when there is no room predecessor, getHiddenHighlightCount should return 0", async () => {
|
|
|
|
const instance = await getRoomViewInstance();
|
|
|
|
expect(instance.getHiddenHighlightCount()).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is an old room", () => {
|
|
|
|
let instance: _RoomView;
|
|
|
|
let oldRoom: Room;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
instance = await getRoomViewInstance();
|
|
|
|
oldRoom = new Room("!old:example.com", cli, cli.getSafeUserId());
|
|
|
|
rooms.set(oldRoom.roomId, oldRoom);
|
2023-02-16 09:38:44 +00:00
|
|
|
jest.spyOn(room, "findPredecessor").mockReturnValue({ roomId: oldRoom.roomId });
|
2023-02-10 07:40:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("and it has 0 unreads, getHiddenHighlightCount should return 0", async () => {
|
|
|
|
jest.spyOn(oldRoom, "getUnreadNotificationCount").mockReturnValue(0);
|
|
|
|
expect(instance.getHiddenHighlightCount()).toBe(0);
|
|
|
|
// assert that msc3946ProcessDynamicPredecessor is false by default
|
|
|
|
expect(room.findPredecessor).toHaveBeenCalledWith(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and it has 23 unreads, getHiddenHighlightCount should return 23", async () => {
|
|
|
|
jest.spyOn(oldRoom, "getUnreadNotificationCount").mockReturnValue(23);
|
|
|
|
expect(instance.getHiddenHighlightCount()).toBe(23);
|
|
|
|
});
|
|
|
|
|
2023-03-08 14:18:03 +00:00
|
|
|
describe("and feature_dynamic_room_predecessors is enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
instance.setState({ msc3946ProcessDynamicPredecessor: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
instance.setState({ msc3946ProcessDynamicPredecessor: false });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should pass the setting to findPredecessor", async () => {
|
|
|
|
expect(instance.getHiddenHighlightCount()).toBe(0);
|
|
|
|
expect(room.findPredecessor).toHaveBeenCalledWith(true);
|
|
|
|
});
|
2023-02-10 07:40:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-10 12:59:07 +00:00
|
|
|
it("updates url preview visibility on encryption state change", async () => {
|
2024-03-12 14:52:54 +00:00
|
|
|
room.getMyMembership = jest.fn().mockReturnValue(KnownMembership.Join);
|
2022-06-10 12:59:07 +00:00
|
|
|
// we should be starting unencrypted
|
|
|
|
expect(cli.isRoomEncrypted(room.roomId)).toEqual(false);
|
2022-04-08 17:32:35 +00:00
|
|
|
|
2022-06-10 12:59:07 +00:00
|
|
|
const roomViewInstance = await getRoomViewInstance();
|
2022-04-08 17:32:35 +00:00
|
|
|
|
|
|
|
// in a default (non-encrypted room, it should start out with url previews enabled)
|
|
|
|
// This is a white-box test in that we're asserting things about the state, which
|
|
|
|
// is not ideal, but asserting that a URL preview just isn't there could risk the
|
|
|
|
// test being invalid because the previews just hasn't rendered yet. This feels
|
|
|
|
// like the safest way I think?
|
|
|
|
// This also relies on the default settings being URL previews on normally and
|
|
|
|
// off for e2e rooms because 1) it's probably useful to assert this and
|
|
|
|
// 2) SettingsStore is a static class and so very hard to mock out.
|
|
|
|
expect(roomViewInstance.state.showUrlPreview).toBe(true);
|
|
|
|
|
2022-06-10 12:59:07 +00:00
|
|
|
// now enable encryption
|
|
|
|
cli.isRoomEncrypted.mockReturnValue(true);
|
2022-04-08 17:32:35 +00:00
|
|
|
|
|
|
|
// and fake an encryption event into the room to prompt it to re-check
|
2022-06-10 12:59:07 +00:00
|
|
|
room.addLiveEvents([
|
|
|
|
new MatrixEvent({
|
2022-04-08 17:32:35 +00:00
|
|
|
type: "m.room.encryption",
|
2022-12-08 21:37:25 +00:00
|
|
|
sender: cli.getUserId()!,
|
2022-04-08 17:32:35 +00:00
|
|
|
content: {},
|
|
|
|
event_id: "someid",
|
2022-06-10 12:59:07 +00:00
|
|
|
room_id: room.roomId,
|
2022-04-08 17:32:35 +00:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// URL previews should now be disabled
|
|
|
|
expect(roomViewInstance.state.showUrlPreview).toBe(false);
|
|
|
|
});
|
2022-06-10 12:59:07 +00:00
|
|
|
|
|
|
|
it("updates live timeline when a timeline reset happens", async () => {
|
|
|
|
const roomViewInstance = await getRoomViewInstance();
|
|
|
|
const oldTimeline = roomViewInstance.state.liveTimeline;
|
|
|
|
|
|
|
|
room.getUnfilteredTimelineSet().resetLiveTimeline();
|
|
|
|
expect(roomViewInstance.state.liveTimeline).not.toEqual(oldTimeline);
|
|
|
|
});
|
2022-06-17 20:57:40 +00:00
|
|
|
|
2022-12-08 21:37:25 +00:00
|
|
|
describe("with virtual rooms", () => {
|
|
|
|
it("checks for a virtual room on initial load", async () => {
|
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(VoipUserMapper.sharedInstance().getVirtualRoomForRoom).toHaveBeenCalledWith(room.roomId);
|
|
|
|
|
|
|
|
// quick check that rendered without error
|
|
|
|
expect(container.querySelector(".mx_ErrorBoundary")).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("checks for a virtual room on room event", async () => {
|
|
|
|
await renderRoomView();
|
|
|
|
expect(VoipUserMapper.sharedInstance().getVirtualRoomForRoom).toHaveBeenCalledWith(room.roomId);
|
|
|
|
|
|
|
|
cli.emit(ClientEvent.Room, room);
|
|
|
|
|
|
|
|
// called again after room event
|
|
|
|
expect(VoipUserMapper.sharedInstance().getVirtualRoomForRoom).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-17 20:57:40 +00:00
|
|
|
describe("video rooms", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
// Make it a video room
|
|
|
|
room.isElementVideoRoom = () => true;
|
|
|
|
await SettingsStore.setValue("feature_video_rooms", null, SettingLevel.DEVICE, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("normally doesn't open the chat panel", async () => {
|
|
|
|
jest.spyOn(NotificationState.prototype, "isUnread", "get").mockReturnValue(false);
|
|
|
|
await mountRoomView();
|
2022-10-19 12:07:03 +00:00
|
|
|
expect(stores.rightPanelStore.isOpen).toEqual(false);
|
2022-06-17 20:57:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("opens the chat panel if there are unread messages", async () => {
|
|
|
|
jest.spyOn(NotificationState.prototype, "isUnread", "get").mockReturnValue(true);
|
|
|
|
await mountRoomView();
|
2022-10-19 12:07:03 +00:00
|
|
|
expect(stores.rightPanelStore.isOpen).toEqual(true);
|
|
|
|
expect(stores.rightPanelStore.currentCard.phase).toEqual(RightPanelPhases.Timeline);
|
2022-06-17 20:57:40 +00:00
|
|
|
});
|
2024-10-04 09:41:00 +00:00
|
|
|
|
|
|
|
it("should render joined video room view", async () => {
|
|
|
|
jest.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Join);
|
|
|
|
const { asFragment } = await mountRoomView();
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
2022-06-17 20:57:40 +00:00
|
|
|
});
|
2022-08-04 06:19:52 +00:00
|
|
|
|
|
|
|
describe("for a local room", () => {
|
|
|
|
let localRoom: LocalRoom;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
localRoom = room = await createDmLocalRoom(cli, [new DirectoryMember({ user_id: "@user:example.com" })]);
|
2023-02-10 07:40:38 +00:00
|
|
|
rooms.set(localRoom.roomId, localRoom);
|
2022-08-04 06:19:52 +00:00
|
|
|
cli.store.storeRoom(room);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove the room from the store on unmount", async () => {
|
2022-12-08 21:37:25 +00:00
|
|
|
const { unmount } = await renderRoomView();
|
|
|
|
unmount();
|
2022-08-04 06:19:52 +00:00
|
|
|
expect(cli.store.removeRoom).toHaveBeenCalledWith(room.roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("in state NEW", () => {
|
|
|
|
it("should match the snapshot", async () => {
|
2022-12-08 21:37:25 +00:00
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-08-04 06:19:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("that is encrypted", () => {
|
|
|
|
beforeEach(() => {
|
2024-11-19 10:09:25 +00:00
|
|
|
// Not all the calls to cli.isRoomEncrypted are migrated, so we need to mock both.
|
2022-08-04 06:19:52 +00:00
|
|
|
mocked(cli.isRoomEncrypted).mockReturnValue(true);
|
2024-11-19 10:09:25 +00:00
|
|
|
jest.spyOn(cli, "getCrypto").mockReturnValue(crypto);
|
|
|
|
jest.spyOn(cli.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
|
|
|
|
jest.spyOn(cli.getCrypto()!, "getUserVerificationStatus").mockResolvedValue(
|
|
|
|
new UserVerificationStatus(false, true, false),
|
|
|
|
);
|
2022-08-04 06:19:52 +00:00
|
|
|
localRoom.encrypted = true;
|
|
|
|
localRoom.currentState.setStateEvents([
|
|
|
|
new MatrixEvent({
|
|
|
|
event_id: `~${localRoom.roomId}:${cli.makeTxnId()}`,
|
|
|
|
type: EventType.RoomEncryption,
|
|
|
|
content: {
|
2024-07-04 15:50:07 +00:00
|
|
|
algorithm: MEGOLM_ENCRYPTION_ALGORITHM,
|
2022-08-04 06:19:52 +00:00
|
|
|
},
|
2022-12-08 21:37:25 +00:00
|
|
|
sender: cli.getUserId()!,
|
2022-08-04 06:19:52 +00:00
|
|
|
state_key: "",
|
|
|
|
room_id: localRoom.roomId,
|
|
|
|
origin_server_ts: Date.now(),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should match the snapshot", async () => {
|
2022-12-08 21:37:25 +00:00
|
|
|
const { container } = await renderRoomView();
|
2024-11-19 10:09:25 +00:00
|
|
|
await waitFor(() => expect(container).toMatchSnapshot());
|
2022-08-04 06:19:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("in state CREATING should match the snapshot", async () => {
|
|
|
|
localRoom.state = LocalRoomState.CREATING;
|
2022-12-08 21:37:25 +00:00
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-08-04 06:19:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("in state ERROR", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
localRoom.state = LocalRoomState.ERROR;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should match the snapshot", async () => {
|
2022-12-08 21:37:25 +00:00
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-08-04 06:19:52 +00:00
|
|
|
});
|
|
|
|
|
2022-12-08 21:37:25 +00:00
|
|
|
it("clicking retry should set the room state to new dispatch a local room event", async () => {
|
2022-08-04 06:19:52 +00:00
|
|
|
jest.spyOn(defaultDispatcher, "dispatch");
|
2022-12-08 21:37:25 +00:00
|
|
|
const { getByText } = await renderRoomView();
|
|
|
|
fireEvent.click(getByText("Retry"));
|
2022-08-04 06:19:52 +00:00
|
|
|
expect(localRoom.state).toBe(LocalRoomState.NEW);
|
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: "local_room_event",
|
|
|
|
roomId: room.roomId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-02-22 12:59:42 +00:00
|
|
|
|
2023-03-06 11:08:04 +00:00
|
|
|
describe("when rendering a DM room with a single third-party invite", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
mkRoomMemberJoinEvent(cli.getSafeUserId(), room.roomId),
|
|
|
|
mkThirdPartyInviteEvent(cli.getSafeUserId(), "user@example.com", room.roomId),
|
|
|
|
]);
|
|
|
|
jest.spyOn(DMRoomMap.shared(), "getUserIdForRoomId").mockReturnValue(cli.getSafeUserId());
|
|
|
|
jest.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([room.roomId]));
|
|
|
|
mocked(cli).isRoomEncrypted.mockReturnValue(true);
|
|
|
|
await renderRoomView();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render the »waiting for third-party« view", () => {
|
|
|
|
expect(screen.getByText("Waiting for users to join Element")).toBeInTheDocument();
|
|
|
|
expect(
|
|
|
|
screen.getByText(
|
|
|
|
"Once invited users have joined Element, you will be able to chat and the room will be end-to-end encrypted",
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
|
|
|
// no message composer
|
|
|
|
expect(screen.queryByText("Send an encrypted message…")).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByText("Send a message…")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-22 12:59:42 +00:00
|
|
|
describe("when there is a RoomView", () => {
|
|
|
|
const widget1Id = "widget1";
|
|
|
|
const widget2Id = "widget2";
|
|
|
|
const otherUserId = "@other:example.com";
|
|
|
|
|
|
|
|
const addJitsiWidget = async (id: string, user: string, ts?: number): Promise<void> => {
|
|
|
|
const widgetEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
room: room.roomId,
|
|
|
|
user,
|
|
|
|
type: "im.vector.modular.widgets",
|
|
|
|
content: {
|
|
|
|
id,
|
|
|
|
name: "Jitsi",
|
|
|
|
type: WidgetType.JITSI.preferred,
|
|
|
|
url: "https://example.com",
|
|
|
|
},
|
|
|
|
skey: id,
|
|
|
|
ts,
|
|
|
|
});
|
|
|
|
room.addLiveEvents([widgetEvent]);
|
|
|
|
room.currentState.setStateEvents([widgetEvent]);
|
|
|
|
cli.emit(RoomStateEvent.Events, widgetEvent, room.currentState, null);
|
|
|
|
await flushPromises();
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
jest.spyOn(WidgetUtils, "setRoomWidget");
|
|
|
|
const widgetStore = WidgetStore.instance;
|
|
|
|
await setupAsyncStoreWithClient(widgetStore, cli);
|
|
|
|
getRoomViewInstance();
|
|
|
|
});
|
|
|
|
|
|
|
|
const itShouldNotRemoveTheLastWidget = (): void => {
|
|
|
|
it("should not remove the last widget", (): void => {
|
|
|
|
expect(WidgetUtils.setRoomWidget).not.toHaveBeenCalledWith(room.roomId, widget2Id);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
describe("and there is a Jitsi widget from another user", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await addJitsiWidget(widget1Id, otherUserId, 10_000);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the current user adds a Jitsi widget after 10s", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await addJitsiWidget(widget2Id, cli.getSafeUserId(), 20_000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("the last Jitsi widget should be removed", () => {
|
2023-05-23 15:24:12 +00:00
|
|
|
expect(WidgetUtils.setRoomWidget).toHaveBeenCalledWith(cli, room.roomId, widget2Id);
|
2023-02-22 12:59:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the current user adds a Jitsi widget after two minutes", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await addJitsiWidget(widget2Id, cli.getSafeUserId(), 130_000);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldNotRemoveTheLastWidget();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the current user adds a Jitsi widget without timestamp", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await addJitsiWidget(widget2Id, cli.getSafeUserId());
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldNotRemoveTheLastWidget();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and there is a Jitsi widget from another user without timestamp", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await addJitsiWidget(widget1Id, otherUserId);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the current user adds a Jitsi widget", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await addJitsiWidget(widget2Id, cli.getSafeUserId(), 10_000);
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldNotRemoveTheLastWidget();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-06-19 16:08:16 +00:00
|
|
|
|
|
|
|
it("should show error view if failed to look up room alias", async () => {
|
|
|
|
const { asFragment, findByText } = await renderRoomView(false);
|
|
|
|
|
|
|
|
defaultDispatcher.dispatch<ViewRoomErrorPayload>({
|
|
|
|
action: Action.ViewRoomError,
|
|
|
|
room_alias: "#addy:server",
|
|
|
|
room_id: null,
|
|
|
|
err: new MatrixError({ errcode: "M_NOT_FOUND" }),
|
|
|
|
});
|
|
|
|
await emitPromise(stores.roomViewStore, UPDATE_EVENT);
|
|
|
|
|
|
|
|
await findByText("Are you sure you're at the right place?");
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
2023-07-25 07:57:26 +00:00
|
|
|
|
2023-08-07 06:27:09 +00:00
|
|
|
describe("knock rooms", () => {
|
|
|
|
const client = createTestClient();
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === "feature_ask_to_join");
|
|
|
|
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Knock);
|
2024-11-05 19:12:43 +00:00
|
|
|
jest.spyOn(defaultDispatcher, "dispatch");
|
2023-08-07 06:27:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("allows to request to join", async () => {
|
|
|
|
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(client);
|
|
|
|
jest.spyOn(client, "knockRoom").mockResolvedValue({ room_id: room.roomId });
|
|
|
|
|
|
|
|
await mountRoomView();
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Request access" }));
|
2024-11-05 19:12:43 +00:00
|
|
|
await untilDispatch(Action.SubmitAskToJoin, defaultDispatcher);
|
2023-08-07 06:27:09 +00:00
|
|
|
|
2024-11-05 19:12:43 +00:00
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({
|
2023-08-07 06:27:09 +00:00
|
|
|
action: "submit_ask_to_join",
|
|
|
|
roomId: room.roomId,
|
|
|
|
opts: { reason: undefined },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("allows to cancel a join request", async () => {
|
|
|
|
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(client);
|
|
|
|
jest.spyOn(client, "leave").mockResolvedValue({});
|
2024-03-12 14:52:54 +00:00
|
|
|
jest.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Knock);
|
2023-08-07 06:27:09 +00:00
|
|
|
|
|
|
|
await mountRoomView();
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Cancel request" }));
|
2024-11-05 19:12:43 +00:00
|
|
|
await untilDispatch(Action.CancelAskToJoin, defaultDispatcher);
|
2023-08-07 06:27:09 +00:00
|
|
|
|
2024-11-05 19:12:43 +00:00
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: "cancel_ask_to_join",
|
|
|
|
roomId: room.roomId,
|
|
|
|
});
|
2023-08-07 06:27:09 +00:00
|
|
|
});
|
|
|
|
});
|
2023-12-19 16:01:17 +00:00
|
|
|
|
|
|
|
it("should close search results when edit is clicked", async () => {
|
2024-03-12 14:52:54 +00:00
|
|
|
room.getMyMembership = jest.fn().mockReturnValue(KnownMembership.Join);
|
2023-12-19 16:01:17 +00:00
|
|
|
|
|
|
|
const eventMapper = (obj: Partial<IEvent>) => new MatrixEvent(obj);
|
|
|
|
|
|
|
|
const roomViewRef = createRef<_RoomView>();
|
|
|
|
const { container, getByText, findByLabelText } = await mountRoomView(roomViewRef);
|
|
|
|
// @ts-ignore - triggering a search organically is a lot of work
|
|
|
|
roomViewRef.current!.setState({
|
|
|
|
search: {
|
|
|
|
searchId: 1,
|
|
|
|
roomId: room.roomId,
|
|
|
|
term: "search term",
|
|
|
|
scope: SearchScope.Room,
|
|
|
|
promise: Promise.resolve({
|
|
|
|
results: [
|
|
|
|
SearchResult.fromJson(
|
|
|
|
{
|
|
|
|
rank: 1,
|
|
|
|
result: {
|
|
|
|
content: {
|
|
|
|
body: "search term",
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
type: "m.room.message",
|
|
|
|
event_id: "$eventId",
|
|
|
|
sender: cli.getSafeUserId(),
|
|
|
|
origin_server_ts: 123456789,
|
|
|
|
room_id: room.roomId,
|
|
|
|
},
|
|
|
|
context: {
|
|
|
|
events_before: [],
|
|
|
|
events_after: [],
|
|
|
|
profile_info: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventMapper,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
highlights: [],
|
|
|
|
count: 1,
|
|
|
|
}),
|
|
|
|
inProgress: false,
|
|
|
|
count: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(container.querySelector(".mx_RoomView_searchResultsPanel")).toBeVisible();
|
|
|
|
});
|
|
|
|
const prom = waitForElementToBeRemoved(() => container.querySelector(".mx_RoomView_searchResultsPanel"));
|
|
|
|
|
|
|
|
await userEvent.hover(getByText("search term"));
|
|
|
|
await userEvent.click(await findByLabelText("Edit"));
|
|
|
|
|
|
|
|
await prom;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should switch rooms when edit is clicked on a search result for a different room", async () => {
|
|
|
|
const room2 = new Room(`!${roomCount++}:example.org`, cli, "@alice:example.org");
|
|
|
|
rooms.set(room2.roomId, room2);
|
|
|
|
|
2024-03-12 14:52:54 +00:00
|
|
|
room.getMyMembership = jest.fn().mockReturnValue(KnownMembership.Join);
|
2023-12-19 16:01:17 +00:00
|
|
|
|
|
|
|
const eventMapper = (obj: Partial<IEvent>) => new MatrixEvent(obj);
|
|
|
|
|
|
|
|
const roomViewRef = createRef<_RoomView>();
|
|
|
|
const { container, getByText, findByLabelText } = await mountRoomView(roomViewRef);
|
|
|
|
// @ts-ignore - triggering a search organically is a lot of work
|
|
|
|
roomViewRef.current!.setState({
|
|
|
|
search: {
|
|
|
|
searchId: 1,
|
|
|
|
roomId: room.roomId,
|
|
|
|
term: "search term",
|
|
|
|
scope: SearchScope.All,
|
|
|
|
promise: Promise.resolve({
|
|
|
|
results: [
|
|
|
|
SearchResult.fromJson(
|
|
|
|
{
|
|
|
|
rank: 1,
|
|
|
|
result: {
|
|
|
|
content: {
|
|
|
|
body: "search term",
|
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
type: "m.room.message",
|
|
|
|
event_id: "$eventId",
|
|
|
|
sender: cli.getSafeUserId(),
|
|
|
|
origin_server_ts: 123456789,
|
|
|
|
room_id: room2.roomId,
|
|
|
|
},
|
|
|
|
context: {
|
|
|
|
events_before: [],
|
|
|
|
events_after: [],
|
|
|
|
profile_info: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventMapper,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
highlights: [],
|
|
|
|
count: 1,
|
|
|
|
}),
|
|
|
|
inProgress: false,
|
|
|
|
count: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(container.querySelector(".mx_RoomView_searchResultsPanel")).toBeVisible();
|
|
|
|
});
|
2024-11-05 19:12:43 +00:00
|
|
|
const prom = untilDispatch(Action.ViewRoom, defaultDispatcher);
|
2023-12-19 16:01:17 +00:00
|
|
|
|
|
|
|
await userEvent.hover(getByText("search term"));
|
|
|
|
await userEvent.click(await findByLabelText("Edit"));
|
|
|
|
|
|
|
|
await expect(prom).resolves.toEqual(expect.objectContaining({ room_id: room2.roomId }));
|
|
|
|
});
|
2024-02-27 12:02:16 +00:00
|
|
|
|
|
|
|
it("fires Action.RoomLoaded", async () => {
|
2024-11-05 19:12:43 +00:00
|
|
|
jest.spyOn(defaultDispatcher, "dispatch");
|
2024-02-27 12:02:16 +00:00
|
|
|
await mountRoomView();
|
2024-11-05 19:12:43 +00:00
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ action: Action.RoomLoaded });
|
2024-02-27 12:02:16 +00:00
|
|
|
});
|
2022-04-08 17:32:35 +00:00
|
|
|
});
|