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";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { act, fireEvent, render, screen, waitFor } from "jest-matrix-react";
|
2022-10-21 17:26:33 +00:00
|
|
|
|
2024-10-15 13:57:26 +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";
|
|
|
|
import { flushPromises } from "../../../../../test-utils";
|
|
|
|
import { SendWysiwygComposer } from "../../../../../../src/components/views/rooms/wysiwyg_composer/";
|
|
|
|
import { aboveLeftOf } from "../../../../../../src/components/structures/ContextMenu";
|
|
|
|
import { ComposerInsertPayload, ComposerType } from "../../../../../../src/dispatcher/payloads/ComposerInsertPayload";
|
|
|
|
import { setSelection } from "../../../../../../src/components/views/rooms/wysiwyg_composer/utils/selection";
|
2023-02-03 16:43:02 +00:00
|
|
|
import { createMocks } from "./utils";
|
2022-12-06 15:38:25 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../../src/components/views/rooms/EmojiButton", () => ({
|
2022-12-06 15:38:25 +00:00
|
|
|
EmojiButton: ({ addEmoji }: { addEmoji: (emoji: string) => void }) => {
|
|
|
|
return (
|
|
|
|
<button aria-label="Emoji" type="button" onClick={() => addEmoji("🦫")}>
|
|
|
|
Emoji
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}));
|
2022-10-21 17:26:33 +00:00
|
|
|
|
|
|
|
describe("SendWysiwygComposer", () => {
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-02-03 16:43:02 +00:00
|
|
|
const { defaultRoomContext, mockClient } = createMocks();
|
2022-10-21 17:26:33 +00:00
|
|
|
|
2022-12-06 15:38:25 +00:00
|
|
|
const registerId = defaultDispatcher.register((payload) => {
|
|
|
|
switch (payload.action) {
|
|
|
|
case Action.ComposerInsert: {
|
|
|
|
if (payload.composerType) break;
|
|
|
|
|
|
|
|
// re-dispatch to the correct composer
|
|
|
|
defaultDispatcher.dispatch<ComposerInsertPayload>({
|
|
|
|
...(payload as ComposerInsertPayload),
|
|
|
|
composerType: ComposerType.Send,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
defaultDispatcher.unregister(registerId);
|
|
|
|
});
|
|
|
|
|
2022-10-26 15:16:13 +00:00
|
|
|
const customRender = (
|
2022-12-06 15:45:25 +00:00
|
|
|
onChange = (_content: string): void => void 0,
|
|
|
|
onSend = (): void => void 0,
|
2022-10-26 15:16:13 +00:00
|
|
|
disabled = false,
|
2022-11-24 10:31:56 +00:00
|
|
|
isRichTextEnabled = true,
|
|
|
|
placeholder?: string,
|
|
|
|
) => {
|
2022-10-21 17:26:33 +00:00
|
|
|
return render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider value={defaultRoomContext}>
|
2022-11-24 10:31:56 +00:00
|
|
|
<SendWysiwygComposer
|
|
|
|
onChange={onChange}
|
|
|
|
onSend={onSend}
|
|
|
|
disabled={disabled}
|
|
|
|
isRichTextEnabled={isRichTextEnabled}
|
|
|
|
menuPosition={aboveLeftOf({ top: 0, bottom: 0, right: 0 })}
|
|
|
|
placeholder={placeholder}
|
|
|
|
/>
|
2022-10-21 17:26:33 +00:00
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-20 14:59:18 +00:00
|
|
|
it("Should render WysiwygComposer when isRichTextEnabled is at true", async () => {
|
2022-10-26 15:16:13 +00:00
|
|
|
// When
|
|
|
|
customRender(jest.fn(), jest.fn(), false, true);
|
2022-10-21 17:26:33 +00:00
|
|
|
|
2022-10-26 15:16:13 +00:00
|
|
|
// Then
|
2023-07-17 08:17:35 +00:00
|
|
|
expect(await screen.findByTestId("WysiwygComposer", undefined, { timeout: 5000 })).toBeInTheDocument();
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
|
|
|
|
2023-03-31 13:14:55 +00:00
|
|
|
it("Should render PlainTextComposer when isRichTextEnabled is at false", async () => {
|
2022-10-26 15:16:13 +00:00
|
|
|
// When
|
|
|
|
customRender(jest.fn(), jest.fn(), false, false);
|
2022-10-21 17:26:33 +00:00
|
|
|
|
2022-10-26 15:16:13 +00:00
|
|
|
// Then
|
2023-03-31 13:14:55 +00:00
|
|
|
expect(await screen.findByTestId("PlainTextComposer")).toBeInTheDocument();
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
|
|
|
|
2022-12-23 11:34:15 +00:00
|
|
|
describe.each([{ isRichTextEnabled: true }, { isRichTextEnabled: false }])(
|
2022-10-26 15:16:13 +00:00
|
|
|
"Should focus when receiving an Action.FocusSendMessageComposer action",
|
2022-12-23 11:34:15 +00:00
|
|
|
({ isRichTextEnabled }) => {
|
2022-10-26 15:16:13 +00:00
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should focus when receiving an Action.FocusSendMessageComposer action", async () => {
|
2022-11-16 15:38:00 +00:00
|
|
|
// Given we don't have focus
|
2022-10-26 15:16:13 +00:00
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
2022-11-16 15:38:00 +00:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-10-26 15:16:13 +00:00
|
|
|
|
|
|
|
// When we send the right action
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.FocusSendMessageComposer,
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then the component gets the focus
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveFocus());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should focus and clear when receiving an Action.ClearAndFocusSendMessageComposer", async () => {
|
2022-11-16 15:38:00 +00:00
|
|
|
// Given we don't have focus
|
|
|
|
const onChange = jest.fn();
|
|
|
|
customRender(onChange, jest.fn(), false, isRichTextEnabled);
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
|
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
data: "foo bar",
|
|
|
|
inputType: "insertText",
|
|
|
|
});
|
2022-10-26 15:16:13 +00:00
|
|
|
|
|
|
|
// When we send the right action
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.ClearAndFocusSendMessageComposer,
|
2023-01-23 14:36:40 +00:00
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
2022-10-26 15:16:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Then the component gets the focus
|
2022-11-16 15:38:00 +00:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(screen.getByRole("textbox")).toHaveTextContent(/^$/);
|
|
|
|
expect(screen.getByRole("textbox")).toHaveFocus();
|
|
|
|
});
|
2022-10-26 15:16:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should focus when receiving a reply_to_event action", async () => {
|
2022-11-16 15:38:00 +00:00
|
|
|
// Given we don't have focus
|
2022-10-26 15:16:13 +00:00
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
2022-11-16 15:38:00 +00:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-10-26 15:16:13 +00:00
|
|
|
|
|
|
|
// When we send the right action
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: "reply_to_event",
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then the component gets the focus
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveFocus());
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should not focus when disabled", async () => {
|
2022-11-16 15:38:00 +00:00
|
|
|
// Given we don't have focus and we are disabled
|
2022-10-26 15:16:13 +00:00
|
|
|
customRender(jest.fn(), jest.fn(), true, isRichTextEnabled);
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
|
|
|
|
|
|
|
// When we send an action that would cause us to get focus
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.FocusSendMessageComposer,
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
// (Send a second event to exercise the clearTimeout logic)
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.FocusSendMessageComposer,
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
2022-12-23 11:34:15 +00:00
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
2022-10-26 15:16:13 +00:00
|
|
|
|
|
|
|
// Then we don't get it because we are disabled
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
|
|
|
});
|
2022-10-21 17:26:33 +00:00
|
|
|
},
|
|
|
|
);
|
2022-11-24 10:31:56 +00:00
|
|
|
|
|
|
|
describe.each([{ isRichTextEnabled: true }, { isRichTextEnabled: false }])(
|
|
|
|
"Placeholder when %s",
|
|
|
|
({ isRichTextEnabled }) => {
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should not has placeholder", async () => {
|
|
|
|
// When
|
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveClass("mx_WysiwygComposer_Editor_content_placeholder");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should has placeholder", async () => {
|
|
|
|
// When
|
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled, "my placeholder");
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(screen.getByRole("textbox")).toHaveClass("mx_WysiwygComposer_Editor_content_placeholder");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should display or not placeholder when editor content change", async () => {
|
|
|
|
// When
|
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled, "my placeholder");
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
screen.getByRole("textbox").innerHTML = "f";
|
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
data: "f",
|
|
|
|
inputType: "insertText",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveClass(
|
|
|
|
"mx_WysiwygComposer_Editor_content_placeholder",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// When
|
|
|
|
screen.getByRole("textbox").innerHTML = "";
|
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
inputType: "deleteContentBackward",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(screen.getByRole("textbox")).toHaveClass("mx_WysiwygComposer_Editor_content_placeholder"),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2022-12-06 15:38:25 +00:00
|
|
|
|
2022-12-09 10:38:14 +00:00
|
|
|
describe.each([{ isRichTextEnabled: true }, { isRichTextEnabled: false }])(
|
2022-12-06 15:38:25 +00:00
|
|
|
"Emoji when %s",
|
|
|
|
({ isRichTextEnabled }) => {
|
|
|
|
let emojiButton: HTMLElement;
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2022-12-06 15:38:25 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
emojiButton = screen.getByLabelText("Emoji");
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should add an emoji in an empty composer", async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
// When
|
2022-12-06 15:38:25 +00:00
|
|
|
emojiButton.click();
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/🦫/));
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2022-12-06 15:38:25 +00:00
|
|
|
it("Should add an emoji in the middle of a word", async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
// When
|
2022-12-06 15:38:25 +00:00
|
|
|
screen.getByRole("textbox").focus();
|
|
|
|
screen.getByRole("textbox").innerHTML = "word";
|
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
data: "word",
|
|
|
|
inputType: "insertText",
|
2022-10-21 17:26:33 +00:00
|
|
|
});
|
|
|
|
|
2022-12-06 15:38:25 +00:00
|
|
|
const textNode = screen.getByRole("textbox").firstChild;
|
2023-01-03 15:35:14 +00:00
|
|
|
await setSelection({
|
2022-12-06 15:38:25 +00:00
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: 2,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: 2,
|
2023-01-26 10:08:23 +00:00
|
|
|
isForward: true,
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2022-12-06 15:38:25 +00:00
|
|
|
// the event is not automatically fired by jest
|
|
|
|
document.dispatchEvent(new CustomEvent("selectionchange"));
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2022-12-06 15:38:25 +00:00
|
|
|
emojiButton.click();
|
2022-12-12 11:24:14 +00:00
|
|
|
|
|
|
|
// Then
|
2022-12-06 15:38:25 +00:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/wo🦫rd/));
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2023-01-26 10:08:23 +00:00
|
|
|
|
|
|
|
it("Should add an emoji when a word is selected", async () => {
|
|
|
|
// When
|
|
|
|
screen.getByRole("textbox").focus();
|
|
|
|
screen.getByRole("textbox").innerHTML = "word";
|
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
data: "word",
|
|
|
|
inputType: "insertText",
|
|
|
|
});
|
|
|
|
|
|
|
|
const textNode = screen.getByRole("textbox").firstChild;
|
|
|
|
await setSelection({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: 3,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: 2,
|
|
|
|
isForward: false,
|
|
|
|
});
|
|
|
|
// the event is not automatically fired by jest
|
|
|
|
document.dispatchEvent(new CustomEvent("selectionchange"));
|
|
|
|
|
|
|
|
emojiButton.click();
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/wo🦫d/));
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|