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