2022-04-08 19:32:35 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2022-08-02 15:10:43 +02:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-06-10 14:59:07 +02:00
|
|
|
import { mount, ReactWrapper } from "enzyme";
|
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
2022-12-08 22:37:25 +01:00
|
|
|
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client";
|
2022-04-08 19:32:35 +02:00
|
|
|
import { Room, RoomEvent } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2022-08-04 08:19:52 +02:00
|
|
|
import { EventType } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { MEGOLM_ALGORITHM } from "matrix-js-sdk/src/crypto/olmlib";
|
2022-12-08 22:37:25 +01:00
|
|
|
import { fireEvent, render } from "@testing-library/react";
|
|
|
|
|
|
|
|
import {
|
|
|
|
stubClient,
|
|
|
|
mockPlatformPeg,
|
|
|
|
unmockPlatformPeg,
|
|
|
|
wrapInMatrixClientContext,
|
|
|
|
flushPromises,
|
|
|
|
} from "../../test-utils";
|
2022-04-08 19:32:35 +02:00
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
|
|
|
import { Action } from "../../../src/dispatcher/actions";
|
2022-08-04 08:19:52 +02:00
|
|
|
import { defaultDispatcher } from "../../../src/dispatcher/dispatcher";
|
2022-04-08 19:32:35 +02:00
|
|
|
import { ViewRoomPayload } from "../../../src/dispatcher/payloads/ViewRoomPayload";
|
2022-06-10 14:59:07 +02:00
|
|
|
import { RoomView as _RoomView } from "../../../src/components/structures/RoomView";
|
2022-04-08 19:32:35 +02:00
|
|
|
import ResizeNotifier from "../../../src/utils/ResizeNotifier";
|
2022-06-17 22:57:40 +02:00
|
|
|
import SettingsStore from "../../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../../src/settings/SettingLevel";
|
2022-04-08 19:32:35 +02:00
|
|
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
2022-06-17 22:57:40 +02:00
|
|
|
import { NotificationState } from "../../../src/stores/notifications/NotificationState";
|
|
|
|
import { RightPanelPhases } from "../../../src/stores/right-panel/RightPanelStorePhases";
|
2022-08-04 08:19:52 +02:00
|
|
|
import { LocalRoom, LocalRoomState } from "../../../src/models/LocalRoom";
|
|
|
|
import { DirectoryMember } from "../../../src/utils/direct-messages";
|
|
|
|
import { createDmLocalRoom } from "../../../src/utils/dm/createDmLocalRoom";
|
2022-09-20 17:32:39 +02:00
|
|
|
import { UPDATE_EVENT } from "../../../src/stores/AsyncStore";
|
2022-10-19 14:07:03 +02:00
|
|
|
import { SdkContextClass, SDKContext } from "../../../src/contexts/SDKContext";
|
2022-12-08 22:37:25 +01:00
|
|
|
import VoipUserMapper from "../../../src/VoipUserMapper";
|
2022-04-08 19:32:35 +02:00
|
|
|
|
2022-06-10 14:59:07 +02:00
|
|
|
const RoomView = wrapInMatrixClientContext(_RoomView);
|
|
|
|
|
2022-04-08 19:32:35 +02:00
|
|
|
describe("RoomView", () => {
|
2022-06-10 14:59:07 +02:00
|
|
|
let cli: MockedObject<MatrixClient>;
|
|
|
|
let room: Room;
|
2022-06-17 22:57:40 +02:00
|
|
|
let roomCount = 0;
|
2022-10-19 14:07:03 +02:00
|
|
|
let stores: SdkContextClass;
|
2022-08-04 08:19:52 +02:00
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
mockPlatformPeg({ reload: () => {} });
|
2022-04-08 19:32:35 +02:00
|
|
|
stubClient();
|
2022-06-10 14:59:07 +02:00
|
|
|
cli = mocked(MatrixClientPeg.get());
|
2022-04-08 19:32:35 +02:00
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
room = new Room(`!${roomCount++}:example.org`, cli, "@alice:example.org");
|
2022-06-10 14:59:07 +02:00
|
|
|
room.getPendingEvents = () => [];
|
2022-08-04 08:19:52 +02:00
|
|
|
cli.getRoom.mockImplementation(() => room);
|
2022-06-10 14:59:07 +02: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 19:32:35 +02:00
|
|
|
|
|
|
|
DMRoomMap.makeShared();
|
2022-10-19 14:07:03 +02:00
|
|
|
stores = new SdkContextClass();
|
|
|
|
stores.client = cli;
|
|
|
|
stores.rightPanelStore.useUnitTestClient(cli);
|
2022-12-08 22:37:25 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(VoipUserMapper.sharedInstance(), "getVirtualRoomForRoom").mockResolvedValue(null);
|
2022-06-17 22:57:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
unmockPlatformPeg();
|
|
|
|
jest.restoreAllMocks();
|
2022-06-10 14:59:07 +02:00
|
|
|
});
|
2022-04-08 19:32:35 +02:00
|
|
|
|
2022-06-10 14:59:07 +02:00
|
|
|
const mountRoomView = async (): Promise<ReactWrapper> => {
|
2022-10-19 14:07:03 +02:00
|
|
|
if (stores.roomViewStore.getRoomId() !== room.roomId) {
|
2022-12-12 12:24:14 +01:00
|
|
|
const switchedRoom = new Promise<void>((resolve) => {
|
2022-09-20 17:32:39 +02:00
|
|
|
const subFn = () => {
|
2022-10-19 14:07:03 +02:00
|
|
|
if (stores.roomViewStore.getRoomId()) {
|
|
|
|
stores.roomViewStore.off(UPDATE_EVENT, subFn);
|
2022-06-10 14:59:07 +02:00
|
|
|
resolve();
|
|
|
|
}
|
2022-09-20 17:32:39 +02:00
|
|
|
};
|
2022-10-19 14:07:03 +02:00
|
|
|
stores.roomViewStore.on(UPDATE_EVENT, subFn);
|
2022-04-08 19:32:35 +02:00
|
|
|
});
|
|
|
|
|
2022-08-04 08:19:52 +02:00
|
|
|
defaultDispatcher.dispatch<ViewRoomPayload>({
|
2022-06-10 14:59:07 +02:00
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
2022-12-08 22:37:25 +01:00
|
|
|
metricsTrigger: undefined,
|
2022-06-10 14:59:07 +02:00
|
|
|
});
|
2022-04-08 19:32:35 +02:00
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
await switchedRoom;
|
2022-06-10 14:59:07 +02:00
|
|
|
}
|
2022-04-08 19:32:35 +02:00
|
|
|
|
2022-06-17 22:57:40 +02:00
|
|
|
const roomView = mount(
|
2022-10-19 14:07:03 +02:00
|
|
|
<SDKContext.Provider value={stores}>
|
|
|
|
<RoomView
|
2022-12-08 22:37:25 +01:00
|
|
|
// threepidInvite should be optional on RoomView props
|
|
|
|
// it is treated as optional in RoomView
|
|
|
|
threepidInvite={undefined as any}
|
2022-10-19 14:07:03 +02:00
|
|
|
resizeNotifier={new ResizeNotifier()}
|
|
|
|
forceTimeline={false}
|
|
|
|
/>
|
|
|
|
</SDKContext.Provider>,
|
2022-06-10 14:59:07 +02:00
|
|
|
);
|
2022-12-08 22:37:25 +01:00
|
|
|
await flushPromises();
|
|
|
|
return roomView;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderRoomView = async (): Promise<ReturnType<typeof render>> => {
|
|
|
|
if (stores.roomViewStore.getRoomId() !== room.roomId) {
|
2022-12-12 12:24:14 +01:00
|
|
|
const switchedRoom = new Promise<void>((resolve) => {
|
2022-12-08 22:37:25 +01:00
|
|
|
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 22:57:40 +02:00
|
|
|
return roomView;
|
2022-06-10 14:59:07 +02:00
|
|
|
};
|
|
|
|
const getRoomViewInstance = async (): Promise<_RoomView> =>
|
|
|
|
(await mountRoomView()).find(_RoomView).instance() as _RoomView;
|
|
|
|
|
|
|
|
it("updates url preview visibility on encryption state change", async () => {
|
|
|
|
// we should be starting unencrypted
|
|
|
|
expect(cli.isCryptoEnabled()).toEqual(false);
|
|
|
|
expect(cli.isRoomEncrypted(room.roomId)).toEqual(false);
|
2022-04-08 19:32:35 +02:00
|
|
|
|
2022-06-10 14:59:07 +02:00
|
|
|
const roomViewInstance = await getRoomViewInstance();
|
2022-04-08 19:32:35 +02: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 14:59:07 +02:00
|
|
|
// now enable encryption
|
|
|
|
cli.isCryptoEnabled.mockReturnValue(true);
|
|
|
|
cli.isRoomEncrypted.mockReturnValue(true);
|
2022-04-08 19:32:35 +02:00
|
|
|
|
|
|
|
// and fake an encryption event into the room to prompt it to re-check
|
2022-12-12 12:24:14 +01:00
|
|
|
room.addLiveEvents([
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.room.encryption",
|
|
|
|
sender: cli.getUserId()!,
|
|
|
|
content: {},
|
|
|
|
event_id: "someid",
|
|
|
|
room_id: room.roomId,
|
|
|
|
}),
|
|
|
|
]);
|
2022-04-08 19:32:35 +02:00
|
|
|
|
|
|
|
// URL previews should now be disabled
|
|
|
|
expect(roomViewInstance.state.showUrlPreview).toBe(false);
|
|
|
|
});
|
2022-06-10 14:59:07 +02: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 22:57:40 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("with virtual rooms", () => {
|
2022-12-08 22:37:25 +01:00
|
|
|
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
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(container.querySelector(".mx_ErrorBoundary")).toBeFalsy();
|
2022-12-08 22:37:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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 22:57:40 +02: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 14:07:03 +02:00
|
|
|
expect(stores.rightPanelStore.isOpen).toEqual(false);
|
2022-06-17 22:57:40 +02: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 14:07:03 +02:00
|
|
|
expect(stores.rightPanelStore.isOpen).toEqual(true);
|
|
|
|
expect(stores.rightPanelStore.currentCard.phase).toEqual(RightPanelPhases.Timeline);
|
2022-06-17 22:57:40 +02:00
|
|
|
});
|
|
|
|
});
|
2022-08-04 08:19:52 +02:00
|
|
|
|
|
|
|
describe("for a local room", () => {
|
|
|
|
let localRoom: LocalRoom;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
localRoom = room = await createDmLocalRoom(cli, [new DirectoryMember({ user_id: "@user:example.com" })]);
|
|
|
|
cli.store.storeRoom(room);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove the room from the store on unmount", async () => {
|
2022-12-08 22:37:25 +01:00
|
|
|
const { unmount } = await renderRoomView();
|
|
|
|
unmount();
|
2022-08-04 08:19:52 +02:00
|
|
|
expect(cli.store.removeRoom).toHaveBeenCalledWith(room.roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("in state NEW", () => {
|
|
|
|
it("should match the snapshot", async () => {
|
2022-12-08 22:37:25 +01:00
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-08-04 08:19:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("that is encrypted", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(cli.isRoomEncrypted).mockReturnValue(true);
|
|
|
|
localRoom.encrypted = true;
|
|
|
|
localRoom.currentState.setStateEvents([
|
|
|
|
new MatrixEvent({
|
|
|
|
event_id: `~${localRoom.roomId}:${cli.makeTxnId()}`,
|
|
|
|
type: EventType.RoomEncryption,
|
|
|
|
content: {
|
|
|
|
algorithm: MEGOLM_ALGORITHM,
|
|
|
|
},
|
2022-12-08 22:37:25 +01:00
|
|
|
user_id: cli.getUserId()!,
|
|
|
|
sender: cli.getUserId()!,
|
2022-08-04 08:19:52 +02:00
|
|
|
state_key: "",
|
|
|
|
room_id: localRoom.roomId,
|
|
|
|
origin_server_ts: Date.now(),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should match the snapshot", async () => {
|
2022-12-08 22:37:25 +01:00
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-08-04 08:19:52 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("in state CREATING should match the snapshot", async () => {
|
|
|
|
localRoom.state = LocalRoomState.CREATING;
|
2022-12-08 22:37:25 +01:00
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-08-04 08:19:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("in state ERROR", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
localRoom.state = LocalRoomState.ERROR;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should match the snapshot", async () => {
|
2022-12-08 22:37:25 +01:00
|
|
|
const { container } = await renderRoomView();
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-08-04 08:19:52 +02:00
|
|
|
});
|
|
|
|
|
2022-12-08 22:37:25 +01:00
|
|
|
it("clicking retry should set the room state to new dispatch a local room event", async () => {
|
2022-08-04 08:19:52 +02:00
|
|
|
jest.spyOn(defaultDispatcher, "dispatch");
|
2022-12-08 22:37:25 +01:00
|
|
|
const { getByText } = await renderRoomView();
|
2022-12-12 12:24:14 +01:00
|
|
|
fireEvent.click(getByText("Retry"));
|
2022-08-04 08:19:52 +02:00
|
|
|
expect(localRoom.state).toBe(LocalRoomState.NEW);
|
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: "local_room_event",
|
|
|
|
roomId: room.roomId,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-04-08 19:32:35 +02:00
|
|
|
});
|