2022-10-21 17:26:33 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-21 17:26:33 +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-10-21 17:26:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import "@testing-library/jest-dom";
|
|
|
|
import React from "react";
|
2022-11-28 15:01:49 +00:00
|
|
|
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext";
|
|
|
|
import RoomContext from "../../../../../src/contexts/RoomContext";
|
|
|
|
import defaultDispatcher from "../../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../../src/dispatcher/actions";
|
2023-02-03 16:43:02 +00:00
|
|
|
import { flushPromises, mkEvent } from "../../../../test-utils";
|
2022-10-21 17:26:33 +00:00
|
|
|
import { EditWysiwygComposer } from "../../../../../src/components/views/rooms/wysiwyg_composer";
|
|
|
|
import EditorStateTransfer from "../../../../../src/utils/EditorStateTransfer";
|
2023-01-03 15:35:14 +00:00
|
|
|
import { Emoji } from "../../../../../src/components/views/rooms/wysiwyg_composer/components/Emoji";
|
|
|
|
import { ChevronFace } from "../../../../../src/components/structures/ContextMenu";
|
|
|
|
import { ComposerInsertPayload, ComposerType } from "../../../../../src/dispatcher/payloads/ComposerInsertPayload";
|
|
|
|
import { ActionPayload } from "../../../../../src/dispatcher/payloads";
|
|
|
|
import * as EmojiButton from "../../../../../src/components/views/rooms/EmojiButton";
|
2023-02-03 16:43:02 +00:00
|
|
|
import { createMocks } from "./utils";
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
describe("EditWysiwygComposer", () => {
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-02-01 12:12:12 +00:00
|
|
|
const { editorStateTransfer, defaultRoomContext, mockClient, mockEvent } = createMocks();
|
|
|
|
|
|
|
|
const customRender = (
|
|
|
|
disabled = false,
|
|
|
|
_editorStateTransfer = editorStateTransfer,
|
|
|
|
client = mockClient,
|
|
|
|
roomContext = defaultRoomContext,
|
|
|
|
) => {
|
2022-10-21 17:26:33 +00:00
|
|
|
return render(
|
2023-02-01 12:12:12 +00:00
|
|
|
<MatrixClientContext.Provider value={client}>
|
|
|
|
<RoomContext.Provider value={roomContext}>
|
2022-10-21 17:26:33 +00:00
|
|
|
<EditWysiwygComposer disabled={disabled} editorStateTransfer={_editorStateTransfer} />
|
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-11-19 21:15:23 +00:00
|
|
|
beforeAll(
|
|
|
|
async () => {
|
|
|
|
// Load the dynamic import
|
|
|
|
const component = customRender(false);
|
|
|
|
await component.findByRole("textbox");
|
|
|
|
component.unmount();
|
|
|
|
},
|
|
|
|
// it can take a while to load the wasm
|
|
|
|
20000,
|
|
|
|
);
|
2022-12-21 11:22:17 +00:00
|
|
|
|
2022-12-20 14:59:18 +00:00
|
|
|
it("Should not render the component when not ready", async () => {
|
|
|
|
// When
|
|
|
|
const { rerender } = customRender(false);
|
2022-12-21 11:22:17 +00:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-12-20 14:59:18 +00:00
|
|
|
|
|
|
|
rerender(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider value={{ ...defaultRoomContext, room: undefined }}>
|
|
|
|
<EditWysiwygComposer disabled={false} editorStateTransfer={editorStateTransfer} />
|
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => expect(screen.queryByRole("textbox")).toBeNull());
|
|
|
|
});
|
|
|
|
|
2022-10-21 17:26:33 +00:00
|
|
|
describe("Initialize with content", () => {
|
|
|
|
it("Should initialize useWysiwyg with html content", async () => {
|
|
|
|
// When
|
2022-11-16 15:38:00 +00:00
|
|
|
customRender(false, editorStateTransfer);
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
// Then
|
2022-12-20 14:59:18 +00:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"), {
|
|
|
|
timeout: 2000,
|
|
|
|
});
|
|
|
|
|
2022-11-16 15:38:00 +00:00
|
|
|
await waitFor(() =>
|
|
|
|
expect(screen.getByRole("textbox")).toContainHTML(mockEvent.getContent()["formatted_body"]),
|
|
|
|
);
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should initialize useWysiwyg with plain text content", async () => {
|
|
|
|
// When
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
|
|
|
content: {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Replying to this",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
2022-11-16 15:38:00 +00:00
|
|
|
customRender(false, editorStateTransfer);
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
// Then
|
2022-11-16 15:38:00 +00:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toContainHTML(mockEvent.getContent()["body"]));
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
2022-11-28 15:01:49 +00:00
|
|
|
|
|
|
|
it("Should ignore when formatted_body is not filled", async () => {
|
|
|
|
// When
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
|
|
|
content: {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Replying to this",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
|
|
|
customRender(false, editorStateTransfer);
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should strip <mx-reply> tag from initial content", async () => {
|
|
|
|
// When
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
|
|
|
content: {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Replying to this",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "<mx-reply>Reply</mx-reply>My content",
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
|
|
|
customRender(false, editorStateTransfer);
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(screen.getByRole("textbox")).not.toContainHTML("<mx-reply>Reply</mx-reply>");
|
|
|
|
expect(screen.getByRole("textbox")).toContainHTML("My content");
|
|
|
|
});
|
|
|
|
});
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Edit and save actions", () => {
|
2023-02-01 12:12:12 +00:00
|
|
|
let spyDispatcher: jest.SpyInstance<void, [payload: ActionPayload, sync?: boolean]>;
|
2022-11-16 15:38:00 +00:00
|
|
|
beforeEach(async () => {
|
2023-02-01 12:12:12 +00:00
|
|
|
spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
2022-11-16 15:38:00 +00:00
|
|
|
customRender();
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
});
|
|
|
|
|
2022-10-21 17:26:33 +00:00
|
|
|
afterEach(() => {
|
|
|
|
spyDispatcher.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should cancel edit on cancel button click", async () => {
|
|
|
|
// When
|
2022-11-16 15:38:00 +00:00
|
|
|
screen.getByText("Cancel").click();
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
// Then
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(spyDispatcher).toHaveBeenCalledWith({
|
2022-10-21 17:26:33 +00:00
|
|
|
action: Action.EditEvent,
|
|
|
|
event: null,
|
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
|
|
|
});
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(spyDispatcher).toHaveBeenCalledWith({
|
2022-10-21 17:26:33 +00:00
|
|
|
action: Action.FocusSendMessageComposer,
|
|
|
|
context: defaultRoomContext.timelineRenderingType,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should send message on save button click", async () => {
|
|
|
|
// When
|
2022-11-16 15:38:00 +00:00
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
data: "foo bar",
|
|
|
|
inputType: "insertText",
|
|
|
|
});
|
|
|
|
await waitFor(() => expect(screen.getByText("Save")).not.toHaveAttribute("disabled"));
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
// Then
|
2022-11-16 15:38:00 +00:00
|
|
|
screen.getByText("Save").click();
|
2022-10-21 17:26:33 +00:00
|
|
|
const expectedContent = {
|
2022-11-16 15:38:00 +00:00
|
|
|
"body": ` * foo bar`,
|
2022-10-21 17:26:33 +00:00
|
|
|
"format": "org.matrix.custom.html",
|
2022-11-16 15:38:00 +00:00
|
|
|
"formatted_body": ` * foo bar`,
|
2022-10-21 17:26:33 +00:00
|
|
|
"m.new_content": {
|
2022-11-16 15:38:00 +00:00
|
|
|
body: "foo bar",
|
2022-10-21 17:26:33 +00:00
|
|
|
format: "org.matrix.custom.html",
|
2022-11-16 15:38:00 +00:00
|
|
|
formatted_body: "foo bar",
|
2022-10-21 17:26:33 +00:00
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
"m.relates_to": {
|
|
|
|
event_id: mockEvent.getId(),
|
|
|
|
rel_type: "m.replace",
|
|
|
|
},
|
|
|
|
"msgtype": "m.text",
|
|
|
|
};
|
2023-01-04 12:57:09 +00:00
|
|
|
await waitFor(() =>
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(mockClient.sendMessage).toHaveBeenCalledWith(mockEvent.getRoomId(), null, expectedContent),
|
2023-01-04 12:57:09 +00:00
|
|
|
);
|
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(spyDispatcher).toHaveBeenCalledWith({ action: "message_sent" });
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should focus when receiving an Action.FocusEditMessageComposer action", async () => {
|
|
|
|
// Given we don't have focus
|
|
|
|
customRender();
|
2022-10-26 12:53:44 +00:00
|
|
|
screen.getByLabelText("Bold").focus();
|
2022-10-21 17:26:33 +00:00
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
|
|
|
|
|
|
|
// When we send the right action
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.FocusEditMessageComposer,
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then the component gets the focus
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveFocus());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should not focus when disabled", async () => {
|
|
|
|
// Given we don't have focus and we are disabled
|
|
|
|
customRender(true);
|
2022-10-26 12:53:44 +00:00
|
|
|
screen.getByLabelText("Bold").focus();
|
2022-10-21 17:26:33 +00:00
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
|
|
|
|
|
|
|
// When we send an action that would cause us to get focus
|
2022-12-23 11:34:15 +00:00
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.FocusEditMessageComposer,
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
// (Send a second event to exercise the clearTimeout logic)
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.FocusEditMessageComposer,
|
|
|
|
context: null,
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
2022-12-23 11:34:15 +00:00
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
// Then we don't get it because we are disabled
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
|
|
|
});
|
2023-01-03 15:35:14 +00:00
|
|
|
|
|
|
|
it("Should add emoji", async () => {
|
|
|
|
// When
|
|
|
|
|
|
|
|
// We are not testing here the emoji button (open modal, select emoji ...)
|
|
|
|
// Instead we are directly firing an emoji to make the test easier to write
|
|
|
|
jest.spyOn(EmojiButton, "EmojiButton").mockImplementation(
|
|
|
|
({ addEmoji }: { addEmoji: (emoji: string) => void }) => {
|
|
|
|
return (
|
|
|
|
<button aria-label="Emoji" type="button" onClick={() => addEmoji("🦫")}>
|
|
|
|
Emoji
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider value={defaultRoomContext}>
|
|
|
|
<EditWysiwygComposer editorStateTransfer={editorStateTransfer} />
|
|
|
|
<Emoji menuPosition={{ chevronFace: ChevronFace.Top }} />
|
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
// Same behavior as in RoomView.tsx
|
|
|
|
// RoomView is re-dispatching the composer messages.
|
|
|
|
// It adds the composerType fields where the value refers if the composer is in editing or not
|
|
|
|
// The listeners in the RTE ignore the message if the composerType is missing in the payload
|
2023-08-03 12:56:30 +00:00
|
|
|
const dispatcherRef = defaultDispatcher.register((payload: ActionPayload) => {
|
|
|
|
defaultDispatcher.dispatch<ComposerInsertPayload>({
|
2023-01-03 15:35:14 +00:00
|
|
|
...(payload as ComposerInsertPayload),
|
|
|
|
composerType: ComposerType.Edit,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
screen.getByLabelText("Emoji").click();
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/🦫/));
|
2023-08-03 12:56:30 +00:00
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
2023-01-03 15:35:14 +00:00
|
|
|
});
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|