riot-web/test/components/views/rooms/wysiwyg_composer/utils/message-test.ts

309 lines
12 KiB
TypeScript
Raw Normal View History

2022-10-10 11:16:42 +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.
*/
2022-10-21 19:26:33 +02:00
import { EventStatus } from "matrix-js-sdk/src/matrix";
import { IRoomState } from "../../../../../../src/components/structures/RoomView";
2022-12-12 12:24:14 +01:00
import { editMessage, sendMessage } from "../../../../../../src/components/views/rooms/wysiwyg_composer/utils/message";
2022-10-21 19:26:33 +02:00
import { createTestClient, getRoomContext, mkEvent, mkStubRoom } from "../../../../../test-utils";
import defaultDispatcher from "../../../../../../src/dispatcher/dispatcher";
import SettingsStore from "../../../../../../src/settings/SettingsStore";
import { SettingLevel } from "../../../../../../src/settings/SettingLevel";
import { RoomPermalinkCreator } from "../../../../../../src/utils/permalinks/Permalinks";
import EditorStateTransfer from "../../../../../../src/utils/EditorStateTransfer";
2022-12-12 12:24:14 +01:00
import * as ConfirmRedactDialog from "../../../../../../src/components/views/dialogs/ConfirmRedactDialog";
2022-10-10 11:16:42 +02:00
2022-12-12 12:24:14 +01:00
describe("message", () => {
2022-10-14 15:52:05 +02:00
const permalinkCreator = {
forEvent(eventId: string): string {
return "$$permalink$$";
},
} as RoomPermalinkCreator;
2022-12-12 12:24:14 +01:00
const message = "<i><b>hello</b> world</i>";
2022-10-10 11:16:42 +02:00
const mockEvent = mkEvent({
type: "m.room.message",
2022-12-12 12:24:14 +01:00
room: "myfakeroom",
user: "myfakeuser",
2022-10-21 19:26:33 +02:00
content: {
2022-12-12 12:24:14 +01:00
msgtype: "m.text",
body: "Replying to this",
format: "org.matrix.custom.html",
formatted_body: "Replying to this",
2022-10-21 19:26:33 +02:00
},
2022-10-10 11:16:42 +02:00
event: true,
});
2022-10-21 19:26:33 +02:00
const mockClient = createTestClient();
2022-12-12 12:24:14 +01:00
const mockRoom = mkStubRoom("myfakeroom", "myfakeroom", mockClient) as any;
mockRoom.findEventById = jest.fn((eventId) => {
2022-10-21 19:26:33 +02:00
return eventId === mockEvent.getId() ? mockEvent : null;
2022-10-10 11:16:42 +02:00
});
2022-10-21 19:26:33 +02:00
const defaultRoomContext: IRoomState = getRoomContext(mockRoom, {});
2022-10-10 11:16:42 +02:00
2022-10-21 19:26:33 +02:00
const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
2022-10-14 15:52:05 +02:00
2022-10-21 19:26:33 +02:00
afterEach(() => {
jest.resetAllMocks();
2022-10-10 11:16:42 +02:00
});
2022-12-12 12:24:14 +01:00
describe("sendMessage", () => {
it("Should not send empty html message", async () => {
2022-10-14 15:52:05 +02:00
// When
2022-12-12 12:24:14 +01:00
await sendMessage("", true, { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator });
2022-10-14 15:52:05 +02:00
// Then
expect(mockClient.sendMessage).toBeCalledTimes(0);
expect(spyDispatcher).toBeCalledTimes(0);
});
it("Should not send message when there is no roomId", async () => {
// When
const mockRoomWithoutId = mkStubRoom("", "room without id", mockClient) as any;
const mockRoomContextWithoutId: IRoomState = getRoomContext(mockRoomWithoutId, {});
await sendMessage(message, true, {
roomContext: mockRoomContextWithoutId,
mxClient: mockClient,
permalinkCreator,
});
// Then
expect(mockClient.sendMessage).toBeCalledTimes(0);
expect(spyDispatcher).toBeCalledTimes(0);
});
describe("calls client.sendMessage with", () => {
it("a null argument if SendMessageParams is missing relation", async () => {
// When
await sendMessage(message, true, {
roomContext: defaultRoomContext,
mxClient: mockClient,
permalinkCreator,
});
// Then
expect(mockClient.sendMessage).toHaveBeenCalledWith(expect.anything(), null, expect.anything());
});
it("a null argument if SendMessageParams has relation but relation is missing event_id", async () => {
// When
await sendMessage(message, true, {
roomContext: defaultRoomContext,
mxClient: mockClient,
permalinkCreator,
relation: {},
});
// Then
expect(mockClient.sendMessage).toBeCalledWith(expect.anything(), null, expect.anything());
});
it("a null argument if SendMessageParams has relation but rel_type does not match THREAD_RELATION_TYPE.name", async () => {
// When
await sendMessage(message, true, {
roomContext: defaultRoomContext,
mxClient: mockClient,
permalinkCreator,
relation: {
event_id: "valid_id",
rel_type: "m.does_not_match",
},
});
// Then
expect(mockClient.sendMessage).toBeCalledWith(expect.anything(), null, expect.anything());
});
it("the event_id if SendMessageParams has relation and rel_type matches THREAD_RELATION_TYPE.name", async () => {
// When
await sendMessage(message, true, {
roomContext: defaultRoomContext,
mxClient: mockClient,
permalinkCreator,
relation: {
event_id: "valid_id",
rel_type: "m.thread",
},
});
// Then
expect(mockClient.sendMessage).toBeCalledWith(expect.anything(), "valid_id", expect.anything());
});
});
2022-12-12 12:24:14 +01:00
it("Should send html message", async () => {
2022-10-10 11:16:42 +02:00
// When
2022-12-12 12:24:14 +01:00
await sendMessage(message, true, {
roomContext: defaultRoomContext,
mxClient: mockClient,
permalinkCreator,
});
2022-10-10 11:16:42 +02:00
// Then
const expectedContent = {
body: "*__hello__ world*",
2022-12-12 12:24:14 +01:00
format: "org.matrix.custom.html",
formatted_body: "<i><b>hello</b> world</i>",
msgtype: "m.text",
2022-10-10 11:16:42 +02:00
};
2022-12-12 12:24:14 +01:00
expect(mockClient.sendMessage).toBeCalledWith("myfakeroom", null, expectedContent);
expect(spyDispatcher).toBeCalledWith({ action: "message_sent" });
2022-10-10 11:16:42 +02:00
});
2022-12-12 12:24:14 +01:00
it("Should send reply to html message", async () => {
2022-10-14 15:52:05 +02:00
const mockReplyEvent = mkEvent({
type: "m.room.message",
2022-12-12 12:24:14 +01:00
room: "myfakeroom",
user: "myfakeuser2",
content: { msgtype: "m.text", body: "My reply" },
2022-10-14 15:52:05 +02:00
event: true,
});
2022-10-10 11:16:42 +02:00
// When
2022-10-25 17:26:14 +02:00
await sendMessage(message, true, {
2022-10-14 15:52:05 +02:00
roomContext: defaultRoomContext,
mxClient: mockClient,
permalinkCreator,
replyToEvent: mockReplyEvent,
});
2022-10-10 11:16:42 +02:00
// Then
2022-10-14 15:52:05 +02:00
expect(spyDispatcher).toBeCalledWith({
2022-12-12 12:24:14 +01:00
action: "reply_to_event",
2022-10-14 15:52:05 +02:00
event: null,
context: defaultRoomContext.timelineRenderingType,
});
const expectedContent = {
"body": "> <myfakeuser2> My reply\n\n*__hello__ world*",
2022-10-14 15:52:05 +02:00
"format": "org.matrix.custom.html",
2022-12-12 12:24:14 +01:00
"formatted_body":
'<mx-reply><blockquote><a href="$$permalink$$">In reply to</a>' +
' <a href="https://matrix.to/#/myfakeuser2">myfakeuser2</a>' +
"<br>My reply</blockquote></mx-reply><i><b>hello</b> world</i>",
2022-10-14 15:52:05 +02:00
"msgtype": "m.text",
"m.relates_to": {
"m.in_reply_to": {
2022-12-12 12:24:14 +01:00
event_id: mockReplyEvent.getId(),
2022-10-14 15:52:05 +02:00
},
},
};
2022-12-12 12:24:14 +01:00
expect(mockClient.sendMessage).toBeCalledWith("myfakeroom", null, expectedContent);
2022-10-10 11:16:42 +02:00
});
2022-12-12 12:24:14 +01:00
it("Should scroll to bottom after sending a html message", async () => {
2022-10-10 11:16:42 +02:00
// When
SettingsStore.setValue("scrollToBottomOnMessageSent", null, SettingLevel.DEVICE, true);
2022-12-12 12:24:14 +01:00
await sendMessage(message, true, {
roomContext: defaultRoomContext,
mxClient: mockClient,
permalinkCreator,
});
2022-10-10 11:16:42 +02:00
// Then
2022-12-12 12:24:14 +01:00
expect(spyDispatcher).toBeCalledWith({
action: "scroll_to_bottom",
timelineRenderingType: defaultRoomContext.timelineRenderingType,
});
2022-10-10 11:16:42 +02:00
});
2022-12-12 12:24:14 +01:00
it("Should handle emojis", async () => {
2022-10-10 11:16:42 +02:00
// When
2022-12-12 12:24:14 +01:00
await sendMessage("🎉", false, { roomContext: defaultRoomContext, mxClient: mockClient, permalinkCreator });
2022-10-10 11:16:42 +02:00
// Then
2022-12-12 12:24:14 +01:00
expect(spyDispatcher).toBeCalledWith({ action: "effects.confetti" });
2022-10-10 11:16:42 +02:00
});
});
2022-10-21 19:26:33 +02:00
2022-12-12 12:24:14 +01:00
describe("editMessage", () => {
2022-10-21 19:26:33 +02:00
const editorStateTransfer = new EditorStateTransfer(mockEvent);
2022-12-12 12:24:14 +01:00
it("Should cancel editing and ask for event removal when message is empty", async () => {
2022-10-21 19:26:33 +02:00
// When
2022-12-12 12:24:14 +01:00
const mockCreateRedactEventDialog = jest.spyOn(ConfirmRedactDialog, "createRedactEventDialog");
2022-10-21 19:26:33 +02:00
const mockEvent = mkEvent({
type: "m.room.message",
2022-12-12 12:24:14 +01:00
room: "myfakeroom",
user: "myfakeuser",
content: { msgtype: "m.text", body: "Replying to this" },
2022-10-21 19:26:33 +02:00
event: true,
});
const replacingEvent = mkEvent({
type: "m.room.message",
2022-12-12 12:24:14 +01:00
room: "myfakeroom",
user: "myfakeuser",
content: { msgtype: "m.text", body: "ReplacingEvent" },
2022-10-21 19:26:33 +02:00
event: true,
});
replacingEvent.setStatus(EventStatus.QUEUED);
mockEvent.makeReplaced(replacingEvent);
const editorStateTransfer = new EditorStateTransfer(mockEvent);
2022-12-12 12:24:14 +01:00
await editMessage("", { roomContext: defaultRoomContext, mxClient: mockClient, editorStateTransfer });
2022-10-21 19:26:33 +02:00
// Then
expect(mockClient.sendMessage).toBeCalledTimes(0);
expect(mockClient.cancelPendingEvent).toBeCalledTimes(1);
expect(mockCreateRedactEventDialog).toBeCalledTimes(1);
expect(spyDispatcher).toBeCalledTimes(0);
});
2022-12-12 12:24:14 +01:00
it("Should do nothing if the content is unmodified", async () => {
2022-10-21 19:26:33 +02:00
// When
2022-12-12 12:24:14 +01:00
await editMessage(mockEvent.getContent().body, {
roomContext: defaultRoomContext,
mxClient: mockClient,
editorStateTransfer,
});
2022-10-21 19:26:33 +02:00
// Then
expect(mockClient.sendMessage).toBeCalledTimes(0);
});
2022-12-12 12:24:14 +01:00
it("Should send a message when the content is modified", async () => {
2022-10-21 19:26:33 +02:00
// When
const newMessage = `${mockEvent.getContent().body} new content`;
2022-12-12 12:24:14 +01:00
await editMessage(newMessage, {
roomContext: defaultRoomContext,
mxClient: mockClient,
editorStateTransfer,
});
2022-10-21 19:26:33 +02:00
// Then
const { msgtype, format } = mockEvent.getContent();
const expectedContent = {
2022-10-25 17:26:14 +02:00
"body": ` * ${newMessage}`,
2022-10-21 19:26:33 +02:00
"formatted_body": ` * ${newMessage}`,
"m.new_content": {
2022-12-12 12:24:14 +01:00
body: "Replying to this new content",
format: "org.matrix.custom.html",
formatted_body: "Replying to this new content",
msgtype: "m.text",
2022-10-21 19:26:33 +02:00
},
"m.relates_to": {
2022-12-12 12:24:14 +01:00
event_id: mockEvent.getId(),
rel_type: "m.replace",
2022-10-21 19:26:33 +02:00
},
msgtype,
format,
};
expect(mockClient.sendMessage).toBeCalledWith(mockEvent.getRoomId(), null, expectedContent);
2022-12-12 12:24:14 +01:00
expect(spyDispatcher).toBeCalledWith({ action: "message_sent" });
2022-10-21 19:26:33 +02:00
});
});
2022-10-10 11:16:42 +02:00
});