From 99e38ca88e084f06c45e16fce5b1994e01a5ae11 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 10 Jan 2023 09:20:10 -0700 Subject: [PATCH] Revert "Factor out `MessageEvent.from()` usage (#9882)" This reverts commit c09ca7b4eecd57c383e85aa0bc4bf384eefb2c90. --- cypress/e2e/editing/editing.spec.ts | 8 +- cypress/e2e/timeline/timeline.spec.ts | 31 ++++--- .../structures/TimelinePanel-test.tsx | 24 +++--- .../context_menus/MessageContextMenu-test.tsx | 83 +++++++------------ test/test-utils/events.ts | 42 ---------- 5 files changed, 61 insertions(+), 127 deletions(-) delete mode 100644 test/test-utils/events.ts diff --git a/cypress/e2e/editing/editing.spec.ts b/cypress/e2e/editing/editing.spec.ts index 9051a70c60..6f16a6a4cd 100644 --- a/cypress/e2e/editing/editing.spec.ts +++ b/cypress/e2e/editing/editing.spec.ts @@ -16,17 +16,15 @@ limitations under the License. /// -import type { MsgType } from "matrix-js-sdk/src/@types/event"; +import { MessageEvent } from "matrix-events-sdk"; + import type { ISendEventResponse } from "matrix-js-sdk/src/@types/requests"; import type { EventType } from "matrix-js-sdk/src/@types/event"; import { SynapseInstance } from "../../plugins/synapsedocker"; import Chainable = Cypress.Chainable; const sendEvent = (roomId: string): Chainable => { - return cy.sendEvent(roomId, null, "m.room.message" as EventType, { - msgtype: "m.text" as MsgType, - body: "Message", - }); + return cy.sendEvent(roomId, null, "m.room.message" as EventType, MessageEvent.from("Message").serialize().content); }; describe("Editing", () => { diff --git a/cypress/e2e/timeline/timeline.spec.ts b/cypress/e2e/timeline/timeline.spec.ts index 90406eae02..9eea2a5567 100644 --- a/cypress/e2e/timeline/timeline.spec.ts +++ b/cypress/e2e/timeline/timeline.spec.ts @@ -16,8 +16,10 @@ limitations under the License. /// +import { MessageEvent } from "matrix-events-sdk"; + import type { ISendEventResponse } from "matrix-js-sdk/src/@types/requests"; -import type { EventType, MsgType } from "matrix-js-sdk/src/@types/event"; +import type { EventType } from "matrix-js-sdk/src/@types/event"; import { SynapseInstance } from "../../plugins/synapsedocker"; import { SettingLevel } from "../../../src/settings/SettingLevel"; import { Layout } from "../../../src/settings/enums/Layout"; @@ -53,17 +55,12 @@ const expectAvatar = (e: JQuery, avatarUrl: string): void => { }; const sendEvent = (roomId: string, html = false): Chainable => { - const content = { - msgtype: "m.text" as MsgType, - body: "Message", - format: undefined, - formatted_body: undefined, - }; - if (html) { - content.format = "org.matrix.custom.html"; - content.formatted_body = "Message"; - } - return cy.sendEvent(roomId, null, "m.room.message" as EventType, content); + return cy.sendEvent( + roomId, + null, + "m.room.message" as EventType, + MessageEvent.from("Message", html ? "Message" : undefined).serialize().content, + ); }; describe("Timeline", () => { @@ -317,10 +314,12 @@ describe("Timeline", () => { }, }).as("preview_url"); - cy.sendEvent(roomId, null, "m.room.message" as EventType, { - msgtype: "m.text" as MsgType, - body: "https://call.element.io/", - }); + cy.sendEvent( + roomId, + null, + "m.room.message" as EventType, + MessageEvent.from("https://call.element.io/").serialize().content, + ); cy.visit("/#/room/" + roomId); cy.get(".mx_LinkPreviewWidget").should("exist").should("contain.text", "Element Call"); diff --git a/test/components/structures/TimelinePanel-test.tsx b/test/components/structures/TimelinePanel-test.tsx index 95847bb8c5..d7ff659c9d 100644 --- a/test/components/structures/TimelinePanel-test.tsx +++ b/test/components/structures/TimelinePanel-test.tsx @@ -17,6 +17,7 @@ limitations under the License. import { render, RenderResult, waitFor, screen } from "@testing-library/react"; // eslint-disable-next-line deprecate/import import { mount, ReactWrapper } from "enzyme"; +import { MessageEvent } from "matrix-events-sdk"; import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts"; import { EventTimelineSet, @@ -47,7 +48,6 @@ import SettingsStore from "../../../src/settings/SettingsStore"; import { isCallEvent } from "../../../src/components/structures/LegacyCallEventGrouper"; import { flushPromises, mkMembership, mkRoom, stubClient } from "../../test-utils"; import { mkThread } from "../../test-utils/threads"; -import { createMessageEventContent } from "../../test-utils/events"; const newReceipt = (eventId: string, userId: string, readTs: number, fullyReadTs: number): MatrixEvent => { const receiptContent = { @@ -89,8 +89,8 @@ const mockEvents = (room: Room, count = 2): MatrixEvent[] => { room_id: room.roomId, event_id: `${room.roomId}_event_${index}`, type: EventType.RoomMessage, - sender: "userId", - content: createMessageEventContent("`Event${index}`"), + user_id: "userId", + content: MessageEvent.from(`Event${index}`).serialize().content, }), ); } @@ -125,15 +125,13 @@ describe("TimelinePanel", () => { event_id: "ev0", sender: "@u2:m.org", origin_server_ts: 111, - type: EventType.RoomMessage, - content: createMessageEventContent("hello 1"), + ...MessageEvent.from("hello 1").serialize(), }); const ev1 = new MatrixEvent({ event_id: "ev1", sender: "@u2:m.org", origin_server_ts: 222, - type: EventType.RoomMessage, - content: createMessageEventContent("hello 2"), + ...MessageEvent.from("hello 2").serialize(), }); const roomId = "#room:example.com"; @@ -387,24 +385,24 @@ describe("TimelinePanel", () => { room_id: room.roomId, event_id: "event_reply_1", type: EventType.RoomMessage, - sender: "userId", - content: createMessageEventContent("ReplyEvent1"), + user_id: "userId", + content: MessageEvent.from(`ReplyEvent1`).serialize().content, }); reply2 = new MatrixEvent({ room_id: room.roomId, event_id: "event_reply_2", type: EventType.RoomMessage, - sender: "userId", - content: createMessageEventContent("ReplyEvent2"), + user_id: "userId", + content: MessageEvent.from(`ReplyEvent2`).serialize().content, }); root = new MatrixEvent({ room_id: room.roomId, event_id: "event_root_1", type: EventType.RoomMessage, - sender: "userId", - content: createMessageEventContent("RootEvent"), + user_id: "userId", + content: MessageEvent.from(`RootEvent`).serialize().content, }); const eventMap: { [key: string]: MatrixEvent } = { diff --git a/test/components/views/context_menus/MessageContextMenu-test.tsx b/test/components/views/context_menus/MessageContextMenu-test.tsx index a0ad320c5b..3fdf26832d 100644 --- a/test/components/views/context_menus/MessageContextMenu-test.tsx +++ b/test/components/views/context_menus/MessageContextMenu-test.tsx @@ -26,7 +26,7 @@ import { getBeaconInfoIdentifier, EventType, } from "matrix-js-sdk/src/matrix"; -import { M_POLL_KIND_DISCLOSED, PollStartEvent } from "matrix-events-sdk"; +import { ExtensibleEvent, MessageEvent, M_POLL_KIND_DISCLOSED, PollStartEvent } from "matrix-events-sdk"; import { FeatureSupport, Thread } from "matrix-js-sdk/src/models/thread"; import { mocked } from "jest-mock"; import { act } from "@testing-library/react"; @@ -44,7 +44,6 @@ import { ReadPinsEventId } from "../../../../src/components/views/right_panel/ty import { Action } from "../../../../src/dispatcher/actions"; import { mkVoiceBroadcastInfoStateEvent } from "../../../voice-broadcast/utils/test-utils"; import { VoiceBroadcastInfoState } from "../../../../src/voice-broadcast"; -import { createMessageEventContent } from "../../../test-utils/events"; jest.mock("../../../../src/utils/strings", () => ({ copyPlaintext: jest.fn(), @@ -65,7 +64,7 @@ describe("MessageContextMenu", () => { }); it("does show copy link button when supplied a link", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const props = { link: "https://google.com/", }; @@ -76,7 +75,7 @@ describe("MessageContextMenu", () => { }); it("does not show copy link button when not supplied a link", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const menu = createMenuWithContent(eventContent); const copyLinkButton = menu.find('a[aria-label="Copy link"]'); expect(copyLinkButton).toHaveLength(0); @@ -92,8 +91,8 @@ describe("MessageContextMenu", () => { }); it("does not show pin option when user does not have rights to pin", () => { - const eventContent = createMessageEventContent("hello"); - const event = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent }); + const eventContent = MessageEvent.from("hello"); + const event = new MatrixEvent(eventContent.serialize()); const room = makeDefaultRoom(); // mock permission to disallow adding pinned messages to room @@ -117,12 +116,8 @@ describe("MessageContextMenu", () => { }); it("does not show pin option when pinning feature is disabled", () => { - const eventContent = createMessageEventContent("hello"); - const pinnableEvent = new MatrixEvent({ - type: EventType.RoomMessage, - content: eventContent, - room_id: roomId, - }); + const eventContent = MessageEvent.from("hello"); + const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId }); const room = makeDefaultRoom(); // mock permission to allow adding pinned messages to room @@ -136,12 +131,8 @@ describe("MessageContextMenu", () => { }); it("shows pin option when pinning feature is enabled", () => { - const eventContent = createMessageEventContent("hello"); - const pinnableEvent = new MatrixEvent({ - type: EventType.RoomMessage, - content: eventContent, - room_id: roomId, - }); + const eventContent = MessageEvent.from("hello"); + const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId }); const room = makeDefaultRoom(); // mock permission to allow adding pinned messages to room @@ -154,12 +145,8 @@ describe("MessageContextMenu", () => { it("pins event on pin option click", () => { const onFinished = jest.fn(); - const eventContent = createMessageEventContent("hello"); - const pinnableEvent = new MatrixEvent({ - type: EventType.RoomMessage, - content: eventContent, - room_id: roomId, - }); + const eventContent = MessageEvent.from("hello"); + const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId }); pinnableEvent.event.event_id = "!3"; const client = MatrixClientPeg.get(); const room = makeDefaultRoom(); @@ -201,12 +188,8 @@ describe("MessageContextMenu", () => { }); it("unpins event on pin option click when event is pinned", () => { - const eventContent = createMessageEventContent("hello"); - const pinnableEvent = new MatrixEvent({ - type: EventType.RoomMessage, - content: eventContent, - room_id: roomId, - }); + const eventContent = MessageEvent.from("hello"); + const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId }); pinnableEvent.event.event_id = "!3"; const client = MatrixClientPeg.get(); const room = makeDefaultRoom(); @@ -248,7 +231,7 @@ describe("MessageContextMenu", () => { describe("message forwarding", () => { it("allows forwarding a room message", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const menu = createMenuWithContent(eventContent); expect(menu.find('div[aria-label="Forward"]')).toHaveLength(1); }); @@ -352,7 +335,7 @@ describe("MessageContextMenu", () => { describe("open as map link", () => { it("does not allow opening a plain message in open street maps", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const menu = createMenuWithContent(eventContent); expect(menu.find('a[aria-label="Open in OpenStreetMap"]')).toHaveLength(0); }); @@ -397,7 +380,7 @@ describe("MessageContextMenu", () => { describe("right click", () => { it("copy button does work as expected", () => { const text = "hello"; - const eventContent = createMessageEventContent(text); + const eventContent = MessageEvent.from(text); mocked(getSelectedText).mockReturnValue(text); const menu = createRightClickMenuWithContent(eventContent); @@ -408,7 +391,7 @@ describe("MessageContextMenu", () => { it("copy button is not shown when there is nothing to copy", () => { const text = "hello"; - const eventContent = createMessageEventContent(text); + const eventContent = MessageEvent.from(text); mocked(getSelectedText).mockReturnValue(""); const menu = createRightClickMenuWithContent(eventContent); @@ -417,7 +400,7 @@ describe("MessageContextMenu", () => { }); it("shows edit button when we can edit", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); mocked(canEditContent).mockReturnValue(true); const menu = createRightClickMenuWithContent(eventContent); @@ -426,7 +409,7 @@ describe("MessageContextMenu", () => { }); it("does not show edit button when we cannot edit", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); mocked(canEditContent).mockReturnValue(false); const menu = createRightClickMenuWithContent(eventContent); @@ -435,7 +418,7 @@ describe("MessageContextMenu", () => { }); it("shows reply button when we can reply", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const context = { canSendMessages: true, }; @@ -446,11 +429,11 @@ describe("MessageContextMenu", () => { }); it("does not show reply button when we cannot reply", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const context = { canSendMessages: true, }; - const unsentMessage = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent }); + const unsentMessage = new MatrixEvent(eventContent.serialize()); // queued messages are not actionable unsentMessage.setStatus(EventStatus.QUEUED); @@ -460,7 +443,7 @@ describe("MessageContextMenu", () => { }); it("shows react button when we can react", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const context = { canReact: true, }; @@ -471,7 +454,7 @@ describe("MessageContextMenu", () => { }); it("does not show react button when we cannot react", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const context = { canReact: false, }; @@ -482,8 +465,8 @@ describe("MessageContextMenu", () => { }); it("shows view in room button when the event is a thread root", () => { - const eventContent = createMessageEventContent("hello"); - const mxEvent = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent }); + const eventContent = MessageEvent.from("hello"); + const mxEvent = new MatrixEvent(eventContent.serialize()); mxEvent.getThread = () => ({ rootEvent: mxEvent } as Thread); const props = { rightClick: true, @@ -498,7 +481,7 @@ describe("MessageContextMenu", () => { }); it("does not show view in room button when the event is not a thread root", () => { - const eventContent = createMessageEventContent("hello"); + const eventContent = MessageEvent.from("hello"); const menu = createRightClickMenuWithContent(eventContent); const reactButton = menu.find('div[aria-label="View in room"]'); @@ -506,8 +489,8 @@ describe("MessageContextMenu", () => { }); it("creates a new thread on reply in thread click", () => { - const eventContent = createMessageEventContent("hello"); - const mxEvent = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent }); + const eventContent = MessageEvent.from("hello"); + const mxEvent = new MatrixEvent(eventContent.serialize()); Thread.hasServerSideSupport = FeatureSupport.Stable; const context = { @@ -530,7 +513,7 @@ describe("MessageContextMenu", () => { }); }); -function createRightClickMenuWithContent(eventContent: object, context?: Partial): ReactWrapper { +function createRightClickMenuWithContent(eventContent: ExtensibleEvent, context?: Partial): ReactWrapper { return createMenuWithContent(eventContent, { rightClick: true }, context); } @@ -539,13 +522,11 @@ function createRightClickMenu(mxEvent: MatrixEvent, context?: Partial>, context?: Partial, ): ReactWrapper { - // XXX: We probably shouldn't be assuming all events are going to be message events, but considering this - // test is for the Message context menu, it's a fairly safe assumption. - const mxEvent = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent }); + const mxEvent = new MatrixEvent(eventContent.serialize()); return createMenu(mxEvent, props, context); } diff --git a/test/test-utils/events.ts b/test/test-utils/events.ts deleted file mode 100644 index 28189ffd8d..0000000000 --- a/test/test-utils/events.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* -Copyright 2023 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 { MsgType } from "matrix-js-sdk/src/@types/event"; - -interface MessageContent { - msgtype: MsgType; - body: string; - format?: string; - formatted_body?: string; -} - -/** - * Creates the `content` for an `m.room.message` event based on input. - * @param text The text to put in the event. - * @param html Optional HTML to put in the event. - * @returns A complete `content` object for an `m.room.message` event. - */ -export function createMessageEventContent(text: string, html?: string): MessageContent { - const content: MessageContent = { - msgtype: MsgType.Text, - body: text, - }; - if (html) { - content.format = "org.matrix.custom.html"; - content.formatted_body = html; - } - return content; -}