2022-03-03 15:54:44 +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.
|
|
|
|
*/
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
import React from "react";
|
2023-03-15 15:47:21 +03:00
|
|
|
import { render, act, RenderResult, fireEvent, waitForElementToBeRemoved, screen } from "@testing-library/react";
|
2022-03-21 12:03:03 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2023-08-08 13:12:12 +03:00
|
|
|
import {
|
|
|
|
MatrixEvent,
|
|
|
|
RoomStateEvent,
|
|
|
|
IEvent,
|
|
|
|
Room,
|
|
|
|
EventTimelineSet,
|
|
|
|
IMinimalEvent,
|
|
|
|
EventType,
|
|
|
|
RelationType,
|
|
|
|
MsgType,
|
2023-08-23 12:04:25 +03:00
|
|
|
M_POLL_KIND_DISCLOSED,
|
2023-08-08 13:12:12 +03:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2023-01-13 20:02:33 +03:00
|
|
|
import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent";
|
|
|
|
import { PollResponseEvent } from "matrix-js-sdk/src/extensible_events_v1/PollResponseEvent";
|
|
|
|
import { PollEndEvent } from "matrix-js-sdk/src/extensible_events_v1/PollEndEvent";
|
2024-06-13 17:15:59 +03:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
2022-03-03 15:54:44 +03:00
|
|
|
|
2023-02-02 23:22:26 +03:00
|
|
|
import { stubClient, mkEvent, mkMessage, flushPromises } from "../../../test-utils";
|
2022-03-03 15:54:44 +03:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-03-21 12:03:03 +03:00
|
|
|
import PinnedMessagesCard from "../../../../src/components/views/right_panel/PinnedMessagesCard";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2022-07-12 09:27:45 +03:00
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2022-03-03 15:54:44 +03:00
|
|
|
|
|
|
|
describe("<PinnedMessagesCard />", () => {
|
|
|
|
stubClient();
|
2023-06-05 20:12:23 +03:00
|
|
|
const cli = mocked(MatrixClientPeg.safeGet());
|
2022-03-05 02:15:03 +03:00
|
|
|
cli.getUserId.mockReturnValue("@alice:example.org");
|
2023-02-02 23:22:26 +03:00
|
|
|
cli.setRoomAccountData.mockResolvedValue({});
|
2022-03-21 12:03:03 +03:00
|
|
|
cli.relations.mockResolvedValue({ originalEvent: {} as unknown as MatrixEvent, events: [] });
|
2022-03-03 15:54:44 +03:00
|
|
|
|
2022-03-05 02:15:03 +03:00
|
|
|
const mkRoom = (localPins: MatrixEvent[], nonLocalPins: MatrixEvent[]): Room => {
|
2023-02-02 23:22:26 +03:00
|
|
|
const room = new Room("!room:example.org", cli, "@me:example.org");
|
2022-03-05 02:15:03 +03:00
|
|
|
// Deferred since we may be adding or removing pins later
|
|
|
|
const pins = () => [...localPins, ...nonLocalPins];
|
2022-03-03 15:54:44 +03:00
|
|
|
|
|
|
|
// Insert pin IDs into room state
|
2023-02-02 23:22:26 +03:00
|
|
|
jest.spyOn(room.currentState, "getStateEvents").mockImplementation((): any =>
|
2022-11-04 13:48:08 +03:00
|
|
|
mkEvent({
|
2022-03-03 15:54:44 +03:00
|
|
|
event: true,
|
|
|
|
type: EventType.RoomPinnedEvents,
|
|
|
|
content: {
|
2022-03-05 02:15:03 +03:00
|
|
|
pinned: pins().map((e) => e.getId()),
|
2022-03-03 15:54:44 +03:00
|
|
|
},
|
2022-03-21 12:03:03 +03:00
|
|
|
user: "@user:example.org",
|
|
|
|
room: "!room:example.org",
|
2022-03-05 02:15:03 +03:00
|
|
|
}),
|
|
|
|
);
|
2022-03-03 15:54:44 +03:00
|
|
|
|
2023-02-02 23:22:26 +03:00
|
|
|
jest.spyOn(room.currentState, "on");
|
|
|
|
|
2022-03-03 15:54:44 +03:00
|
|
|
// Insert local pins into local timeline set
|
|
|
|
room.getUnfilteredTimelineSet = () =>
|
|
|
|
({
|
|
|
|
getTimelineForEvent: () => ({
|
|
|
|
getEvents: () => localPins,
|
|
|
|
}),
|
2024-01-02 21:56:39 +03:00
|
|
|
}) as unknown as EventTimelineSet;
|
2022-03-03 15:54:44 +03:00
|
|
|
|
|
|
|
// Return all pins over fetchRoomEvent
|
2022-03-21 12:03:03 +03:00
|
|
|
cli.fetchRoomEvent.mockImplementation((roomId, eventId) => {
|
|
|
|
const event = pins().find((e) => e.getId() === eventId)?.event;
|
|
|
|
return Promise.resolve(event as IMinimalEvent);
|
|
|
|
});
|
2022-03-03 15:54:44 +03:00
|
|
|
|
2023-02-02 23:22:26 +03:00
|
|
|
cli.getRoom.mockReturnValue(room);
|
|
|
|
|
2022-03-03 15:54:44 +03:00
|
|
|
return room;
|
|
|
|
};
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
const mountPins = async (room: Room): Promise<RenderResult> => {
|
2024-08-06 20:22:02 +03:00
|
|
|
const pins = render(
|
|
|
|
<MatrixClientContext.Provider value={cli}>
|
|
|
|
<PinnedMessagesCard
|
|
|
|
room={room}
|
|
|
|
onClose={jest.fn()}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(room, room.roomId)}
|
|
|
|
/>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
// Wait a tick for state updates
|
|
|
|
await act(() => sleep(0));
|
2022-03-05 02:15:03 +03:00
|
|
|
|
|
|
|
return pins;
|
|
|
|
};
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
const emitPinUpdates = async (room: Room) => {
|
2022-03-21 12:03:03 +03:00
|
|
|
const pinListener = mocked(room.currentState).on.mock.calls.find(
|
2022-03-05 02:15:03 +03:00
|
|
|
([eventName, listener]) => eventName === RoomStateEvent.Events,
|
2023-02-16 12:38:44 +03:00
|
|
|
)![1];
|
2022-03-05 02:15:03 +03:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
// Emit the update
|
2022-03-21 12:03:03 +03:00
|
|
|
// @ts-ignore what is going on here?
|
2022-03-05 02:15:03 +03:00
|
|
|
pinListener(room.currentState.getStateEvents());
|
|
|
|
// Wait a tick for state updates
|
2024-06-13 17:15:59 +03:00
|
|
|
await sleep(0);
|
2022-03-05 02:15:03 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const pin1 = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
msg: "First pinned message",
|
|
|
|
});
|
|
|
|
const pin2 = mkMessage({
|
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
msg: "The second one",
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates when messages are pinned", async () => {
|
|
|
|
// Start with nothing pinned
|
2023-02-02 23:22:26 +03:00
|
|
|
const localPins: MatrixEvent[] = [];
|
|
|
|
const nonLocalPins: MatrixEvent[] = [];
|
2023-03-06 15:13:17 +03:00
|
|
|
const room = mkRoom(localPins, nonLocalPins);
|
|
|
|
const pins = await mountPins(room);
|
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(0);
|
2022-03-05 02:15:03 +03:00
|
|
|
|
|
|
|
// Pin the first message
|
|
|
|
localPins.push(pin1);
|
2023-03-06 15:13:17 +03:00
|
|
|
await emitPinUpdates(room);
|
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(1);
|
2022-03-05 02:15:03 +03:00
|
|
|
|
|
|
|
// Pin the second message
|
|
|
|
nonLocalPins.push(pin2);
|
2023-03-06 15:13:17 +03:00
|
|
|
await emitPinUpdates(room);
|
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(2);
|
2022-03-05 02:15:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("updates when messages are unpinned", async () => {
|
|
|
|
// Start with two pins
|
|
|
|
const localPins = [pin1];
|
|
|
|
const nonLocalPins = [pin2];
|
2023-03-06 15:13:17 +03:00
|
|
|
const room = mkRoom(localPins, nonLocalPins);
|
|
|
|
const pins = await mountPins(room);
|
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(2);
|
2022-03-05 02:15:03 +03:00
|
|
|
|
|
|
|
// Unpin the first message
|
|
|
|
localPins.pop();
|
2023-03-06 15:13:17 +03:00
|
|
|
await emitPinUpdates(room);
|
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(1);
|
2022-03-05 02:15:03 +03:00
|
|
|
|
|
|
|
// Unpin the second message
|
|
|
|
nonLocalPins.pop();
|
2023-03-06 15:13:17 +03:00
|
|
|
await emitPinUpdates(room);
|
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(0);
|
2022-03-05 02:15:03 +03:00
|
|
|
});
|
|
|
|
|
2022-03-03 15:54:44 +03:00
|
|
|
it("hides unpinnable events found in local timeline", async () => {
|
|
|
|
// Redacted messages are unpinnable
|
|
|
|
const pin = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
content: {},
|
2022-03-21 12:03:03 +03:00
|
|
|
unsigned: { redacted_because: {} as unknown as IEvent },
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
2022-03-03 15:54:44 +03:00
|
|
|
});
|
|
|
|
|
2022-03-05 02:15:03 +03:00
|
|
|
const pins = await mountPins(mkRoom([pin], []));
|
2023-03-06 15:13:17 +03:00
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(0);
|
2022-03-03 15:54:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("hides unpinnable events not found in local timeline", async () => {
|
|
|
|
// Redacted messages are unpinnable
|
|
|
|
const pin = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
content: {},
|
2022-03-21 12:03:03 +03:00
|
|
|
unsigned: { redacted_because: {} as unknown as IEvent },
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
2022-03-03 15:54:44 +03:00
|
|
|
});
|
|
|
|
|
2022-03-05 02:15:03 +03:00
|
|
|
const pins = await mountPins(mkRoom([], [pin]));
|
2023-03-06 15:13:17 +03:00
|
|
|
expect(pins.container.querySelectorAll(".mx_PinnedEventTile")).toHaveLength(0);
|
2022-03-03 15:54:44 +03:00
|
|
|
});
|
2022-03-03 20:20:13 +03:00
|
|
|
|
|
|
|
it("accounts for edits", async () => {
|
2022-03-21 12:03:03 +03:00
|
|
|
const messageEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
content: {
|
|
|
|
"msgtype": MsgType.Text,
|
|
|
|
"body": " * First pinned message, edited",
|
|
|
|
"m.new_content": {
|
|
|
|
msgtype: MsgType.Text,
|
|
|
|
body: "First pinned message, edited",
|
2022-03-03 20:20:13 +03:00
|
|
|
},
|
2022-03-21 12:03:03 +03:00
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: RelationType.Replace,
|
|
|
|
event_id: pin1.getId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
cli.relations.mockResolvedValue({
|
|
|
|
originalEvent: pin1,
|
|
|
|
events: [messageEvent],
|
2022-03-03 20:20:13 +03:00
|
|
|
});
|
|
|
|
|
2022-03-05 02:15:03 +03:00
|
|
|
const pins = await mountPins(mkRoom([], [pin1]));
|
2023-03-06 15:13:17 +03:00
|
|
|
const pinTile = pins.container.querySelectorAll(".mx_PinnedEventTile");
|
2022-03-03 20:20:13 +03:00
|
|
|
expect(pinTile.length).toBe(1);
|
2023-03-06 15:13:17 +03:00
|
|
|
expect(pinTile[0].querySelector(".mx_EventTile_body")!).toHaveTextContent("First pinned message, edited");
|
2022-03-05 02:15:03 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("displays votes on polls not found in local timeline", async () => {
|
|
|
|
const poll = mkEvent({
|
|
|
|
...PollStartEvent.from("A poll", ["Option 1", "Option 2"], M_POLL_KIND_DISCLOSED).serialize(),
|
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
});
|
|
|
|
|
|
|
|
const answers = (poll.unstableExtensibleEvent as PollStartEvent).answers;
|
|
|
|
const responses = [
|
2023-02-13 14:39:16 +03:00
|
|
|
["@alice:example.org", 0] as [string, number],
|
|
|
|
["@bob:example.org", 0] as [string, number],
|
|
|
|
["@eve:example.org", 1] as [string, number],
|
2022-03-05 02:15:03 +03:00
|
|
|
].map(([user, option], i) =>
|
|
|
|
mkEvent({
|
2023-02-02 23:22:26 +03:00
|
|
|
...PollResponseEvent.from([answers[option as number].id], poll.getId()!).serialize(),
|
2022-03-05 02:15:03 +03:00
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
2023-03-15 15:47:21 +03:00
|
|
|
user,
|
2022-03-05 02:15:03 +03:00
|
|
|
}),
|
|
|
|
);
|
2023-02-02 23:22:26 +03:00
|
|
|
|
2022-03-05 02:15:03 +03:00
|
|
|
const end = mkEvent({
|
2023-02-02 23:22:26 +03:00
|
|
|
...PollEndEvent.from(poll.getId()!, "Closing the poll").serialize(),
|
2022-03-05 02:15:03 +03:00
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the responses available
|
2023-02-02 23:22:26 +03:00
|
|
|
cli.relations.mockImplementation(async (roomId, eventId, relationType, eventType, opts) => {
|
2022-03-05 02:15:03 +03:00
|
|
|
if (eventId === poll.getId() && relationType === RelationType.Reference) {
|
2023-02-02 23:22:26 +03:00
|
|
|
// Paginate the results, for added challenge
|
|
|
|
return opts?.from === "page2"
|
|
|
|
? { originalEvent: poll, events: responses.slice(2) }
|
|
|
|
: { originalEvent: poll, events: [...responses.slice(0, 2), end], nextBatch: "page2" };
|
2022-03-05 02:15:03 +03:00
|
|
|
}
|
2022-03-21 12:03:03 +03:00
|
|
|
// type does not allow originalEvent to be falsy
|
|
|
|
// but code seems to
|
|
|
|
// so still test that
|
|
|
|
return { originalEvent: undefined as unknown as MatrixEvent, events: [] };
|
2022-03-05 02:15:03 +03:00
|
|
|
});
|
|
|
|
|
2023-02-02 23:22:26 +03:00
|
|
|
const room = mkRoom([], [poll]);
|
|
|
|
// poll end event validates against this
|
|
|
|
jest.spyOn(room.currentState, "maySendRedactionForEvent").mockReturnValue(true);
|
|
|
|
|
|
|
|
const pins = await mountPins(room);
|
|
|
|
// two pages of results
|
|
|
|
await flushPromises();
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
const pollInstance = room.polls.get(poll.getId()!);
|
|
|
|
expect(pollInstance).toBeTruthy();
|
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
const pinTile = pins.container.querySelectorAll(".mx_MPollBody");
|
2023-02-02 23:22:26 +03:00
|
|
|
|
2023-03-06 15:13:17 +03:00
|
|
|
expect(pinTile).toHaveLength(1);
|
|
|
|
expect(pinTile[0].querySelectorAll(".mx_PollOption_ended")).toHaveLength(2);
|
|
|
|
expect(pinTile[0].querySelectorAll(".mx_PollOption_optionVoteCount")[0]).toHaveTextContent("2 votes");
|
|
|
|
expect([...pinTile[0].querySelectorAll(".mx_PollOption_optionVoteCount")].at(-1)).toHaveTextContent("1 vote");
|
2022-03-03 20:20:13 +03:00
|
|
|
});
|
2023-03-15 15:47:21 +03:00
|
|
|
|
|
|
|
it("should allow admins to unpin messages", async () => {
|
|
|
|
const nonLocalPins = [pin1];
|
|
|
|
const room = mkRoom([], nonLocalPins);
|
|
|
|
jest.spyOn(room.currentState, "mayClientSendStateEvent").mockReturnValue(true);
|
|
|
|
const sendStateEvent = jest.spyOn(cli, "sendStateEvent");
|
|
|
|
|
|
|
|
const pins = await mountPins(room);
|
|
|
|
const pinTile = pins.container.querySelectorAll(".mx_PinnedEventTile");
|
|
|
|
expect(pinTile).toHaveLength(1);
|
|
|
|
|
|
|
|
fireEvent.click(pinTile[0].querySelector(".mx_PinnedEventTile_unpinButton")!);
|
|
|
|
expect(sendStateEvent).toHaveBeenCalledWith(room.roomId, "m.room.pinned_events", { pinned: [] }, "");
|
|
|
|
|
|
|
|
nonLocalPins.pop();
|
|
|
|
await Promise.all([waitForElementToBeRemoved(pinTile[0]), emitPinUpdates(room)]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show spinner whilst loading", async () => {
|
|
|
|
const room = mkRoom([], [pin1]);
|
|
|
|
mountPins(room);
|
2024-08-06 20:22:02 +03:00
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByRole("progressbar"));
|
2023-03-15 15:47:21 +03:00
|
|
|
});
|
2022-03-03 15:54:44 +03:00
|
|
|
});
|