2022-03-04 18:53:22 +03: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 * as React from "react";
|
2022-08-02 16:10:43 +03:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-03-04 18:53:22 +03:00
|
|
|
import { mount, ReactWrapper } from "enzyme";
|
2022-09-16 12:10:33 +03:00
|
|
|
import { MatrixEvent, MsgType, RoomMember } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
|
2022-03-04 18:53:22 +03:00
|
|
|
|
|
|
|
import { createTestClient, mkEvent, mkStubRoom, stubClient } from "../../../test-utils";
|
2022-10-24 16:03:18 +03:00
|
|
|
import MessageComposer, { MessageComposer as MessageComposerClass }
|
|
|
|
from "../../../../src/components/views/rooms/MessageComposer";
|
2022-03-04 18:53:22 +03:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import RoomContext from "../../../../src/contexts/RoomContext";
|
2022-03-21 12:03:03 +03:00
|
|
|
import { IRoomState } from "../../../../src/components/structures/RoomView";
|
|
|
|
import ResizeNotifier from "../../../../src/utils/ResizeNotifier";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2022-08-11 18:51:24 +03:00
|
|
|
import { LocalRoom } from "../../../../src/models/LocalRoom";
|
|
|
|
import MessageComposerButtons from "../../../../src/components/views/rooms/MessageComposerButtons";
|
2022-09-16 12:10:33 +03:00
|
|
|
import { Features } from "../../../../src/settings/Settings";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
|
|
|
import dis from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
|
|
|
import { SendMessageComposer } from "../../../../src/components/views/rooms/SendMessageComposer";
|
|
|
|
import { E2EStatus } from "../../../../src/utils/ShieldUtils";
|
|
|
|
import { addTextToComposer } from "../../../test-utils/composer";
|
|
|
|
import UIStore, { UI_EVENTS } from "../../../../src/stores/UIStore";
|
2022-10-24 15:41:11 +03:00
|
|
|
import { SendWysiwygComposer } from "../../../../src/components/views/rooms/wysiwyg_composer";
|
2022-10-10 18:36:18 +03:00
|
|
|
|
|
|
|
// The wysiwyg fetch wasm bytes and a specific workaround is needed to make it works in a node (jest) environnement
|
|
|
|
// See https://github.com/matrix-org/matrix-wysiwyg/blob/main/platforms/web/test.setup.ts
|
|
|
|
jest.mock("@matrix-org/matrix-wysiwyg", () => ({
|
2022-10-13 17:46:47 +03:00
|
|
|
useWysiwyg: () => {
|
|
|
|
return { ref: { current: null }, isWysiwygReady: true, wysiwyg: { clear: () => void 0 },
|
2022-11-14 13:45:31 +03:00
|
|
|
actionStates: { bold: 'enabled', italic: 'enabled', underline: 'enabled', strikeThrough: 'enabled' } };
|
2022-10-10 18:36:18 +03:00
|
|
|
},
|
|
|
|
}));
|
2022-03-04 18:53:22 +03:00
|
|
|
|
|
|
|
describe("MessageComposer", () => {
|
|
|
|
stubClient();
|
|
|
|
const cli = createTestClient();
|
|
|
|
|
2022-08-11 18:51:24 +03:00
|
|
|
describe("for a Room", () => {
|
|
|
|
const room = mkStubRoom("!roomId:server", "Room 1", cli);
|
|
|
|
|
|
|
|
it("Renders a SendMessageComposer and MessageComposerButtons by default", () => {
|
|
|
|
const wrapper = wrapAndRender({ room });
|
|
|
|
|
|
|
|
expect(wrapper.find("SendMessageComposer")).toHaveLength(1);
|
|
|
|
expect(wrapper.find("MessageComposerButtons")).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Does not render a SendMessageComposer or MessageComposerButtons when user has no permission", () => {
|
|
|
|
const wrapper = wrapAndRender({ room }, false);
|
|
|
|
|
|
|
|
expect(wrapper.find("SendMessageComposer")).toHaveLength(0);
|
|
|
|
expect(wrapper.find("MessageComposerButtons")).toHaveLength(0);
|
|
|
|
expect(wrapper.find(".mx_MessageComposer_noperm_error")).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Does not render a SendMessageComposer or MessageComposerButtons when room is tombstoned", () => {
|
2022-09-16 12:10:33 +03:00
|
|
|
const wrapper = wrapAndRender({ room }, true, false, mkEvent({
|
2022-08-11 18:51:24 +03:00
|
|
|
event: true,
|
|
|
|
type: "m.room.tombstone",
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@user1:server",
|
|
|
|
skey: "",
|
|
|
|
content: {},
|
|
|
|
ts: Date.now(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(wrapper.find("SendMessageComposer")).toHaveLength(0);
|
|
|
|
expect(wrapper.find("MessageComposerButtons")).toHaveLength(0);
|
|
|
|
expect(wrapper.find(".mx_MessageComposer_roomReplaced_header")).toHaveLength(1);
|
|
|
|
});
|
2022-09-16 12:10:33 +03:00
|
|
|
|
|
|
|
describe("when receiving a »reply_to_event«", () => {
|
|
|
|
let wrapper: ReactWrapper;
|
|
|
|
let resizeNotifier: ResizeNotifier;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
resizeNotifier = {
|
|
|
|
notifyTimelineHeightChanged: jest.fn(),
|
|
|
|
} as unknown as ResizeNotifier;
|
|
|
|
wrapper = wrapAndRender({
|
|
|
|
room,
|
|
|
|
resizeNotifier,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call notifyTimelineHeightChanged() for the same context", () => {
|
|
|
|
dis.dispatch({
|
|
|
|
action: "reply_to_event",
|
2022-10-24 16:03:18 +03:00
|
|
|
context: (wrapper.instance as unknown as MessageComposerClass).context,
|
2022-09-16 12:10:33 +03:00
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(150);
|
|
|
|
expect(resizeNotifier.notifyTimelineHeightChanged).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not call notifyTimelineHeightChanged() for a different context", () => {
|
|
|
|
dis.dispatch({
|
|
|
|
action: "reply_to_event",
|
|
|
|
context: "test",
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(150);
|
|
|
|
expect(resizeNotifier.notifyTimelineHeightChanged).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// test button display depending on settings
|
|
|
|
[
|
|
|
|
{
|
|
|
|
setting: "MessageComposerInput.showStickersButton",
|
|
|
|
prop: "showStickersButton",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
setting: "MessageComposerInput.showPollsButton",
|
|
|
|
prop: "showPollsButton",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
setting: Features.VoiceBroadcast,
|
|
|
|
prop: "showVoiceBroadcastButton",
|
|
|
|
},
|
|
|
|
].forEach(({ setting, prop }) => {
|
|
|
|
[true, false].forEach((value: boolean) => {
|
|
|
|
describe(`when ${setting} = ${value}`, () => {
|
|
|
|
let wrapper: ReactWrapper;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
SettingsStore.setValue(setting, null, SettingLevel.DEVICE, value);
|
2022-10-19 16:01:14 +03:00
|
|
|
wrapper = wrapAndRender({ room });
|
2022-09-16 12:10:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`should pass the prop ${prop} = ${value}`, () => {
|
|
|
|
expect(wrapper.find(MessageComposerButtons).props()[prop]).toBe(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(`and setting ${setting} to ${!value}`, () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
// simulate settings update
|
|
|
|
await SettingsStore.setValue(setting, null, SettingLevel.DEVICE, !value);
|
|
|
|
dis.dispatch({
|
|
|
|
action: Action.SettingUpdated,
|
|
|
|
settingName: setting,
|
|
|
|
newValue: !value,
|
|
|
|
}, true);
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`should pass the prop ${prop} = ${!value}`, () => {
|
|
|
|
expect(wrapper.find(MessageComposerButtons).props()[prop]).toBe(!value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not render the send button", () => {
|
|
|
|
const wrapper = wrapAndRender({ room });
|
|
|
|
expect(wrapper.find("SendButton")).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when a message has been entered", () => {
|
|
|
|
let wrapper: ReactWrapper;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = wrapAndRender({ room });
|
|
|
|
addTextToComposer(wrapper, "Hello");
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render the send button", () => {
|
|
|
|
expect(wrapper.find("SendButton")).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("UIStore interactions", () => {
|
|
|
|
let wrapper: ReactWrapper;
|
|
|
|
let resizeCallback: Function;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(UIStore.instance, "on").mockImplementation((_event: string, listener: Function): any => {
|
|
|
|
resizeCallback = listener;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when a non-resize event occurred in UIStore", () => {
|
|
|
|
let stateBefore: any;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-10-24 16:03:18 +03:00
|
|
|
wrapper = wrapAndRender({ room }).children();
|
2022-09-16 12:10:33 +03:00
|
|
|
stateBefore = { ...wrapper.instance().state };
|
|
|
|
resizeCallback("test", {});
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not change the state", () => {
|
|
|
|
expect(wrapper.instance().state).toEqual(stateBefore);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when a resize to narrow event occurred in UIStore", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 16:03:18 +03:00
|
|
|
wrapper = wrapAndRender({ room }, true, true).children();
|
|
|
|
|
2022-09-16 12:10:33 +03:00
|
|
|
wrapper.setState({
|
|
|
|
isMenuOpen: true,
|
|
|
|
isStickerPickerOpen: true,
|
|
|
|
});
|
|
|
|
resizeCallback(UI_EVENTS.Resize, {});
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("isMenuOpen should be true", () => {
|
|
|
|
expect(wrapper.state("isMenuOpen")).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("isStickerPickerOpen should be false", () => {
|
|
|
|
expect(wrapper.state("isStickerPickerOpen")).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when a resize to non-narrow event occurred in UIStore", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-24 16:03:18 +03:00
|
|
|
wrapper = wrapAndRender({ room }, true, false).children();
|
2022-09-16 12:10:33 +03:00
|
|
|
wrapper.setState({
|
|
|
|
isMenuOpen: true,
|
|
|
|
isStickerPickerOpen: true,
|
|
|
|
});
|
|
|
|
resizeCallback(UI_EVENTS.Resize, {});
|
|
|
|
wrapper.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("isMenuOpen should be false", () => {
|
|
|
|
expect(wrapper.state("isMenuOpen")).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("isStickerPickerOpen should be false", () => {
|
|
|
|
expect(wrapper.state("isStickerPickerOpen")).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when not replying to an event", () => {
|
|
|
|
it("should pass the expected placeholder to SendMessageComposer", () => {
|
|
|
|
const wrapper = wrapAndRender({ room });
|
|
|
|
expect(wrapper.find(SendMessageComposer).props().placeholder).toBe("Send a message…");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("and an e2e status it should pass the expected placeholder to SendMessageComposer", () => {
|
|
|
|
const wrapper = wrapAndRender({
|
|
|
|
room,
|
|
|
|
e2eStatus: E2EStatus.Normal,
|
|
|
|
});
|
|
|
|
expect(wrapper.find(SendMessageComposer).props().placeholder).toBe("Send an encrypted message…");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when replying to an event", () => {
|
|
|
|
let replyToEvent: MatrixEvent;
|
|
|
|
let props: Partial<React.ComponentProps<typeof MessageComposer>>;
|
|
|
|
|
|
|
|
const checkPlaceholder = (expected: string) => {
|
|
|
|
it("should pass the expected placeholder to SendMessageComposer", () => {
|
|
|
|
const wrapper = wrapAndRender(props);
|
|
|
|
expect(wrapper.find(SendMessageComposer).props().placeholder).toBe(expected);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const setEncrypted = () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
props.e2eStatus = E2EStatus.Normal;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
replyToEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: MsgType.Text,
|
|
|
|
user: cli.getUserId(),
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
props = {
|
|
|
|
room,
|
|
|
|
replyToEvent,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("without encryption", () => {
|
|
|
|
checkPlaceholder("Send a reply…");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("with encryption", () => {
|
|
|
|
setEncrypted();
|
|
|
|
checkPlaceholder("Send an encrypted reply…");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("with a non-thread relation", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
props.relation = { rel_type: "test" };
|
|
|
|
});
|
|
|
|
|
|
|
|
checkPlaceholder("Send a reply…");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("that is a thread", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
props.relation = { rel_type: THREAD_RELATION_TYPE.name };
|
|
|
|
});
|
|
|
|
|
|
|
|
checkPlaceholder("Reply to thread…");
|
|
|
|
|
|
|
|
describe("with encryption", () => {
|
|
|
|
setEncrypted();
|
|
|
|
checkPlaceholder("Reply to encrypted thread…");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-03-04 18:53:22 +03:00
|
|
|
});
|
|
|
|
|
2022-08-11 18:51:24 +03:00
|
|
|
describe("for a LocalRoom", () => {
|
2022-09-16 12:10:33 +03:00
|
|
|
const localRoom = new LocalRoom("!room:example.com", cli, cli.getUserId()!);
|
2022-03-04 18:53:22 +03:00
|
|
|
|
2022-08-11 18:51:24 +03:00
|
|
|
it("should pass the sticker picker disabled prop", () => {
|
|
|
|
const wrapper = wrapAndRender({ room: localRoom });
|
|
|
|
expect(wrapper.find(MessageComposerButtons).props().showStickersButton).toBe(false);
|
|
|
|
});
|
2022-03-04 18:53:22 +03:00
|
|
|
});
|
2022-10-10 18:36:18 +03:00
|
|
|
|
2022-10-24 15:41:11 +03:00
|
|
|
it('should render SendWysiwygComposer', () => {
|
2022-10-10 18:36:18 +03:00
|
|
|
const room = mkStubRoom("!roomId:server", "Room 1", cli);
|
|
|
|
|
|
|
|
SettingsStore.setValue("feature_wysiwyg_composer", null, SettingLevel.DEVICE, true);
|
|
|
|
const wrapper = wrapAndRender({ room });
|
2022-10-10 18:42:15 +03:00
|
|
|
|
|
|
|
SettingsStore.setValue("feature_wysiwyg_composer", null, SettingLevel.DEVICE, false);
|
2022-10-24 15:41:11 +03:00
|
|
|
expect(wrapper.find(SendWysiwygComposer)).toBeTruthy();
|
2022-10-10 18:36:18 +03:00
|
|
|
});
|
2022-03-04 18:53:22 +03:00
|
|
|
});
|
|
|
|
|
2022-08-11 18:51:24 +03:00
|
|
|
function wrapAndRender(
|
|
|
|
props: Partial<React.ComponentProps<typeof MessageComposer>> = {},
|
|
|
|
canSendMessages = true,
|
2022-09-16 12:10:33 +03:00
|
|
|
narrow = false,
|
2022-08-11 18:51:24 +03:00
|
|
|
tombstone?: MatrixEvent,
|
|
|
|
): ReactWrapper {
|
2022-03-04 18:53:22 +03:00
|
|
|
const mockClient = MatrixClientPeg.get();
|
|
|
|
const roomId = "myroomid";
|
2022-08-11 18:51:24 +03:00
|
|
|
const room: any = props.room || {
|
2022-03-04 18:53:22 +03:00
|
|
|
currentState: undefined,
|
|
|
|
roomId,
|
|
|
|
client: mockClient,
|
|
|
|
getMember: function(userId: string): RoomMember {
|
|
|
|
return new RoomMember(roomId, userId);
|
|
|
|
},
|
|
|
|
};
|
2022-03-21 12:03:03 +03:00
|
|
|
|
|
|
|
const roomState = {
|
2022-09-16 12:10:33 +03:00
|
|
|
room,
|
|
|
|
canSendMessages,
|
|
|
|
tombstone,
|
|
|
|
narrow,
|
2022-03-21 12:03:03 +03:00
|
|
|
} as unknown as IRoomState;
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
room,
|
|
|
|
resizeNotifier: new ResizeNotifier(),
|
|
|
|
permalinkCreator: new RoomPermalinkCreator(room),
|
|
|
|
};
|
|
|
|
|
2022-03-04 18:53:22 +03:00
|
|
|
return mount(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
2022-03-21 12:03:03 +03:00
|
|
|
<RoomContext.Provider value={roomState}>
|
|
|
|
<MessageComposer {...defaultProps} {...props} />
|
2022-03-04 18:53:22 +03:00
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
}
|