diff --git a/src/components/views/rooms/SendMessageComposer.tsx b/src/components/views/rooms/SendMessageComposer.tsx index 18d6c1aba8..b6d06f26b1 100644 --- a/src/components/views/rooms/SendMessageComposer.tsx +++ b/src/components/views/rooms/SendMessageComposer.tsx @@ -104,7 +104,7 @@ export function attachRelation( export function createMessageContent( model: EditorModel, replyToEvent: MatrixEvent, - relation: IEventRelation, + relation: IEventRelation | undefined, permalinkCreator: RoomPermalinkCreator, includeReplyLegacyFallback = true, ): IContent { diff --git a/test/components/views/rooms/SendMessageComposer-test.tsx b/test/components/views/rooms/SendMessageComposer-test.tsx index 1ae7b141f4..256f37947b 100644 --- a/test/components/views/rooms/SendMessageComposer-test.tsx +++ b/test/components/views/rooms/SendMessageComposer-test.tsx @@ -17,6 +17,7 @@ limitations under the License. import React from "react"; import { act } from "react-dom/test-utils"; import { sleep } from "matrix-js-sdk/src/utils"; +import { MatrixClient } from "matrix-js-sdk/src/matrix"; import { mount } from 'enzyme'; import '../../../skinned-sdk'; // Must be first for skinning to work @@ -30,16 +31,26 @@ import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/Roo import EditorModel from "../../../../src/editor/model"; import { createPartCreator, createRenderer } from "../../../editor/mock"; import { createTestClient, mkEvent, mkStubRoom } from "../../../test-utils"; -import BasicMessageComposer from "../../../../src/components/views/rooms/BasicMessageComposer"; import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; -import MatrixToPermalinkConstructor from "../../../../src/utils/permalinks/MatrixToPermalinkConstructor"; import defaultDispatcher from "../../../../src/dispatcher/dispatcher"; import DocumentOffset from '../../../../src/editor/offset'; import { Layout } from '../../../../src/settings/enums/Layout'; import PlatformPeg from "../../../../src/PlatformPeg"; +import { IRoomState } from "../../../../src/components/structures/RoomView"; +import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks"; +import BasePlatform from "../../../../src/BasePlatform"; + +const WrapWithProviders: React.FC<{ + roomContext: IRoomState; + client: MatrixClient; +}> = ({ children, roomContext, client }) => + + { children } + +; describe('', () => { - const roomContext = { + const defaultRoomContext = { roomLoading: true, peekLoading: false, shouldPeek: true, @@ -82,7 +93,7 @@ describe('', () => { const documentOffset = new DocumentOffset(11, true); model.update("hello world", "insertText", documentOffset); - const content = createMessageContent(model, null, false, permalinkCreator); + const content = createMessageContent(model, null, undefined, permalinkCreator); expect(content).toEqual({ body: "hello world", @@ -95,7 +106,7 @@ describe('', () => { const documentOffset = new DocumentOffset(13, true); model.update("hello *world*", "insertText", documentOffset); - const content = createMessageContent(model, null, false, permalinkCreator); + const content = createMessageContent(model, null, undefined, permalinkCreator); expect(content).toEqual({ body: "hello *world*", @@ -110,7 +121,7 @@ describe('', () => { const documentOffset = new DocumentOffset(22, true); model.update("/me blinks __quickly__", "insertText", documentOffset); - const content = createMessageContent(model, null, false, permalinkCreator); + const content = createMessageContent(model, null, undefined, permalinkCreator); expect(content).toEqual({ body: "blinks __quickly__", @@ -126,7 +137,7 @@ describe('', () => { model.update("//dev/null is my favourite place", "insertText", documentOffset); - const content = createMessageContent(model, null, false, permalinkCreator); + const content = createMessageContent(model, null, undefined, permalinkCreator); expect(content).toEqual({ body: "/dev/null is my favourite place", @@ -136,13 +147,14 @@ describe('', () => { }); describe("functions correctly mounted", () => { - const mockClient = MatrixClientPeg.matrixClient = createTestClient(); - const mockRoom = mkStubRoom('myfakeroom') as any; + const mockClient = createTestClient(); + jest.spyOn(MatrixClientPeg, 'get').mockReturnValue(mockClient); + const mockRoom = mkStubRoom('myfakeroom', 'myfakeroom', mockClient) as any; const mockEvent = mkEvent({ type: "m.room.message", room: 'myfakeroom', user: 'myfakeuser', - content: "Replying to this", + content: { "msgtype": "m.text", "body": "Replying to this" }, event: true, }); mockRoom.findEventById = jest.fn(eventId => { @@ -156,45 +168,48 @@ describe('', () => { spyDispatcher.mockReset(); }); - it("renders text and placeholder correctly", () => { - const wrapper = mount( - - - - ); + const addTextToComposer = (wrapper, text) => act(() => { + // couldn't get input event on contenteditable to work + // paste works without illegal private method access + const pasteEvent = { + clipboardData: { + types: [], + files: [], + getData: type => type === "text/plain" ? text : undefined, + }, + }; + wrapper.find('[role="textbox"]').simulate('paste', pasteEvent); + wrapper.update(); + }); + + const defaultProps = { + room: mockRoom, + toggleStickerPickerOpen: jest.fn(), + permalinkCreator: new RoomPermalinkCreator(mockRoom), + }; + const getComponent = (props = {}, roomContext = defaultRoomContext, client = mockClient) => { + return mount(, { + wrappingComponent: WrapWithProviders, + wrappingComponentProps: { roomContext, client }, + }); + }; + + fit("renders text and placeholder correctly", () => { + const wrapper = getComponent({ placeholder: "placeholder string" }); expect(wrapper.find('[aria-label="placeholder string"]')).toHaveLength(1); - act(() => { - wrapper.find(BasicMessageComposer).instance().insertText("Test Text"); - wrapper.update(); - }); + addTextToComposer(wrapper, "Test Text"); expect(wrapper.text()).toBe("Test Text"); }); it("correctly persists state to and from localStorage", () => { - const wrapper = mount( - + const wrapper = getComponent({ replyToEvent: mockEvent }); - - - ); - - act(() => { - wrapper.find(BasicMessageComposer).instance().insertText("Test Text"); - wrapper.update(); - }); + addTextToComposer(wrapper, "Test Text"); + // @ts-ignore const key = wrapper.find(SendMessageComposerClass).instance().editorStateKey; expect(wrapper.text()).toBe("Test Text"); @@ -224,22 +239,11 @@ describe('', () => { }); it("persists state correctly without replyToEvent onbeforeunload", () => { - const wrapper = mount( - + const wrapper = getComponent(); - - - ); - - act(() => { - wrapper.find(BasicMessageComposer).instance().insertText("Hello World"); - wrapper.update(); - }); + addTextToComposer(wrapper, "Hello World"); + // @ts-ignore const key = wrapper.find(SendMessageComposerClass).instance().editorStateKey; expect(wrapper.text()).toBe("Hello World"); @@ -253,22 +257,13 @@ describe('', () => { }); it("persists to session history upon sending", async () => { - PlatformPeg.get = () => ({ overrideBrowserShortcuts: () => false }); + jest.spyOn(PlatformPeg, 'get').mockReturnValue( + { overrideBrowserShortcuts: () => false } as unknown as BasePlatform); - const wrapper = mount( - - - - - ); + const wrapper = getComponent({ replyToEvent: mockEvent }); + addTextToComposer(wrapper, "This is a message"); act(() => { - wrapper.find(BasicMessageComposer).instance().insertText("This is a message"); wrapper.find(".mx_SendMessageComposer").simulate("keydown", { key: "Enter" }); wrapper.update(); }); @@ -289,22 +284,14 @@ describe('', () => { }); it('correctly sets the editorStateKey for threads', () => { - const wrapper = mount( - - - - - ); + const relation = { + rel_type: "m.thread", + event_id: "myFakeThreadId", + }; + const includeReplyLegacyFallback = false; + const wrapper = getComponent({ relation, includeReplyLegacyFallback }); const instance = wrapper.find(SendMessageComposerClass).instance(); + // @ts-ignore const key = instance.editorStateKey; expect(key).toEqual('mx_cider_state_myfakeroom_myFakeThreadId'); }); diff --git a/tsconfig.json b/tsconfig.json index 64bff21d29..bbfbf020ed 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -32,5 +32,9 @@ "./test/components/structures/**/*.tsx", "./test/components/views/context_menus/**/*.ts", "./test/components/views/context_menus/**/*.tsx", + "./test/components/views/rooms/SendMessageComposer-test.tsx" , ], + "exclude": [ + "./test/end-to-end-tests/" + ] }