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";
|
2023-02-01 20:12:12 +08:00
|
|
|
import { EventTimeline, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
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";
|
2023-02-01 20:12:12 +08:00
|
|
|
import {
|
|
|
|
createTestClient,
|
|
|
|
flushPromises,
|
|
|
|
getRoomContext,
|
|
|
|
mkEvent,
|
|
|
|
mkStubRoom,
|
|
|
|
mockPlatformPeg,
|
|
|
|
} from "../../../../test-utils";
|
2022-10-22 01:26:33 +08:00
|
|
|
import { EditWysiwygComposer } from "../../../../../src/components/views/rooms/wysiwyg_composer";
|
|
|
|
import EditorStateTransfer from "../../../../../src/utils/EditorStateTransfer";
|
2023-01-03 23:35:14 +08:00
|
|
|
import { Emoji } from "../../../../../src/components/views/rooms/wysiwyg_composer/components/Emoji";
|
|
|
|
import { ChevronFace } from "../../../../../src/components/structures/ContextMenu";
|
|
|
|
import dis from "../../../../../src/dispatcher/dispatcher";
|
|
|
|
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-01 20:12:12 +08:00
|
|
|
import { setSelection } from "../../../../../src/components/views/rooms/wysiwyg_composer/utils/selection";
|
|
|
|
import * as EventUtils from "../../../../../src/utils/EventUtils";
|
|
|
|
import { SubSelection } from "../../../../../src/components/views/rooms/wysiwyg_composer/types";
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
describe("EditWysiwygComposer", () => {
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-02-01 20:12:12 +08:00
|
|
|
function createMocks(eventContent = "Replying <strong>to</strong> this new content") {
|
|
|
|
const mockClient = createTestClient();
|
|
|
|
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: eventContent,
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
const mockRoom = mkStubRoom("myfakeroom", "myfakeroom", mockClient) as any;
|
|
|
|
mockRoom.findEventById = jest.fn((eventId) => {
|
|
|
|
return eventId === mockEvent.getId() ? mockEvent : null;
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
|
2023-02-01 20:12:12 +08:00
|
|
|
const defaultRoomContext: IRoomState = getRoomContext(mockRoom, {
|
|
|
|
liveTimeline: { getEvents: (): MatrixEvent[] => [] } as unknown as EventTimeline,
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
|
2023-02-01 20:12:12 +08:00
|
|
|
const editorStateTransfer = new EditorStateTransfer(mockEvent);
|
2022-10-22 01:26:33 +08:00
|
|
|
|
2023-02-01 20:12:12 +08:00
|
|
|
return { defaultRoomContext, editorStateTransfer, mockClient, mockEvent };
|
|
|
|
}
|
|
|
|
|
|
|
|
const { editorStateTransfer, defaultRoomContext, mockClient, mockEvent } = createMocks();
|
|
|
|
|
|
|
|
const customRender = (
|
|
|
|
disabled = false,
|
|
|
|
_editorStateTransfer = editorStateTransfer,
|
|
|
|
client = mockClient,
|
|
|
|
roomContext = defaultRoomContext,
|
|
|
|
) => {
|
2022-10-22 01:26:33 +08:00
|
|
|
return render(
|
2023-02-01 20:12:12 +08:00
|
|
|
<MatrixClientContext.Provider value={client}>
|
|
|
|
<RoomContext.Provider value={roomContext}>
|
2022-10-22 01:26:33 +08:00
|
|
|
<EditWysiwygComposer disabled={disabled} editorStateTransfer={_editorStateTransfer} />
|
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-21 19:22:17 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
// Load the dynamic import
|
|
|
|
customRender(false).unmount();
|
|
|
|
});
|
|
|
|
|
2022-12-20 22:59:18 +08:00
|
|
|
it("Should not render the component when not ready", async () => {
|
|
|
|
// When
|
|
|
|
const { rerender } = customRender(false);
|
2022-12-21 19:22:17 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
2022-12-20 22:59:18 +08: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-22 01:26:33 +08:00
|
|
|
describe("Initialize with content", () => {
|
|
|
|
it("Should initialize useWysiwyg with html content", async () => {
|
|
|
|
// When
|
2022-11-16 23:38:00 +08:00
|
|
|
customRender(false, editorStateTransfer);
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then
|
2022-12-20 22:59:18 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"), {
|
|
|
|
timeout: 2000,
|
|
|
|
});
|
|
|
|
|
2022-11-16 23:38:00 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
expect(screen.getByRole("textbox")).toContainHTML(mockEvent.getContent()["formatted_body"]),
|
|
|
|
);
|
2022-10-22 01:26:33 +08: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 23:38:00 +08:00
|
|
|
customRender(false, editorStateTransfer);
|
|
|
|
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(() => expect(screen.getByRole("textbox")).toContainHTML(mockEvent.getContent()["body"]));
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
2022-11-28 23:01:49 +08: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-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Edit and save actions", () => {
|
2023-02-01 20:12:12 +08:00
|
|
|
let spyDispatcher: jest.SpyInstance<void, [payload: ActionPayload, sync?: boolean]>;
|
2022-11-16 23:38:00 +08:00
|
|
|
beforeEach(async () => {
|
2023-02-01 20:12:12 +08:00
|
|
|
spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
2022-11-16 23:38:00 +08:00
|
|
|
customRender();
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
});
|
|
|
|
|
2022-10-22 01:26:33 +08:00
|
|
|
afterEach(() => {
|
|
|
|
spyDispatcher.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should cancel edit on cancel button click", async () => {
|
|
|
|
// When
|
2022-11-16 23:38:00 +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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should send message on save button click", async () => {
|
|
|
|
// When
|
2022-11-16 23:38:00 +08:00
|
|
|
fireEvent.input(screen.getByRole("textbox"), {
|
|
|
|
data: "foo bar",
|
|
|
|
inputType: "insertText",
|
|
|
|
});
|
|
|
|
await waitFor(() => expect(screen.getByText("Save")).not.toHaveAttribute("disabled"));
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then
|
2022-11-16 23:38:00 +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-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
|
|
|
msgtype: "m.text",
|
|
|
|
},
|
|
|
|
"m.relates_to": {
|
|
|
|
event_id: mockEvent.getId(),
|
|
|
|
rel_type: "m.replace",
|
|
|
|
},
|
|
|
|
"msgtype": "m.text",
|
|
|
|
};
|
2023-01-04 20:57:09 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
expect(mockClient.sendMessage).toBeCalledWith(mockEvent.getRoomId(), null, expectedContent),
|
|
|
|
);
|
|
|
|
|
2022-10-22 01:26:33 +08:00
|
|
|
expect(spyDispatcher).toBeCalledWith({ action: "message_sent" });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should focus when receiving an Action.FocusEditMessageComposer action", async () => {
|
|
|
|
// Given we don't have focus
|
|
|
|
customRender();
|
2022-10-26 20:53:44 +08:00
|
|
|
screen.getByLabelText("Bold").focus();
|
2022-10-22 01:26:33 +08: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 20:53:44 +08:00
|
|
|
screen.getByLabelText("Bold").focus();
|
2022-10-22 01:26:33 +08:00
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
|
|
|
|
|
|
|
// When we send an action that would cause us to get focus
|
2022-12-23 19:34:15 +08: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-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
2022-12-23 19:34:15 +08:00
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
// Then we don't get it because we are disabled
|
|
|
|
expect(screen.getByRole("textbox")).not.toHaveFocus();
|
|
|
|
});
|
2023-01-03 23:35:14 +08: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
|
|
|
|
const dispatcherRef = dis.register((payload: ActionPayload) => {
|
|
|
|
dis.dispatch<ComposerInsertPayload>({
|
|
|
|
...(payload as ComposerInsertPayload),
|
|
|
|
composerType: ComposerType.Edit,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
screen.getByLabelText("Emoji").click();
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveTextContent(/🦫/));
|
|
|
|
dis.unregister(dispatcherRef);
|
|
|
|
});
|
2023-02-01 20:12:12 +08:00
|
|
|
|
|
|
|
describe("Keyboard navigation", () => {
|
|
|
|
const setup = async (
|
|
|
|
editorState = editorStateTransfer,
|
|
|
|
client = createTestClient(),
|
|
|
|
roomContext = defaultRoomContext,
|
|
|
|
) => {
|
|
|
|
const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
customRender(false, editorState, client, roomContext);
|
|
|
|
await waitFor(() => expect(screen.getByRole("textbox")).toHaveAttribute("contentEditable", "true"));
|
|
|
|
return { textbox: screen.getByRole("textbox"), spyDispatcher };
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
|
|
|
|
jest.spyOn(EventUtils, "findEditableEvent").mockReturnValue(mockEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
function select(selection: SubSelection) {
|
|
|
|
return act(async () => {
|
|
|
|
await setSelection(selection);
|
|
|
|
// the event is not automatically fired by jest
|
|
|
|
document.dispatchEvent(new CustomEvent("selectionchange"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Moving up", () => {
|
|
|
|
it("Should not moving when caret is not at beginning of the text", async () => {
|
|
|
|
// When
|
|
|
|
const { textbox, spyDispatcher } = await setup();
|
|
|
|
const textNode = textbox.firstChild;
|
|
|
|
await select({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: 1,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: 2,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowUp",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyDispatcher).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should not moving when the content has changed", async () => {
|
|
|
|
// When
|
|
|
|
const { textbox, spyDispatcher } = await setup();
|
|
|
|
fireEvent.input(textbox, {
|
|
|
|
data: "word",
|
|
|
|
inputType: "insertText",
|
|
|
|
});
|
|
|
|
const textNode = textbox.firstChild;
|
|
|
|
await select({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: 0,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: 0,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowUp",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyDispatcher).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should moving up", async () => {
|
|
|
|
// When
|
|
|
|
const { textbox, spyDispatcher } = await setup();
|
|
|
|
const textNode = textbox.firstChild;
|
|
|
|
await select({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: 0,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: 0,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowUp",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(spyDispatcher).toBeCalledWith({
|
|
|
|
action: Action.EditEvent,
|
|
|
|
event: mockEvent,
|
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should moving up in list", async () => {
|
|
|
|
// When
|
|
|
|
const { mockEvent, defaultRoomContext, mockClient, editorStateTransfer } = createMocks(
|
|
|
|
"<ul><li><strong>Content</strong></li><li>Other Content</li></ul>",
|
|
|
|
);
|
|
|
|
jest.spyOn(EventUtils, "findEditableEvent").mockReturnValue(mockEvent);
|
|
|
|
const { textbox, spyDispatcher } = await setup(editorStateTransfer, mockClient, defaultRoomContext);
|
|
|
|
|
|
|
|
const textNode = textbox.firstChild;
|
|
|
|
await select({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: 0,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: 0,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowUp",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyDispatcher).toBeCalledWith({
|
|
|
|
action: Action.EditEvent,
|
|
|
|
event: mockEvent,
|
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Moving down", () => {
|
|
|
|
it("Should not moving when caret is not at the end of the text", async () => {
|
|
|
|
// When
|
|
|
|
const { textbox, spyDispatcher } = await setup();
|
|
|
|
const brNode = textbox.lastChild;
|
|
|
|
await select({
|
|
|
|
anchorNode: brNode,
|
|
|
|
anchorOffset: 0,
|
|
|
|
focusNode: brNode,
|
|
|
|
focusOffset: 0,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowDown",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyDispatcher).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should not moving when the content has changed", async () => {
|
|
|
|
// When
|
|
|
|
const { textbox, spyDispatcher } = await setup();
|
|
|
|
fireEvent.input(textbox, {
|
|
|
|
data: "word",
|
|
|
|
inputType: "insertText",
|
|
|
|
});
|
|
|
|
const brNode = textbox.lastChild;
|
|
|
|
await select({
|
|
|
|
anchorNode: brNode,
|
|
|
|
anchorOffset: 0,
|
|
|
|
focusNode: brNode,
|
|
|
|
focusOffset: 0,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowDown",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyDispatcher).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should moving down", async () => {
|
|
|
|
// When
|
|
|
|
const { textbox, spyDispatcher } = await setup();
|
|
|
|
// Skipping the BR tag
|
|
|
|
const textNode = textbox.childNodes[textbox.childNodes.length - 2];
|
|
|
|
const { length } = textNode.textContent || "";
|
|
|
|
await select({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: length,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: length,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowDown",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(spyDispatcher).toBeCalledWith({
|
|
|
|
action: Action.EditEvent,
|
|
|
|
event: mockEvent,
|
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should moving down in list", async () => {
|
|
|
|
// When
|
|
|
|
const { mockEvent, defaultRoomContext, mockClient, editorStateTransfer } = createMocks(
|
|
|
|
"<ul><li><strong>Content</strong></li><li>Other Content</li></ul>",
|
|
|
|
);
|
|
|
|
jest.spyOn(EventUtils, "findEditableEvent").mockReturnValue(mockEvent);
|
|
|
|
const { textbox, spyDispatcher } = await setup(editorStateTransfer, mockClient, defaultRoomContext);
|
|
|
|
|
|
|
|
// Skipping the BR tag and get the text node inside the last LI tag
|
|
|
|
const textNode = textbox.childNodes[textbox.childNodes.length - 2].lastChild?.lastChild || textbox;
|
|
|
|
const { length } = textNode.textContent || "";
|
|
|
|
await select({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: length,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: length,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowDown",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
expect(spyDispatcher).toBeCalledWith({
|
|
|
|
action: Action.EditEvent,
|
|
|
|
event: mockEvent,
|
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should close editing", async () => {
|
|
|
|
// When
|
|
|
|
jest.spyOn(EventUtils, "findEditableEvent").mockReturnValue(undefined);
|
|
|
|
const { textbox, spyDispatcher } = await setup();
|
|
|
|
// Skipping the BR tag
|
|
|
|
const textNode = textbox.childNodes[textbox.childNodes.length - 2];
|
|
|
|
const { length } = textNode.textContent || "";
|
|
|
|
await select({
|
|
|
|
anchorNode: textNode,
|
|
|
|
anchorOffset: length,
|
|
|
|
focusNode: textNode,
|
|
|
|
focusOffset: length,
|
|
|
|
isForward: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(textbox, {
|
|
|
|
key: "ArrowDown",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wait for event dispatch to happen
|
|
|
|
await act(async () => {
|
|
|
|
await flushPromises();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(spyDispatcher).toBeCalledWith({
|
|
|
|
action: Action.EditEvent,
|
|
|
|
event: null,
|
|
|
|
timelineRenderingType: defaultRoomContext.timelineRenderingType,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|