2022-10-20 13:48:25 +02:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2022-10-20 13:48:25 +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-10-20 13:48:25 +02:00
|
|
|
*/
|
|
|
|
|
2023-02-22 11:52:55 +01:00
|
|
|
import { act, getByTestId, render, RenderResult, waitFor } from "@testing-library/react";
|
2022-10-20 13:48:25 +02:00
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
import { mocked } from "jest-mock";
|
2023-08-09 09:18:41 +02:00
|
|
|
import {
|
|
|
|
MsgType,
|
|
|
|
RelationType,
|
|
|
|
EventStatus,
|
|
|
|
MatrixEvent,
|
|
|
|
Room,
|
|
|
|
MatrixClient,
|
|
|
|
PendingEventOrdering,
|
2023-08-14 10:58:55 +02:00
|
|
|
THREAD_RELATION_TYPE,
|
2023-08-09 09:18:41 +02:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-10-20 13:48:25 +02:00
|
|
|
import React, { useState } from "react";
|
|
|
|
|
|
|
|
import ThreadView from "../../../src/components/structures/ThreadView";
|
|
|
|
import MatrixClientContext from "../../../src/contexts/MatrixClientContext";
|
|
|
|
import RoomContext from "../../../src/contexts/RoomContext";
|
2022-10-25 18:53:31 +02:00
|
|
|
import { SdkContextClass } from "../../../src/contexts/SDKContext";
|
2023-01-23 14:35:44 +01:00
|
|
|
import { Action } from "../../../src/dispatcher/actions";
|
|
|
|
import dispatcher from "../../../src/dispatcher/dispatcher";
|
2022-10-20 13:48:25 +02:00
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
|
|
|
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
|
|
|
import ResizeNotifier from "../../../src/utils/ResizeNotifier";
|
|
|
|
import { mockPlatformPeg } from "../../test-utils/platform";
|
|
|
|
import { getRoomContext } from "../../test-utils/room";
|
2023-07-14 12:59:55 +02:00
|
|
|
import { mkMessage, stubClient } from "../../test-utils/test-utils";
|
2022-10-20 13:48:25 +02:00
|
|
|
import { mkThread } from "../../test-utils/threads";
|
|
|
|
|
|
|
|
describe("ThreadView", () => {
|
|
|
|
const ROOM_ID = "!roomId:example.org";
|
|
|
|
const SENDER = "@alice:example.org";
|
|
|
|
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
let rootEvent: MatrixEvent;
|
|
|
|
|
|
|
|
let changeEvent: (event: MatrixEvent) => void;
|
|
|
|
|
2023-01-23 14:35:44 +01:00
|
|
|
function TestThreadView({ initialEvent }: { initialEvent?: MatrixEvent }) {
|
2022-10-20 13:48:25 +02:00
|
|
|
const [event, setEvent] = useState(rootEvent);
|
|
|
|
changeEvent = setEvent;
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
return (
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider
|
|
|
|
value={getRoomContext(room, {
|
|
|
|
canSendMessages: true,
|
|
|
|
})}
|
|
|
|
>
|
2023-01-23 14:35:44 +01:00
|
|
|
<ThreadView
|
|
|
|
room={room}
|
|
|
|
onClose={jest.fn()}
|
|
|
|
mxEvent={event}
|
|
|
|
initialEvent={initialEvent}
|
|
|
|
resizeNotifier={new ResizeNotifier()}
|
|
|
|
/>
|
2022-12-12 12:24:14 +01:00
|
|
|
</RoomContext.Provider>
|
|
|
|
,
|
|
|
|
</MatrixClientContext.Provider>
|
|
|
|
);
|
2022-10-20 13:48:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-23 14:35:44 +01:00
|
|
|
async function getComponent(initialEvent?: MatrixEvent): Promise<RenderResult> {
|
|
|
|
const renderResult = render(<TestThreadView initialEvent={initialEvent} />);
|
2022-10-20 13:48:25 +02:00
|
|
|
|
|
|
|
await waitFor(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(() => getByTestId(renderResult.container, "spinner")).toThrow();
|
2022-10-20 13:48:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return renderResult;
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
async function sendMessage(container: HTMLElement, text: string): Promise<void> {
|
2022-10-20 13:48:25 +02:00
|
|
|
const composer = getByTestId(container, "basicmessagecomposer");
|
|
|
|
await userEvent.click(composer);
|
|
|
|
await userEvent.keyboard(text);
|
|
|
|
const sendMessageBtn = getByTestId(container, "sendmessagebtn");
|
|
|
|
await userEvent.click(sendMessageBtn);
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:39:16 +01:00
|
|
|
function expectedMessageBody(rootEvent: MatrixEvent, message: string) {
|
2022-10-20 13:48:25 +02:00
|
|
|
return {
|
|
|
|
"body": message,
|
|
|
|
"m.relates_to": {
|
|
|
|
"event_id": rootEvent.getId(),
|
|
|
|
"is_falling_back": true,
|
|
|
|
"m.in_reply_to": {
|
2022-12-12 12:24:14 +01:00
|
|
|
event_id: rootEvent
|
2023-02-16 18:21:44 +01:00
|
|
|
.getThread()!
|
2022-12-12 12:24:14 +01:00
|
|
|
.lastReply((ev: MatrixEvent) => {
|
|
|
|
return ev.isRelation(THREAD_RELATION_TYPE.name);
|
2023-02-16 18:21:44 +01:00
|
|
|
})!
|
2022-12-12 12:24:14 +01:00
|
|
|
.getId(),
|
2022-10-20 13:48:25 +02:00
|
|
|
},
|
|
|
|
"rel_type": RelationType.Thread,
|
|
|
|
},
|
|
|
|
"msgtype": MsgType.Text,
|
2023-09-14 13:36:15 +02:00
|
|
|
"m.mentions": {},
|
2022-10-20 13:48:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
|
|
|
stubClient();
|
|
|
|
mockPlatformPeg();
|
2023-06-05 19:12:23 +02:00
|
|
|
mockClient = mocked(MatrixClientPeg.safeGet());
|
2023-01-30 13:20:11 +01:00
|
|
|
jest.spyOn(mockClient, "supportsThreads").mockReturnValue(true);
|
2022-10-20 13:48:25 +02:00
|
|
|
|
|
|
|
room = new Room(ROOM_ID, mockClient, mockClient.getUserId() ?? "", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = mkThread({
|
|
|
|
room,
|
|
|
|
client: mockClient,
|
2023-02-16 18:21:44 +01:00
|
|
|
authorId: mockClient.getUserId()!,
|
|
|
|
participantUserIds: [mockClient.getUserId()!],
|
2022-10-20 13:48:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
rootEvent = res.rootEvent;
|
|
|
|
|
2023-05-23 17:24:12 +02:00
|
|
|
DMRoomMap.makeShared(mockClient);
|
2022-10-20 13:48:25 +02:00
|
|
|
jest.spyOn(DMRoomMap.shared(), "getUserIdForRoomId").mockReturnValue(SENDER);
|
|
|
|
});
|
|
|
|
|
2023-07-14 12:59:55 +02:00
|
|
|
it("does not include pending root event in the timeline twice", async () => {
|
|
|
|
rootEvent = mkMessage({
|
|
|
|
user: mockClient.getUserId()!,
|
|
|
|
event: true,
|
|
|
|
room: room.roomId,
|
|
|
|
msg: "root event message " + Math.random(),
|
|
|
|
});
|
|
|
|
|
|
|
|
rootEvent.status = EventStatus.SENDING;
|
|
|
|
rootEvent.setTxnId("1234");
|
|
|
|
room.addPendingEvent(rootEvent, "1234");
|
|
|
|
room.updatePendingEvent(rootEvent, EventStatus.SENT, rootEvent.getId());
|
|
|
|
|
|
|
|
const { container } = await getComponent();
|
|
|
|
const tiles = container.getElementsByClassName("mx_EventTile");
|
|
|
|
expect(tiles.length).toEqual(1);
|
|
|
|
});
|
|
|
|
|
2022-10-20 13:48:25 +02:00
|
|
|
it("sends a message with the correct fallback", async () => {
|
|
|
|
const { container } = await getComponent();
|
|
|
|
|
|
|
|
await sendMessage(container, "Hello world!");
|
|
|
|
|
|
|
|
expect(mockClient.sendMessage).toHaveBeenCalledWith(
|
2022-12-12 12:24:14 +01:00
|
|
|
ROOM_ID,
|
|
|
|
rootEvent.getId(),
|
|
|
|
expectedMessageBody(rootEvent, "Hello world!"),
|
2022-10-20 13:48:25 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-03-01 16:23:35 +01:00
|
|
|
it("sends a thread message with the correct fallback", async () => {
|
2022-10-20 13:48:25 +02:00
|
|
|
const { container } = await getComponent();
|
|
|
|
|
|
|
|
const { rootEvent: rootEvent2 } = mkThread({
|
|
|
|
room,
|
|
|
|
client: mockClient,
|
2023-02-16 18:21:44 +01:00
|
|
|
authorId: mockClient.getUserId()!,
|
|
|
|
participantUserIds: [mockClient.getUserId()!],
|
2022-10-20 13:48:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
changeEvent(rootEvent2);
|
|
|
|
});
|
|
|
|
|
|
|
|
await sendMessage(container, "yolo");
|
|
|
|
|
|
|
|
expect(mockClient.sendMessage).toHaveBeenCalledWith(
|
2022-12-12 12:24:14 +01:00
|
|
|
ROOM_ID,
|
|
|
|
rootEvent2.getId(),
|
|
|
|
expectedMessageBody(rootEvent2, "yolo"),
|
2022-10-20 13:48:25 +02:00
|
|
|
);
|
|
|
|
});
|
2022-10-25 18:53:31 +02:00
|
|
|
|
|
|
|
it("sets the correct thread in the room view store", async () => {
|
|
|
|
// expect(SdkContextClass.instance.roomViewStore.getThreadId()).toBeNull();
|
|
|
|
const { unmount } = await getComponent();
|
2024-05-02 16:53:35 +02:00
|
|
|
waitFor(() => {
|
|
|
|
expect(SdkContextClass.instance.roomViewStore.getThreadId()).toBe(rootEvent.getId());
|
|
|
|
});
|
2022-10-25 18:53:31 +02:00
|
|
|
|
|
|
|
unmount();
|
|
|
|
await waitFor(() => expect(SdkContextClass.instance.roomViewStore.getThreadId()).toBeNull());
|
|
|
|
});
|
2023-01-23 14:35:44 +01:00
|
|
|
|
|
|
|
it("clears highlight message in the room view store", async () => {
|
|
|
|
jest.spyOn(SdkContextClass.instance.roomViewStore, "getRoomId").mockReturnValue(room.roomId);
|
|
|
|
const mock = jest.spyOn(dispatcher, "dispatch");
|
|
|
|
const { unmount } = await getComponent(rootEvent);
|
|
|
|
mock.mockClear();
|
|
|
|
unmount();
|
|
|
|
expect(mock).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
metricsTrigger: undefined,
|
|
|
|
});
|
|
|
|
});
|
2022-10-20 13:48:25 +02:00
|
|
|
});
|