2022-06-02 08:25:56 +00:00
|
|
|
/*
|
2023-02-20 14:46:07 +00:00
|
|
|
Copyright 2022 - 2023 The Matrix.org Foundation C.I.C.
|
2022-06-02 08:25:56 +00:00
|
|
|
|
|
|
|
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 11:24:14 +00:00
|
|
|
import React from "react";
|
2023-01-05 07:40:09 +00:00
|
|
|
import { act, render, fireEvent } from "@testing-library/react";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { EventType, EventStatus, MatrixEvent, MatrixEventEvent, MsgType, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { FeatureSupport, Thread } from "matrix-js-sdk/src/models/thread";
|
|
|
|
|
|
|
|
import MessageActionBar from "../../../../src/components/views/messages/MessageActionBar";
|
2022-06-02 08:25:56 +00:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
mockClientMethodsUser,
|
|
|
|
mockClientMethodsEvents,
|
2022-06-02 15:43:19 +00:00
|
|
|
makeBeaconInfoEvent,
|
2022-12-12 11:24:14 +00:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/RoomContext";
|
|
|
|
import { IRoomState } from "../../../../src/components/structures/RoomView";
|
|
|
|
import dispatcher from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
|
|
|
|
|
|
|
jest.mock("../../../../src/dispatcher/dispatcher");
|
|
|
|
|
|
|
|
describe("<MessageActionBar />", () => {
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const roomId = "!room:server.org";
|
2022-06-02 08:25:56 +00:00
|
|
|
const alicesMessageEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "Hello",
|
2022-06-02 08:25:56 +00:00
|
|
|
},
|
2022-07-15 19:55:11 +00:00
|
|
|
event_id: "$alices_message",
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const bobsMessageEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
2022-12-12 11:24:14 +00:00
|
|
|
sender: "@bob:server.org",
|
2022-06-02 08:25:56 +00:00
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "I am bob",
|
2022-06-02 08:25:56 +00:00
|
|
|
},
|
2022-07-15 19:55:11 +00:00
|
|
|
event_id: "$bobs_message",
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const redactedEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: userId,
|
|
|
|
});
|
|
|
|
redactedEvent.makeRedacted(redactedEvent);
|
|
|
|
|
|
|
|
const client = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(userId),
|
|
|
|
...mockClientMethodsEvents(),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
});
|
2022-07-15 19:55:11 +00:00
|
|
|
|
|
|
|
const localStorageMock = (() => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let store: Record<string, any> = {};
|
2022-07-15 19:55:11 +00:00
|
|
|
return {
|
2022-12-12 11:24:14 +00:00
|
|
|
getItem: jest.fn().mockImplementation((key) => store[key] ?? null),
|
2022-07-15 19:55:11 +00:00
|
|
|
setItem: jest.fn().mockImplementation((key, value) => {
|
|
|
|
store[key] = value;
|
|
|
|
}),
|
|
|
|
clear: jest.fn().mockImplementation(() => {
|
|
|
|
store = {};
|
|
|
|
}),
|
|
|
|
removeItem: jest.fn().mockImplementation((key) => delete store[key]),
|
|
|
|
};
|
|
|
|
})();
|
2022-12-12 11:24:14 +00:00
|
|
|
Object.defineProperty(window, "localStorage", {
|
2022-07-15 19:55:11 +00:00
|
|
|
value: localStorageMock,
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
|
2022-06-02 08:25:56 +00:00
|
|
|
const room = new Room(roomId, client, userId);
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(room, "getPendingEvents").mockReturnValue([]);
|
2022-06-02 08:25:56 +00:00
|
|
|
|
|
|
|
client.getRoom.mockReturnValue(room);
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
getTile: jest.fn(),
|
|
|
|
getReplyChain: jest.fn(),
|
|
|
|
toggleThreadExpanded: jest.fn(),
|
|
|
|
mxEvent: alicesMessageEvent,
|
|
|
|
permalinkCreator: new RoomPermalinkCreator(room),
|
|
|
|
};
|
|
|
|
const defaultRoomContext = {
|
|
|
|
...RoomContext,
|
|
|
|
timelineRenderingType: TimelineRenderingType.Room,
|
|
|
|
canSendMessages: true,
|
|
|
|
canReact: true,
|
|
|
|
} as unknown as IRoomState;
|
|
|
|
const getComponent = (props = {}, roomContext: Partial<IRoomState> = {}) =>
|
|
|
|
render(
|
|
|
|
<RoomContext.Provider value={{ ...defaultRoomContext, ...roomContext }}>
|
|
|
|
<MessageActionBar {...defaultProps} {...props} />
|
2022-12-12 11:24:14 +00:00
|
|
|
</RoomContext.Provider>,
|
|
|
|
);
|
2022-06-02 08:25:56 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
alicesMessageEvent.setStatus(EventStatus.SENT);
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
|
|
|
jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined);
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockRestore();
|
|
|
|
jest.spyOn(SettingsStore, "setValue").mockRestore();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("kills event listeners on unmount", () => {
|
|
|
|
const offSpy = jest.spyOn(alicesMessageEvent, "off").mockClear();
|
2022-06-02 08:25:56 +00:00
|
|
|
const wrapper = getComponent({ mxEvent: alicesMessageEvent });
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(offSpy.mock.calls[0][0]).toEqual(MatrixEventEvent.Status);
|
|
|
|
expect(offSpy.mock.calls[1][0]).toEqual(MatrixEventEvent.Decrypted);
|
|
|
|
expect(offSpy.mock.calls[2][0]).toEqual(MatrixEventEvent.BeforeRedaction);
|
|
|
|
|
|
|
|
expect(client.decryptEventIfNeeded).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("decryption", () => {
|
|
|
|
it("decrypts event if needed", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
getComponent({ mxEvent: alicesMessageEvent });
|
|
|
|
expect(client.decryptEventIfNeeded).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates component on decrypted event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const decryptingEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessageEncrypted,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(decryptingEvent, "isBeingDecrypted").mockReturnValue(true);
|
2022-06-02 08:25:56 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: decryptingEvent });
|
|
|
|
|
|
|
|
// still encrypted event is not actionable => no reply button
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply")).toBeFalsy();
|
2022-06-02 08:25:56 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// ''decrypt'' the event
|
|
|
|
decryptingEvent.event.type = alicesMessageEvent.getType();
|
|
|
|
decryptingEvent.event.content = alicesMessageEvent.getContent();
|
|
|
|
decryptingEvent.emit(MatrixEventEvent.Decrypted, decryptingEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
// new available actions after decryption
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("status", () => {
|
|
|
|
it("updates component when event status changes", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
alicesMessageEvent.setStatus(EventStatus.QUEUED);
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
|
|
|
|
|
|
|
// pending event status, cancel action available
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Delete")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
alicesMessageEvent.setStatus(EventStatus.SENT);
|
|
|
|
});
|
|
|
|
|
|
|
|
// event is sent, no longer cancelable
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Delete")).toBeFalsy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("redaction", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
// this doesn't do what it's supposed to
|
|
|
|
// because beforeRedaction event is fired... before redaction
|
|
|
|
// event is unchanged at point when this component updates
|
|
|
|
// TODO file bug
|
2023-03-01 15:23:35 +00:00
|
|
|
it.skip("updates component on before redaction event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "Hello",
|
2022-06-02 08:25:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: event });
|
|
|
|
|
|
|
|
// no pending redaction => no delete button
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Delete")).toBeFalsy();
|
2022-06-02 08:25:56 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
const redactionEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomRedaction,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
});
|
|
|
|
redactionEvent.setStatus(EventStatus.QUEUED);
|
|
|
|
event.markLocallyRedacted(redactionEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
// updated with local redaction event, delete now available
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Delete")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("options button", () => {
|
|
|
|
it("renders options menu", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Options")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("opens message context menu on click", () => {
|
2022-08-18 07:18:18 +00:00
|
|
|
const { getByTestId, queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(queryByLabelText("Options")!);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(getByTestId("mx_MessageContextMenu")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("reply button", () => {
|
|
|
|
it("renders reply button on own actionable event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders reply button on others actionable event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: bobsMessageEvent }, { canSendMessages: true });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render reply button on non-actionable event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
// redacted event is not actionable
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: redactedEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply")).toBeFalsy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render reply button when user cannot send messaged", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
// redacted event is not actionable
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: redactedEvent }, { canSendMessages: false });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply")).toBeFalsy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("dispatches reply event on click", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(queryByLabelText("Reply")!);
|
2022-06-02 08:25:56 +00:00
|
|
|
|
|
|
|
expect(dispatcher.dispatch).toHaveBeenCalledWith({
|
2022-12-12 11:24:14 +00:00
|
|
|
action: "reply_to_event",
|
2022-06-02 08:25:56 +00:00
|
|
|
event: alicesMessageEvent,
|
|
|
|
context: TimelineRenderingType.Room,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("react button", () => {
|
|
|
|
it("renders react button on own actionable event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("React")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders react button on others actionable event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: bobsMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("React")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render react button on non-actionable event", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
// redacted event is not actionable
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: redactedEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("React")).toBeFalsy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render react button when user cannot react", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
// redacted event is not actionable
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: redactedEvent }, { canReact: false });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("React")).toBeFalsy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("opens reaction picker on click", () => {
|
2022-08-18 07:18:18 +00:00
|
|
|
const { queryByLabelText, getByTestId } = getComponent({ mxEvent: alicesMessageEvent });
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(queryByLabelText("React")!);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(getByTestId("mx_EmojiPicker")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("cancel button", () => {
|
|
|
|
it("renders cancel button for an event with a cancelable status", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
alicesMessageEvent.setStatus(EventStatus.QUEUED);
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Delete")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders cancel button for an event with a pending edit", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "Hello",
|
2022-06-02 08:25:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
event.setStatus(EventStatus.SENT);
|
|
|
|
const replacingEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "replacing event body",
|
2022-06-02 08:25:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
replacingEvent.setStatus(EventStatus.QUEUED);
|
|
|
|
event.makeReplaced(replacingEvent);
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: event });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Delete")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders cancel button for an event with a pending redaction", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "Hello",
|
2022-06-02 08:25:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
event.setStatus(EventStatus.SENT);
|
|
|
|
|
|
|
|
const redactionEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomRedaction,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
});
|
|
|
|
redactionEvent.setStatus(EventStatus.QUEUED);
|
|
|
|
|
|
|
|
event.markLocallyRedacted(redactionEvent);
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: event });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Delete")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders cancel and retry button for an event with NOT_SENT status", () => {
|
2022-06-02 08:25:56 +00:00
|
|
|
alicesMessageEvent.setStatus(EventStatus.NOT_SENT);
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Retry")).toBeTruthy();
|
|
|
|
expect(queryByLabelText("Delete")).toBeTruthy();
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it.todo("unsends event on cancel click");
|
|
|
|
it.todo("retrys event on retry click");
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|
2022-06-02 15:43:19 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("thread button", () => {
|
2022-06-02 15:43:19 +00:00
|
|
|
beforeEach(() => {
|
2022-10-06 08:35:06 +00:00
|
|
|
Thread.setServerSideSupport(FeatureSupport.Stable);
|
2022-06-02 15:43:19 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("when threads feature is enabled", () => {
|
|
|
|
it("renders thread button on own actionable event", () => {
|
2022-06-02 15:43:19 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply in thread")).toBeTruthy();
|
2022-06-02 15:43:19 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render thread button for a beacon_info event", () => {
|
2022-06-02 15:43:19 +00:00
|
|
|
const beaconInfoEvent = makeBeaconInfoEvent(userId, roomId);
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: beaconInfoEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Reply in thread")).toBeFalsy();
|
2022-06-02 15:43:19 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("opens thread on click", () => {
|
2022-06-02 15:43:19 +00:00
|
|
|
const { getByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(getByLabelText("Reply in thread"));
|
2022-06-02 15:43:19 +00:00
|
|
|
|
2022-07-23 12:13:49 +00:00
|
|
|
expect(dispatcher.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ShowThread,
|
2022-06-02 15:43:19 +00:00
|
|
|
rootEvent: alicesMessageEvent,
|
|
|
|
push: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("opens parent thread for a thread reply message", () => {
|
2022-06-02 15:43:19 +00:00
|
|
|
const threadReplyEvent = new MatrixEvent({
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
sender: userId,
|
|
|
|
room_id: roomId,
|
|
|
|
content: {
|
|
|
|
msgtype: MsgType.Text,
|
2022-12-12 11:24:14 +00:00
|
|
|
body: "this is a thread reply",
|
2022-06-02 15:43:19 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
// mock the thread stuff
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(threadReplyEvent, "isThreadRoot", "get").mockReturnValue(false);
|
2022-06-02 15:43:19 +00:00
|
|
|
// set alicesMessageEvent as the root event
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(threadReplyEvent, "getThread").mockReturnValue({
|
|
|
|
rootEvent: alicesMessageEvent,
|
|
|
|
} as unknown as Thread);
|
2022-06-02 15:43:19 +00:00
|
|
|
const { getByLabelText } = getComponent({ mxEvent: threadReplyEvent });
|
|
|
|
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(getByLabelText("Reply in thread"));
|
2022-06-02 15:43:19 +00:00
|
|
|
|
2022-07-23 12:13:49 +00:00
|
|
|
expect(dispatcher.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ShowThread,
|
2022-06-02 15:43:19 +00:00
|
|
|
rootEvent: alicesMessageEvent,
|
|
|
|
initialEvent: threadReplyEvent,
|
|
|
|
highlighted: true,
|
|
|
|
scroll_into_view: true,
|
|
|
|
push: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-07-15 19:55:11 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("favourite button", () => {
|
2022-07-15 19:55:11 +00:00
|
|
|
//for multiple event usecase
|
|
|
|
const favButton = (evt: MatrixEvent) => {
|
2023-02-15 13:36:22 +00:00
|
|
|
return getComponent({ mxEvent: evt }).getByTestId(evt.getId()!);
|
2022-07-15 19:55:11 +00:00
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("when favourite_messages feature is enabled", () => {
|
2022-07-15 19:55:11 +00:00
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(setting) => setting === "feature_favourite_messages",
|
|
|
|
);
|
2022-07-15 19:55:11 +00:00
|
|
|
localStorageMock.clear();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders favourite button on own actionable event", () => {
|
2022-07-15 19:55:11 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Favourite")).toBeTruthy();
|
2022-07-15 19:55:11 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders favourite button on other actionable events", () => {
|
2022-07-15 19:55:11 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: bobsMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Favourite")).toBeTruthy();
|
2022-07-15 19:55:11 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render Favourite button on non-actionable event", () => {
|
2022-07-15 19:55:11 +00:00
|
|
|
//redacted event is not actionable
|
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: redactedEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Favourite")).toBeFalsy();
|
2022-07-15 19:55:11 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("remembers favourited state of multiple events, and handles the localStorage of the events accordingly", () => {
|
|
|
|
const alicesAction = favButton(alicesMessageEvent);
|
|
|
|
const bobsAction = favButton(bobsMessageEvent);
|
|
|
|
|
|
|
|
//default state before being clicked
|
|
|
|
expect(alicesAction.classList).not.toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(bobsAction.classList).not.toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(localStorageMock.getItem("io_element_favouriteMessages")).toBeNull();
|
|
|
|
|
|
|
|
//if only alice's event is fired
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(alicesAction);
|
2022-12-12 11:24:14 +00:00
|
|
|
|
|
|
|
expect(alicesAction.classList).toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(bobsAction.classList).not.toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(localStorageMock.setItem).toHaveBeenCalledWith(
|
|
|
|
"io_element_favouriteMessages",
|
|
|
|
'["$alices_message"]',
|
|
|
|
);
|
|
|
|
|
|
|
|
//when bob's event is fired,both should be styled and stored in localStorage
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(bobsAction);
|
2022-12-12 11:24:14 +00:00
|
|
|
|
|
|
|
expect(alicesAction.classList).toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(bobsAction.classList).toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(localStorageMock.setItem).toHaveBeenCalledWith(
|
|
|
|
"io_element_favouriteMessages",
|
|
|
|
'["$alices_message","$bobs_message"]',
|
|
|
|
);
|
|
|
|
|
|
|
|
//finally, at this point the localStorage should contain the two eventids
|
|
|
|
expect(localStorageMock.getItem("io_element_favouriteMessages")).toEqual(
|
|
|
|
'["$alices_message","$bobs_message"]',
|
|
|
|
);
|
|
|
|
|
|
|
|
//if decided to unfavourite bob's event by clicking again
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.click(bobsAction);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(bobsAction.classList).not.toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(alicesAction.classList).toContain("mx_MessageActionBar_favouriteButton_fillstar");
|
|
|
|
expect(localStorageMock.getItem("io_element_favouriteMessages")).toEqual('["$alices_message"]');
|
|
|
|
});
|
2022-07-15 19:55:11 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("when favourite_messages feature is disabled", () => {
|
|
|
|
it("does not render", () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
2022-07-15 19:55:11 +00:00
|
|
|
const { queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(queryByLabelText("Favourite")).toBeFalsy();
|
2022-07-15 19:55:11 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-08-18 07:18:18 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it.each([["React"], ["Reply"], ["Reply in thread"], ["Favourite"], ["Edit"]])(
|
|
|
|
"does not show context menu when right-clicking",
|
|
|
|
(buttonLabel: string) => {
|
|
|
|
// For favourite button
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
|
2022-08-18 07:18:18 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = new MouseEvent("contextmenu", {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
});
|
|
|
|
event.stopPropagation = jest.fn();
|
|
|
|
event.preventDefault = jest.fn();
|
|
|
|
|
|
|
|
const { queryByTestId, queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent(queryByLabelText(buttonLabel)!, event);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(event.stopPropagation).toHaveBeenCalled();
|
|
|
|
expect(event.preventDefault).toHaveBeenCalled();
|
|
|
|
expect(queryByTestId("mx_MessageContextMenu")).toBeFalsy();
|
|
|
|
},
|
|
|
|
);
|
2022-08-18 07:18:18 +00:00
|
|
|
|
|
|
|
it("does shows context menu when right-clicking options", () => {
|
|
|
|
const { queryByTestId, queryByLabelText } = getComponent({ mxEvent: alicesMessageEvent });
|
2023-02-15 13:36:22 +00:00
|
|
|
fireEvent.contextMenu(queryByLabelText("Options")!);
|
2022-08-18 07:18:18 +00:00
|
|
|
expect(queryByTestId("mx_MessageContextMenu")).toBeTruthy();
|
|
|
|
});
|
2022-06-02 08:25:56 +00:00
|
|
|
});
|