element-web/test/components/views/context_menus/MessageContextMenu-test.tsx

576 lines
24 KiB
TypeScript
Raw Normal View History

2022-02-17 13:57:12 +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.
*/
2022-12-12 14:24:14 +03:00
import React from "react";
import { fireEvent, render, RenderResult } from "@testing-library/react";
import {
EventStatus,
MatrixEvent,
Room,
PendingEventOrdering,
BeaconIdentifier,
Beacon,
getBeaconInfoIdentifier,
EventType,
FeatureSupport,
Thread,
M_POLL_KIND_DISCLOSED,
2022-12-12 14:24:14 +03:00
} from "matrix-js-sdk/src/matrix";
import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent";
import { mocked } from "jest-mock";
2022-02-17 13:57:12 +03:00
2022-12-12 14:24:14 +03:00
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/RoomContext";
import { IRoomState } from "../../../../src/components/structures/RoomView";
import { canEditContent } from "../../../../src/utils/EventUtils";
import { copyPlaintext, getSelectedText } from "../../../../src/utils/strings";
import MessageContextMenu from "../../../../src/components/views/context_menus/MessageContextMenu";
2022-12-12 14:24:14 +03:00
import { makeBeaconEvent, makeBeaconInfoEvent, makeLocationEvent, stubClient } from "../../../test-utils";
import dispatcher from "../../../../src/dispatcher/dispatcher";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { ReadPinsEventId } from "../../../../src/components/views/right_panel/types";
import { Action } from "../../../../src/dispatcher/actions";
2023-01-05 10:40:09 +03:00
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(),
getSelectedText: jest.fn(),
}));
jest.mock("../../../../src/utils/EventUtils", () => ({
2023-01-05 10:40:09 +03:00
...(jest.requireActual("../../../../src/utils/EventUtils") as object),
canEditContent: jest.fn(),
}));
2022-12-12 14:24:14 +03:00
jest.mock("../../../../src/dispatcher/dispatcher");
2022-12-12 14:24:14 +03:00
const roomId = "roomid";
2022-12-12 14:24:14 +03:00
describe("MessageContextMenu", () => {
beforeEach(() => {
jest.resetAllMocks();
stubClient();
});
2022-02-17 13:57:12 +03:00
2022-12-12 14:24:14 +03:00
it("does show copy link button when supplied a link", () => {
const eventContent = createMessageEventContent("hello");
const props = {
link: "https://google.com/",
};
createMenuWithContent(eventContent, props);
const copyLinkButton = document.querySelector('a[aria-label="Copy link"]');
expect(copyLinkButton).toHaveAttribute("href", props.link);
});
2022-12-12 14:24:14 +03:00
it("does not show copy link button when not supplied a link", () => {
const eventContent = createMessageEventContent("hello");
createMenuWithContent(eventContent);
const copyLinkButton = document.querySelector('a[aria-label="Copy link"]');
expect(copyLinkButton).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
describe("message pinning", () => {
beforeEach(() => {
2022-12-12 14:24:14 +03:00
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
});
afterAll(() => {
2022-12-12 14:24:14 +03:00
jest.spyOn(SettingsStore, "getValue").mockRestore();
});
2022-12-12 14:24:14 +03:00
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 room = makeDefaultRoom();
// mock permission to disallow adding pinned messages to room
2022-12-12 14:24:14 +03:00
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(false);
createMenu(event, {}, {}, undefined, room);
expect(document.querySelector('li[aria-label="Pin"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("does not show pin option for beacon_info event", () => {
const deadBeaconEvent = makeBeaconInfoEvent("@alice:server.org", roomId, { isLive: false });
const room = makeDefaultRoom();
// mock permission to allow adding pinned messages to room
2022-12-12 14:24:14 +03:00
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(true);
createMenu(deadBeaconEvent, {}, {}, undefined, room);
expect(document.querySelector('li[aria-label="Pin"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
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 room = makeDefaultRoom();
// mock permission to allow adding pinned messages to room
2022-12-12 14:24:14 +03:00
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(true);
// disable pinning feature
2022-12-12 14:24:14 +03:00
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
createMenu(pinnableEvent, {}, {}, undefined, room);
expect(document.querySelector('li[aria-label="Pin"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
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 room = makeDefaultRoom();
// mock permission to allow adding pinned messages to room
2022-12-12 14:24:14 +03:00
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(true);
createMenu(pinnableEvent, {}, {}, undefined, room);
expect(document.querySelector('li[aria-label="Pin"]')).toBeTruthy();
});
2022-12-12 14:24:14 +03:00
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,
});
2022-12-12 14:24:14 +03:00
pinnableEvent.event.event_id = "!3";
const client = MatrixClientPeg.safeGet();
const room = makeDefaultRoom();
// mock permission to allow adding pinned messages to room
2022-12-12 14:24:14 +03:00
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(true);
// mock read pins account data
2022-12-12 14:24:14 +03:00
const pinsAccountData = new MatrixEvent({ content: { event_ids: ["!1", "!2"] } });
jest.spyOn(room, "getAccountData").mockReturnValue(pinsAccountData);
createMenu(pinnableEvent, { onFinished }, {}, undefined, room);
fireEvent.click(document.querySelector('li[aria-label="Pin"]')!);
// added to account data
2022-12-12 14:24:14 +03:00
expect(client.setRoomAccountData).toHaveBeenCalledWith(roomId, ReadPinsEventId, {
event_ids: [
// from account data
2022-12-12 14:24:14 +03:00
"!1",
"!2",
pinnableEvent.getId(),
],
2022-12-12 14:24:14 +03:00
});
// add to room's pins
2022-12-12 14:24:14 +03:00
expect(client.sendStateEvent).toHaveBeenCalledWith(
roomId,
EventType.RoomPinnedEvents,
{
pinned: [pinnableEvent.getId()],
},
"",
);
expect(onFinished).toHaveBeenCalled();
});
2022-12-12 14:24:14 +03:00
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,
});
2022-12-12 14:24:14 +03:00
pinnableEvent.event.event_id = "!3";
const client = MatrixClientPeg.safeGet();
const room = makeDefaultRoom();
// make the event already pinned in the room
const pinEvent = new MatrixEvent({
type: EventType.RoomPinnedEvents,
room_id: roomId,
state_key: "",
2022-12-12 14:24:14 +03:00
content: { pinned: [pinnableEvent.getId(), "!another-event"] },
});
room.currentState.setStateEvents([pinEvent]);
// mock permission to allow adding pinned messages to room
2022-12-12 14:24:14 +03:00
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(true);
// mock read pins account data
2022-12-12 14:24:14 +03:00
const pinsAccountData = new MatrixEvent({ content: { event_ids: ["!1", "!2"] } });
jest.spyOn(room, "getAccountData").mockReturnValue(pinsAccountData);
createMenu(pinnableEvent, {}, {}, undefined, room);
fireEvent.click(document.querySelector('li[aria-label="Unpin"]')!);
expect(client.setRoomAccountData).not.toHaveBeenCalled();
// add to room's pins
expect(client.sendStateEvent).toHaveBeenCalledWith(
2022-12-12 14:24:14 +03:00
roomId,
EventType.RoomPinnedEvents,
// pinnableEvent's id removed, other pins intact
2022-12-12 14:24:14 +03:00
{ pinned: ["!another-event"] },
"",
);
});
});
2022-12-12 14:24:14 +03:00
describe("message forwarding", () => {
it("allows forwarding a room message", () => {
const eventContent = createMessageEventContent("hello");
createMenuWithContent(eventContent);
expect(document.querySelector('li[aria-label="Forward"]')).toBeTruthy();
});
2022-12-12 14:24:14 +03:00
it("does not allow forwarding a poll", () => {
const eventContent = PollStartEvent.from("why?", ["42"], M_POLL_KIND_DISCLOSED);
createMenuWithContent(eventContent);
expect(document.querySelector('li[aria-label="Forward"]')).toBeFalsy();
});
2023-01-05 10:40:09 +03:00
it("should not allow forwarding a voice broadcast", () => {
const broadcastStartEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
VoiceBroadcastInfoState.Started,
"@user:example.com",
"ABC123",
);
createMenu(broadcastStartEvent);
expect(document.querySelector('li[aria-label="Forward"]')).toBeFalsy();
2023-01-05 10:40:09 +03:00
});
2022-12-12 14:24:14 +03:00
describe("forwarding beacons", () => {
const aliceId = "@alice:server.org";
2022-12-12 14:24:14 +03:00
it("does not allow forwarding a beacon that is not live", () => {
const deadBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false });
const beacon = new Beacon(deadBeaconEvent);
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(deadBeaconEvent), beacon);
createMenu(deadBeaconEvent, {}, {}, beacons);
expect(document.querySelector('li[aria-label="Forward"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("does not allow forwarding a beacon that is not live but has a latestLocation", () => {
const deadBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false });
2022-12-12 14:24:14 +03:00
const beaconLocation = makeBeaconEvent(aliceId, {
beaconInfoId: deadBeaconEvent.getId(),
geoUri: "geo:51,41",
});
const beacon = new Beacon(deadBeaconEvent);
// @ts-ignore illegally set private prop
beacon._latestLocationEvent = beaconLocation;
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(deadBeaconEvent), beacon);
createMenu(deadBeaconEvent, {}, {}, beacons);
expect(document.querySelector('li[aria-label="Forward"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("does not allow forwarding a live beacon that does not have a latestLocation", () => {
const beaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true });
const beacon = new Beacon(beaconEvent);
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(beaconEvent), beacon);
createMenu(beaconEvent, {}, {}, beacons);
expect(document.querySelector('li[aria-label="Forward"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("allows forwarding a live beacon that has a location", () => {
const liveBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true });
2022-12-12 14:24:14 +03:00
const beaconLocation = makeBeaconEvent(aliceId, {
beaconInfoId: liveBeaconEvent.getId(),
geoUri: "geo:51,41",
});
const beacon = new Beacon(liveBeaconEvent);
// @ts-ignore illegally set private prop
beacon._latestLocationEvent = beaconLocation;
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(liveBeaconEvent), beacon);
createMenu(liveBeaconEvent, {}, {}, beacons);
expect(document.querySelector('li[aria-label="Forward"]')).toBeTruthy();
});
2022-12-12 14:24:14 +03:00
it("opens forward dialog with correct event", () => {
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
const liveBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true });
2022-12-12 14:24:14 +03:00
const beaconLocation = makeBeaconEvent(aliceId, {
beaconInfoId: liveBeaconEvent.getId(),
geoUri: "geo:51,41",
});
const beacon = new Beacon(liveBeaconEvent);
// @ts-ignore illegally set private prop
beacon._latestLocationEvent = beaconLocation;
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(liveBeaconEvent), beacon);
createMenu(liveBeaconEvent, {}, {}, beacons);
fireEvent.click(document.querySelector('li[aria-label="Forward"]')!);
// called with forwardableEvent, not beaconInfo event
2022-12-12 14:24:14 +03:00
expect(dispatchSpy).toHaveBeenCalledWith(
expect.objectContaining({
event: beaconLocation,
}),
);
});
});
});
2022-12-12 14:24:14 +03:00
describe("open as map link", () => {
it("does not allow opening a plain message in open street maps", () => {
const eventContent = createMessageEventContent("hello");
createMenuWithContent(eventContent);
expect(document.querySelector('a[aria-label="Open in OpenStreetMap"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("does not allow opening a beacon that does not have a shareable location event", () => {
const deadBeaconEvent = makeBeaconInfoEvent("@alice", roomId, { isLive: false });
const beacon = new Beacon(deadBeaconEvent);
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(deadBeaconEvent), beacon);
createMenu(deadBeaconEvent, {}, {}, beacons);
expect(document.querySelector('a[aria-label="Open in OpenStreetMap"]')).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("allows opening a location event in open street map", () => {
const locationEvent = makeLocationEvent("geo:50,50");
createMenu(locationEvent);
// exists with a href with the lat/lon from the location event
expect(document.querySelector('a[aria-label="Open in OpenStreetMap"]')).toHaveAttribute(
"href",
2022-12-12 14:24:14 +03:00
"https://www.openstreetmap.org/?mlat=50&mlon=50#map=16/50/50",
);
});
2022-12-12 14:24:14 +03:00
it("allows opening a beacon that has a shareable location event", () => {
const liveBeaconEvent = makeBeaconInfoEvent("@alice", roomId, { isLive: true });
const beaconLocation = makeBeaconEvent("@alice", {
beaconInfoId: liveBeaconEvent.getId(),
geoUri: "geo:51,41",
});
const beacon = new Beacon(liveBeaconEvent);
// @ts-ignore illegally set private prop
beacon._latestLocationEvent = beaconLocation;
const beacons = new Map<BeaconIdentifier, Beacon>();
beacons.set(getBeaconInfoIdentifier(liveBeaconEvent), beacon);
createMenu(liveBeaconEvent, {}, {}, beacons);
// exists with a href with the lat/lon from the location event
expect(document.querySelector('a[aria-label="Open in OpenStreetMap"]')).toHaveAttribute(
"href",
2022-12-12 14:24:14 +03:00
"https://www.openstreetmap.org/?mlat=51&mlon=41#map=16/51/41",
);
});
});
describe("right click", () => {
2022-12-12 14:24:14 +03:00
it("copy button does work as expected", () => {
const text = "hello";
const eventContent = createMessageEventContent(text);
mocked(getSelectedText).mockReturnValue(text);
createRightClickMenuWithContent(eventContent);
const copyButton = document.querySelector('li[aria-label="Copy"]')!;
fireEvent.mouseDown(copyButton);
expect(copyPlaintext).toHaveBeenCalledWith(text);
});
2022-12-12 14:24:14 +03:00
it("copy button is not shown when there is nothing to copy", () => {
const text = "hello";
const eventContent = createMessageEventContent(text);
mocked(getSelectedText).mockReturnValue("");
createRightClickMenuWithContent(eventContent);
const copyButton = document.querySelector('li[aria-label="Copy"]');
expect(copyButton).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("shows edit button when we can edit", () => {
const eventContent = createMessageEventContent("hello");
mocked(canEditContent).mockReturnValue(true);
createRightClickMenuWithContent(eventContent);
const editButton = document.querySelector('li[aria-label="Edit"]');
expect(editButton).toBeTruthy();
});
2022-12-12 14:24:14 +03:00
it("does not show edit button when we cannot edit", () => {
const eventContent = createMessageEventContent("hello");
mocked(canEditContent).mockReturnValue(false);
createRightClickMenuWithContent(eventContent);
const editButton = document.querySelector('li[aria-label="Edit"]');
expect(editButton).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("shows reply button when we can reply", () => {
const eventContent = createMessageEventContent("hello");
const context = {
canSendMessages: true,
};
createRightClickMenuWithContent(eventContent, context);
const replyButton = document.querySelector('li[aria-label="Reply"]');
expect(replyButton).toBeTruthy();
});
2022-12-12 14:24:14 +03:00
it("does not show reply button when we cannot reply", () => {
const eventContent = createMessageEventContent("hello");
const context = {
canSendMessages: true,
};
const unsentMessage = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent });
// queued messages are not actionable
unsentMessage.setStatus(EventStatus.QUEUED);
createMenu(unsentMessage, {}, context);
const replyButton = document.querySelector('li[aria-label="Reply"]');
expect(replyButton).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("shows react button when we can react", () => {
const eventContent = createMessageEventContent("hello");
const context = {
canReact: true,
};
createRightClickMenuWithContent(eventContent, context);
const reactButton = document.querySelector('li[aria-label="React"]');
expect(reactButton).toBeTruthy();
});
2022-12-12 14:24:14 +03:00
it("does not show react button when we cannot react", () => {
const eventContent = createMessageEventContent("hello");
const context = {
canReact: false,
};
createRightClickMenuWithContent(eventContent, context);
const reactButton = document.querySelector('li[aria-label="React"]');
expect(reactButton).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
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 });
mxEvent.getThread = () => ({ rootEvent: mxEvent }) as Thread;
const props = {
rightClick: true,
};
const context = {
timelineRenderingType: TimelineRenderingType.Thread,
};
createMenu(mxEvent, props, context);
const reactButton = document.querySelector('li[aria-label="View in room"]');
expect(reactButton).toBeTruthy();
});
2022-12-12 14:24:14 +03:00
it("does not show view in room button when the event is not a thread root", () => {
const eventContent = createMessageEventContent("hello");
createRightClickMenuWithContent(eventContent);
const reactButton = document.querySelector('li[aria-label="View in room"]');
expect(reactButton).toBeFalsy();
});
2022-12-12 14:24:14 +03:00
it("creates a new thread on reply in thread click", () => {
const eventContent = createMessageEventContent("hello");
const mxEvent = new MatrixEvent({ type: EventType.RoomMessage, content: eventContent });
Thread.hasServerSideSupport = FeatureSupport.Stable;
const context = {
canSendMessages: true,
};
2022-12-12 14:24:14 +03:00
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
createRightClickMenu(mxEvent, context);
const replyInThreadButton = document.querySelector('li[aria-label="Reply in thread"]')!;
fireEvent.click(replyInThreadButton);
expect(dispatcher.dispatch).toHaveBeenCalledWith({
action: Action.ShowThread,
rootEvent: mxEvent,
push: false,
});
});
});
2022-02-17 13:57:12 +03:00
});
function createRightClickMenuWithContent(eventContent: object, context?: Partial<IRoomState>): RenderResult {
return createMenuWithContent(eventContent, { rightClick: true }, context);
}
function createRightClickMenu(mxEvent: MatrixEvent, context?: Partial<IRoomState>): RenderResult {
return createMenu(mxEvent, { rightClick: true }, context);
}
function createMenuWithContent(
eventContent: object,
2024-08-01 15:01:05 +03:00
props?: Partial<MessageContextMenu["props"]>,
context?: Partial<IRoomState>,
): RenderResult {
// 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);
}
function makeDefaultRoom(): Room {
return new Room(roomId, MatrixClientPeg.safeGet(), "@user:example.com", {
2022-12-12 14:24:14 +03:00
pendingEventOrdering: PendingEventOrdering.Detached,
});
}
function createMenu(
mxEvent: MatrixEvent,
2024-08-01 15:01:05 +03:00
props?: Partial<MessageContextMenu["props"]>,
context: Partial<IRoomState> = {},
beacons: Map<BeaconIdentifier, Beacon> = new Map(),
room: Room = makeDefaultRoom(),
): RenderResult {
const client = MatrixClientPeg.safeGet();
2022-02-17 13:57:12 +03:00
// @ts-ignore illegally set private prop
room.currentState.beacons = beacons;
2022-02-17 13:57:12 +03:00
mxEvent.setStatus(EventStatus.SENT);
client.getUserId = jest.fn().mockReturnValue("@user:example.com");
client.getRoom = jest.fn().mockReturnValue(room);
return render(
<RoomContext.Provider value={context as IRoomState}>
<MessageContextMenu mxEvent={mxEvent} onFinished={jest.fn()} {...props} />
</RoomContext.Provider>,
2022-02-17 13:57:12 +03:00
);
}