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-16 23:38:00 +08:00
|
|
|
import { 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-10-27 01:05:11 +08:00
|
|
|
import { createTestClient, flushPromises, getRoomContext, mkEvent, mkStubRoom } from "../../../../test-utils";
|
2022-10-22 01:26:33 +08:00
|
|
|
import { SendWysiwygComposer } from "../../../../../src/components/views/rooms/wysiwyg_composer";
|
2022-11-04 23:36:50 +08:00
|
|
|
import { aboveLeftOf } from "../../../../../src/components/structures/ContextMenu";
|
2022-10-22 01:26:33 +08:00
|
|
|
|
|
|
|
describe('SendWysiwygComposer', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockClient = createTestClient();
|
|
|
|
const mockEvent = mkEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
room: 'myfakeroom',
|
|
|
|
user: 'myfakeuser',
|
|
|
|
content: { "msgtype": "m.text", "body": "Replying to this" },
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
const mockRoom = mkStubRoom('myfakeroom', 'myfakeroom', mockClient) as any;
|
|
|
|
mockRoom.findEventById = jest.fn(eventId => {
|
|
|
|
return eventId === mockEvent.getId() ? mockEvent : null;
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultRoomContext: IRoomState = getRoomContext(mockRoom, {});
|
|
|
|
|
2022-10-26 23:16:13 +08:00
|
|
|
const customRender = (
|
|
|
|
onChange = (_content: string) => void 0,
|
|
|
|
onSend = () => void 0,
|
|
|
|
disabled = false,
|
|
|
|
isRichTextEnabled = true) => {
|
2022-10-22 01:26:33 +08:00
|
|
|
return render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider value={defaultRoomContext}>
|
2022-11-04 23:36:50 +08:00
|
|
|
<SendWysiwygComposer onChange={onChange} onSend={onSend} disabled={disabled} isRichTextEnabled={isRichTextEnabled} menuPosition={aboveLeftOf({ top: 0, bottom: 0, right: 0 })} />
|
2022-10-22 01:26:33 +08:00
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-26 23:16:13 +08:00
|
|
|
it('Should render WysiwygComposer when isRichTextEnabled is at true', () => {
|
|
|
|
// When
|
|
|
|
customRender(jest.fn(), jest.fn(), false, true);
|
2022-10-22 01:26:33 +08:00
|
|
|
|
2022-10-26 23:16:13 +08:00
|
|
|
// Then
|
|
|
|
expect(screen.getByTestId('WysiwygComposer')).toBeTruthy();
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2022-10-26 23:16:13 +08:00
|
|
|
it('Should render PlainTextComposer when isRichTextEnabled is at false', () => {
|
|
|
|
// When
|
|
|
|
customRender(jest.fn(), jest.fn(), false, false);
|
2022-10-22 01:26:33 +08:00
|
|
|
|
2022-10-26 23:16:13 +08:00
|
|
|
// Then
|
|
|
|
expect(screen.getByTestId('PlainTextComposer')).toBeTruthy();
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
|
2022-11-16 23:38:00 +08:00
|
|
|
describe.each([
|
|
|
|
{ isRichTextEnabled: true, emptyContent: '<br>' },
|
|
|
|
{ isRichTextEnabled: false, emptyContent: '' },
|
|
|
|
])(
|
2022-10-26 23:16:13 +08:00
|
|
|
'Should focus when receiving an Action.FocusSendMessageComposer action',
|
2022-11-16 23:38:00 +08:00
|
|
|
({ isRichTextEnabled, emptyContent }) => {
|
2022-10-26 23:16:13 +08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should focus when receiving an Action.FocusSendMessageComposer action', async () => {
|
2022-11-16 23:38:00 +08:00
|
|
|
// Given we don't have focus
|
2022-10-26 23:16:13 +08:00
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
2022-11-16 23:38:00 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole('textbox')).toHaveAttribute('contentEditable', "true"));
|
2022-10-26 23:16:13 +08: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 23:38:00 +08: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 23:16:13 +08:00
|
|
|
|
|
|
|
// When we send the right action
|
|
|
|
defaultDispatcher.dispatch({
|
|
|
|
action: Action.ClearAndFocusSendMessageComposer,
|
|
|
|
context: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then the component gets the focus
|
2022-11-16 23:38:00 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(screen.getByRole('textbox')).toHaveTextContent(/^$/);
|
|
|
|
expect(screen.getByRole('textbox')).toHaveFocus();
|
|
|
|
});
|
2022-10-26 23:16:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should focus when receiving a reply_to_event action', async () => {
|
2022-11-16 23:38:00 +08:00
|
|
|
// Given we don't have focus
|
2022-10-26 23:16:13 +08:00
|
|
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
2022-11-16 23:38:00 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole('textbox')).toHaveAttribute('contentEditable', "true"));
|
2022-10-26 23:16:13 +08: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 23:38:00 +08:00
|
|
|
// Given we don't have focus and we are disabled
|
2022-10-26 23:16:13 +08: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-10-27 01:05:11 +08:00
|
|
|
await flushPromises();
|
2022-10-26 23:16:13 +08:00
|
|
|
|
|
|
|
// Then we don't get it because we are disabled
|
|
|
|
expect(screen.getByRole('textbox')).not.toHaveFocus();
|
|
|
|
});
|
2022-10-22 01:26:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|