2022-10-22 01:26:33 +08: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 "@testing-library/jest-dom";
|
|
|
|
import React from "react";
|
2022-11-28 23:01:49 +08:00
|
|
|
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
2022-10-22 01:26:33 +08: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";
|
|
|
|
import { IRoomState } from "../../../../../src/components/structures/RoomView";
|
2022-11-16 23:38:00 +08:00
|
|
|
import { createTestClient, flushPromises, getRoomContext, mkEvent, mkStubRoom } from "../../../../test-utils";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { EditWysiwygComposer } from "../../../../../src/components/views/rooms/wysiwyg_composer";
|
2022-10-22 01:26:33 +08:00
|
|
|
import EditorStateTransfer from "../../../../../src/utils/EditorStateTransfer";
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("EditWysiwygComposer", () => {
|
2022-10-22 01:26:33 +08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockClient = createTestClient();
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
2022-12-12 19:24:14 +08:00
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
2022-10-22 01:26:33 +08:00
|
|
|
content: {
|
2022-12-12 19:24:14 +08:00
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Replying to this",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "Replying <b>to</b> this new content",
|
2022-10-22 01:26:33 +08:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
2022-12-12 19:24:14 +08:00
|
|
|
const mockRoom = mkStubRoom("myfakeroom", "myfakeroom", mockClient) as any;
|
|
|
|
mockRoom.findEventById = jest.fn((eventId) => {
|
2022-10-22 01:26:33 +08:00
|
|
|
return eventId === mockEvent.getId() ? mockEvent : null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultRoomContext: IRoomState = getRoomContext(mockRoom, {});
|
|
|
|
|
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
|
|
|
|
|
|
|
const customRender = (disabled = false, _editorStateTransfer = editorStateTransfer) => {
|
|
|
|
return render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider value={defaultRoomContext}>
|
|
|
|
<EditWysiwygComposer disabled={disabled} editorStateTransfer={_editorStateTransfer} />
|
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("Initialize with content", () => {
|
|
|
|
it("Should initialize useWysiwyg with html content", async () => {
|
2022-10-22 01:26:33 +08:00
|
|
|
// When
|
2022-11-16 23:38:00 +08:00
|
|
|
customRender(false, editorStateTransfer);
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then
|
2022-11-16 23:38:00 +08:00
|
|
|
await waitFor(() =>
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(screen.getByRole("textbox")).toContainHTML(mockEvent.getContent()["formatted_body"]),
|
|
|
|
);
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Should initialize useWysiwyg with plain text content", async () => {
|
2022-10-22 01:26:33 +08:00
|
|
|
// When
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
2022-12-12 19:24:14 +08:00
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
2022-10-22 01:26:33 +08:00
|
|
|
content: {
|
2022-12-12 19:24:14 +08:00
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Replying to this",
|
2022-10-22 01:26:33 +08:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
2022-11-16 23:38:00 +08:00
|
|
|
customRender(false, editorStateTransfer);
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toContainHTML(mockEvent.getContent()["body"]));
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
2022-11-28 23:01:49 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Should ignore when formatted_body is not filled", async () => {
|
2022-11-28 23:01:49 +08:00
|
|
|
// When
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
2022-12-12 19:24:14 +08:00
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
2022-11-28 23:01:49 +08:00
|
|
|
content: {
|
2022-12-12 19:24:14 +08:00
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Replying to this",
|
|
|
|
format: "org.matrix.custom.html",
|
2022-11-28 23:01:49 +08:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
|
|
|
customRender(false, editorStateTransfer);
|
|
|
|
|
|
|
|
// Then
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-11-28 23:01:49 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Should strip <mx-reply> tag from initial content", async () => {
|
2022-11-28 23:01:49 +08:00
|
|
|
// When
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
2022-12-12 19:24:14 +08:00
|
|
|
room: "myfakeroom",
|
|
|
|
user: "myfakeuser",
|
2022-11-28 23:01:49 +08:00
|
|
|
content: {
|
2022-12-12 19:24:14 +08:00
|
|
|
msgtype: "m.text",
|
|
|
|
body: "Replying to this",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "<mx-reply>Reply</mx-reply>My content",
|
2022-11-28 23:01:49 +08:00
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
|
|
|
customRender(false, editorStateTransfer);
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-11-28 23:01:49 +08:00
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => {
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(screen.getByRole("textbox")).not.toContainHTML("<mx-reply>Reply</mx-reply>");
|
|
|
|
expect(screen.getByRole("textbox")).toContainHTML("My content");
|
2022-11-28 23:01:49 +08:00
|
|
|
});
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("Edit and save actions", () => {
|
2022-11-16 23:38:00 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
customRender();
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-11-16 23:38:00 +08:00
|
|
|
});
|
|
|
|
|
2022-10-22 01:26:33 +08:00
|
|
|
const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
afterEach(() => {
|
|
|
|
spyDispatcher.mockRestore();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Should cancel edit on cancel button click", async () => {
|
2022-10-22 01:26:33 +08:00
|
|
|
// When
|
2022-12-12 19:24:14 +08:00
|
|
|
screen.getByText("Cancel").click();
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyDispatcher).toBeCalledWith({
|
|
|
|
action: Action.EditEvent,
|
|
|
|
event: null,
|
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
|
|
|
});
|
|
|
|
expect(spyDispatcher).toBeCalledWith({
|
|
|
|
action: Action.FocusSendMessageComposer,
|
|
|
|
context: defaultRoomContext.timelineRenderingType,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Should send message on save button click", async () => {
|
2022-10-22 01:26:33 +08:00
|
|
|
// When
|
|
|
|
const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
2022-12-12 19:24:14 +08:00
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
data: "foo bar",
|
|
|
|
inputType: "insertText",
|
2022-11-16 23:38:00 +08:00
|
|
|
});
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByText("Save")).not.toHaveAttribute("disabled"));
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then
|
2022-12-12 19:24:14 +08:00
|
|
|
screen.getByText("Save").click();
|
2022-10-22 01:26:33 +08:00
|
|
|
const expectedContent = {
|
2022-11-16 23:38:00 +08:00
|
|
|
"body": ` * foo bar`,
|
2022-10-22 01:26:33 +08:00
|
|
|
"format": "org.matrix.custom.html",
|
2022-11-16 23:38:00 +08:00
|
|
|
"formatted_body": ` * foo bar`,
|
2022-10-22 01:26:33 +08:00
|
|
|
"m.new_content": {
|
2022-12-12 19:24:14 +08:00
|
|
|
body: "foo bar",
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: "foo bar",
|
|
|
|
msgtype: "m.text",
|
2022-10-22 01:26:33 +08:00
|
|
|
},
|
|
|
|
"m.relates_to": {
|
2022-12-12 19:24:14 +08:00
|
|
|
event_id: mockEvent.getId(),
|
|
|
|
rel_type: "m.replace",
|
2022-10-22 01:26:33 +08:00
|
|
|
},
|
|
|
|
"msgtype": "m.text",
|
|
|
|
};
|
|
|
|
expect(mockClient.sendMessage).toBeCalledWith(mockEvent.getRoomId(), null, expectedContent);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(spyDispatcher).toBeCalledWith({ action: "message_sent" });
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Should focus when receiving an Action.FocusEditMessageComposer action", async () => {
|
2022-10-22 01:26:33 +08:00
|
|
|
// Given we don't have focus
|
|
|
|
customRender();
|
2022-12-12 19:24:14 +08:00
|
|
|
screen.getByLabelText("Bold").focus();
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// When we send the right action
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.FocusEditMessageComposer,
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then the component gets the focus
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveFocus());
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Should not focus when disabled", async () => {
|
2022-10-22 01:26:33 +08:00
|
|
|
// Given we don't have focus and we are disabled
|
|
|
|
customRender(true);
|
2022-12-12 19:24:14 +08:00
|
|
|
screen.getByLabelText("Bold").focus();
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// When we send an action that would cause us to get focus
|
2022-11-28 23:01:49 +08:00
|
|
|
act(() => {
|
|
|
|
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-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
2022-11-16 23:38:00 +08:00
|
|
|
await flushPromises();
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then we don't get it because we are disabled
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
});
|