Revert "Revert "Factor out MessageEvent.from()
usage (#9882)""
This reverts commit 99e38ca88e
.
This commit is contained in:
parent
edc44922c2
commit
19ba620de2
5 changed files with 127 additions and 61 deletions
|
@ -26,7 +26,7 @@ import {
|
|||
getBeaconInfoIdentifier,
|
||||
EventType,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { ExtensibleEvent, MessageEvent, M_POLL_KIND_DISCLOSED, PollStartEvent } from "matrix-events-sdk";
|
||||
import { 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,6 +44,7 @@ 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(),
|
||||
|
@ -64,7 +65,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does show copy link button when supplied a link", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const props = {
|
||||
link: "https://google.com/",
|
||||
};
|
||||
|
@ -75,7 +76,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does not show copy link button when not supplied a link", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const menu = createMenuWithContent(eventContent);
|
||||
const copyLinkButton = menu.find('a[aria-label="Copy link"]');
|
||||
expect(copyLinkButton).toHaveLength(0);
|
||||
|
@ -91,8 +92,8 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does not show pin option when user does not have rights to pin", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const event = new MatrixEvent(eventContent.serialize());
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const event = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent });
|
||||
|
||||
const room = makeDefaultRoom();
|
||||
// mock permission to disallow adding pinned messages to room
|
||||
|
@ -116,8 +117,12 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does not show pin option when pinning feature is disabled", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId });
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const pinnableEvent = new MatrixEvent({
|
||||
type: EventType.RoomMessage,
|
||||
content: eventContent,
|
||||
room_id: roomId,
|
||||
});
|
||||
|
||||
const room = makeDefaultRoom();
|
||||
// mock permission to allow adding pinned messages to room
|
||||
|
@ -131,8 +136,12 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("shows pin option when pinning feature is enabled", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId });
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const pinnableEvent = new MatrixEvent({
|
||||
type: EventType.RoomMessage,
|
||||
content: eventContent,
|
||||
room_id: roomId,
|
||||
});
|
||||
|
||||
const room = makeDefaultRoom();
|
||||
// mock permission to allow adding pinned messages to room
|
||||
|
@ -145,8 +154,12 @@ describe("MessageContextMenu", () => {
|
|||
|
||||
it("pins event on pin option click", () => {
|
||||
const onFinished = jest.fn();
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId });
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const pinnableEvent = new MatrixEvent({
|
||||
type: EventType.RoomMessage,
|
||||
content: eventContent,
|
||||
room_id: roomId,
|
||||
});
|
||||
pinnableEvent.event.event_id = "!3";
|
||||
const client = MatrixClientPeg.get();
|
||||
const room = makeDefaultRoom();
|
||||
|
@ -188,8 +201,12 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("unpins event on pin option click when event is pinned", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const pinnableEvent = new MatrixEvent({ ...eventContent.serialize(), room_id: roomId });
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const pinnableEvent = new MatrixEvent({
|
||||
type: EventType.RoomMessage,
|
||||
content: eventContent,
|
||||
room_id: roomId,
|
||||
});
|
||||
pinnableEvent.event.event_id = "!3";
|
||||
const client = MatrixClientPeg.get();
|
||||
const room = makeDefaultRoom();
|
||||
|
@ -231,7 +248,7 @@ describe("MessageContextMenu", () => {
|
|||
|
||||
describe("message forwarding", () => {
|
||||
it("allows forwarding a room message", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const menu = createMenuWithContent(eventContent);
|
||||
expect(menu.find('div[aria-label="Forward"]')).toHaveLength(1);
|
||||
});
|
||||
|
@ -335,7 +352,7 @@ describe("MessageContextMenu", () => {
|
|||
|
||||
describe("open as map link", () => {
|
||||
it("does not allow opening a plain message in open street maps", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const menu = createMenuWithContent(eventContent);
|
||||
expect(menu.find('a[aria-label="Open in OpenStreetMap"]')).toHaveLength(0);
|
||||
});
|
||||
|
@ -380,7 +397,7 @@ describe("MessageContextMenu", () => {
|
|||
describe("right click", () => {
|
||||
it("copy button does work as expected", () => {
|
||||
const text = "hello";
|
||||
const eventContent = MessageEvent.from(text);
|
||||
const eventContent = createMessageEventContent(text);
|
||||
mocked(getSelectedText).mockReturnValue(text);
|
||||
|
||||
const menu = createRightClickMenuWithContent(eventContent);
|
||||
|
@ -391,7 +408,7 @@ describe("MessageContextMenu", () => {
|
|||
|
||||
it("copy button is not shown when there is nothing to copy", () => {
|
||||
const text = "hello";
|
||||
const eventContent = MessageEvent.from(text);
|
||||
const eventContent = createMessageEventContent(text);
|
||||
mocked(getSelectedText).mockReturnValue("");
|
||||
|
||||
const menu = createRightClickMenuWithContent(eventContent);
|
||||
|
@ -400,7 +417,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("shows edit button when we can edit", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
mocked(canEditContent).mockReturnValue(true);
|
||||
|
||||
const menu = createRightClickMenuWithContent(eventContent);
|
||||
|
@ -409,7 +426,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does not show edit button when we cannot edit", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
mocked(canEditContent).mockReturnValue(false);
|
||||
|
||||
const menu = createRightClickMenuWithContent(eventContent);
|
||||
|
@ -418,7 +435,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("shows reply button when we can reply", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const context = {
|
||||
canSendMessages: true,
|
||||
};
|
||||
|
@ -429,11 +446,11 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does not show reply button when we cannot reply", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const context = {
|
||||
canSendMessages: true,
|
||||
};
|
||||
const unsentMessage = new MatrixEvent(eventContent.serialize());
|
||||
const unsentMessage = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent });
|
||||
// queued messages are not actionable
|
||||
unsentMessage.setStatus(EventStatus.QUEUED);
|
||||
|
||||
|
@ -443,7 +460,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("shows react button when we can react", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const context = {
|
||||
canReact: true,
|
||||
};
|
||||
|
@ -454,7 +471,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does not show react button when we cannot react", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const context = {
|
||||
canReact: false,
|
||||
};
|
||||
|
@ -465,8 +482,8 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("shows view in room button when the event is a thread root", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const mxEvent = new MatrixEvent(eventContent.serialize());
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const mxEvent = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent });
|
||||
mxEvent.getThread = () => ({ rootEvent: mxEvent } as Thread);
|
||||
const props = {
|
||||
rightClick: true,
|
||||
|
@ -481,7 +498,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("does not show view in room button when the event is not a thread root", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
|
||||
const menu = createRightClickMenuWithContent(eventContent);
|
||||
const reactButton = menu.find('div[aria-label="View in room"]');
|
||||
|
@ -489,8 +506,8 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
|
||||
it("creates a new thread on reply in thread click", () => {
|
||||
const eventContent = MessageEvent.from("hello");
|
||||
const mxEvent = new MatrixEvent(eventContent.serialize());
|
||||
const eventContent = createMessageEventContent("hello");
|
||||
const mxEvent = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent });
|
||||
|
||||
Thread.hasServerSideSupport = FeatureSupport.Stable;
|
||||
const context = {
|
||||
|
@ -513,7 +530,7 @@ describe("MessageContextMenu", () => {
|
|||
});
|
||||
});
|
||||
|
||||
function createRightClickMenuWithContent(eventContent: ExtensibleEvent, context?: Partial<IRoomState>): ReactWrapper {
|
||||
function createRightClickMenuWithContent(eventContent: object, context?: Partial<IRoomState>): ReactWrapper {
|
||||
return createMenuWithContent(eventContent, { rightClick: true }, context);
|
||||
}
|
||||
|
||||
|
@ -522,11 +539,13 @@ function createRightClickMenu(mxEvent: MatrixEvent, context?: Partial<IRoomState
|
|||
}
|
||||
|
||||
function createMenuWithContent(
|
||||
eventContent: ExtensibleEvent,
|
||||
eventContent: object,
|
||||
props?: Partial<React.ComponentProps<typeof MessageContextMenu>>,
|
||||
context?: Partial<IRoomState>,
|
||||
): ReactWrapper {
|
||||
const mxEvent = new MatrixEvent(eventContent.serialize());
|
||||
// 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 });
|
||||
return createMenu(mxEvent, props, context);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue