2022-03-03 20:54:44 +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.
|
|
|
|
*/
|
|
|
|
|
2023-03-06 20:13:17 +08:00
|
|
|
import React from "react";
|
2024-08-16 20:16:06 +08:00
|
|
|
import { render, act, RenderResult, waitForElementToBeRemoved, screen } from "@testing-library/react";
|
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
2023-08-08 18:12:12 +08:00
|
|
|
import {
|
|
|
|
MatrixEvent,
|
|
|
|
RoomStateEvent,
|
|
|
|
IEvent,
|
|
|
|
Room,
|
|
|
|
IMinimalEvent,
|
|
|
|
EventType,
|
|
|
|
RelationType,
|
|
|
|
MsgType,
|
2023-08-23 17:04:25 +08:00
|
|
|
M_POLL_KIND_DISCLOSED,
|
2024-08-16 20:16:06 +08:00
|
|
|
EventTimeline,
|
|
|
|
MatrixClient,
|
2023-08-08 18:12:12 +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";
|
|
|
|
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 22:15:59 +08:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
2024-08-16 20:16:06 +08:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-03-03 20:54:44 +08:00
|
|
|
|
2023-02-03 04:22:26 +08:00
|
|
|
import { stubClient, mkEvent, mkMessage, flushPromises } from "../../../test-utils";
|
2022-03-03 20:54:44 +08:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2024-08-16 20:16:06 +08:00
|
|
|
import { PinnedMessagesCard } from "../../../../src/components/views/right_panel/PinnedMessagesCard";
|
2022-03-21 17:03:03 +08:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2022-07-12 14:27:45 +08:00
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2024-08-16 20:16:06 +08:00
|
|
|
import Modal from "../../../../src/Modal";
|
|
|
|
import { UnpinAllDialog } from "../../../../src/components/views/dialogs/UnpinAllDialog";
|
2022-03-03 20:54:44 +08:00
|
|
|
|
|
|
|
describe("<PinnedMessagesCard />", () => {
|
2024-08-16 20:16:06 +08:00
|
|
|
let cli: MockedObject<MatrixClient>;
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
|
|
|
cli = mocked(MatrixClientPeg.safeGet());
|
|
|
|
cli.getUserId.mockReturnValue("@alice:example.org");
|
|
|
|
cli.setRoomAccountData.mockResolvedValue({});
|
|
|
|
cli.relations.mockResolvedValue({ originalEvent: {} as unknown as MatrixEvent, events: [] });
|
|
|
|
});
|
2022-03-03 20:54:44 +08:00
|
|
|
|
2022-03-05 07:15:03 +08:00
|
|
|
const mkRoom = (localPins: MatrixEvent[], nonLocalPins: MatrixEvent[]): Room => {
|
2023-02-03 04:22:26 +08:00
|
|
|
const room = new Room("!room:example.org", cli, "@me:example.org");
|
2022-03-05 07:15:03 +08:00
|
|
|
// Deferred since we may be adding or removing pins later
|
|
|
|
const pins = () => [...localPins, ...nonLocalPins];
|
2022-03-03 20:54:44 +08:00
|
|
|
|
|
|
|
// Insert pin IDs into room state
|
2024-08-16 20:16:06 +08:00
|
|
|
jest.spyOn(room.getLiveTimeline().getState(EventTimeline.FORWARDS)!, "getStateEvents").mockImplementation(
|
|
|
|
(): any =>
|
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomPinnedEvents,
|
|
|
|
content: {
|
|
|
|
pinned: pins().map((e) => e.getId()),
|
|
|
|
},
|
|
|
|
user: "@user:example.org",
|
|
|
|
room: "!room:example.org",
|
|
|
|
}),
|
2022-03-05 07:15:03 +08:00
|
|
|
);
|
2022-03-03 20:54:44 +08:00
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
jest.spyOn(room.getLiveTimeline().getState(EventTimeline.FORWARDS)!, "mayClientSendStateEvent").mockReturnValue(
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
// poll end event validates against this
|
|
|
|
jest.spyOn(
|
|
|
|
room.getLiveTimeline().getState(EventTimeline.FORWARDS)!,
|
|
|
|
"maySendRedactionForEvent",
|
|
|
|
).mockReturnValue(true);
|
2022-03-03 20:54:44 +08:00
|
|
|
|
|
|
|
// Return all pins over fetchRoomEvent
|
2022-03-21 17:03:03 +08:00
|
|
|
cli.fetchRoomEvent.mockImplementation((roomId, eventId) => {
|
|
|
|
const event = pins().find((e) => e.getId() === eventId)?.event;
|
|
|
|
return Promise.resolve(event as IMinimalEvent);
|
|
|
|
});
|
2022-03-03 20:54:44 +08:00
|
|
|
|
2023-02-03 04:22:26 +08:00
|
|
|
cli.getRoom.mockReturnValue(room);
|
|
|
|
|
2022-03-03 20:54:44 +08:00
|
|
|
return room;
|
|
|
|
};
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
async function renderMessagePinList(room: Room): Promise<RenderResult> {
|
|
|
|
const renderResult = render(
|
2024-08-07 01:22:02 +08:00
|
|
|
<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 07:15:03 +08:00
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
return renderResult;
|
|
|
|
}
|
2022-03-05 07:15:03 +08:00
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param room
|
|
|
|
*/
|
|
|
|
async function emitPinUpdate(room: Room) {
|
2022-03-05 07:15:03 +08:00
|
|
|
await act(async () => {
|
2024-08-16 20:16:06 +08:00
|
|
|
const roomState = room.getLiveTimeline().getState(EventTimeline.FORWARDS)!;
|
|
|
|
roomState.emit(
|
|
|
|
RoomStateEvent.Events,
|
|
|
|
new MatrixEvent({ type: EventType.RoomPinnedEvents }),
|
|
|
|
roomState,
|
|
|
|
null,
|
|
|
|
);
|
2022-03-05 07:15:03 +08:00
|
|
|
});
|
2024-08-16 20:16:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the pinned messages card with the given pinned messages.
|
|
|
|
* Return the room, testing library helpers and functions to add and remove pinned messages.
|
|
|
|
* @param localPins
|
|
|
|
* @param nonLocalPins
|
|
|
|
*/
|
|
|
|
async function initPinnedMessagesCard(localPins: MatrixEvent[], nonLocalPins: MatrixEvent[]) {
|
|
|
|
const room = mkRoom(localPins, nonLocalPins);
|
|
|
|
const addLocalPinEvent = async (event: MatrixEvent) => {
|
|
|
|
localPins.push(event);
|
|
|
|
await emitPinUpdate(room);
|
|
|
|
};
|
|
|
|
const removeLastLocalPinEvent = async () => {
|
|
|
|
localPins.pop();
|
|
|
|
await emitPinUpdate(room);
|
|
|
|
};
|
|
|
|
const addNonLocalPinEvent = async (event: MatrixEvent) => {
|
|
|
|
nonLocalPins.push(event);
|
|
|
|
await emitPinUpdate(room);
|
|
|
|
};
|
|
|
|
const removeLastNonLocalPinEvent = async () => {
|
|
|
|
nonLocalPins.pop();
|
|
|
|
await emitPinUpdate(room);
|
|
|
|
};
|
|
|
|
const renderResult = await renderMessagePinList(room);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...renderResult,
|
|
|
|
addLocalPinEvent,
|
|
|
|
removeLastLocalPinEvent,
|
|
|
|
addNonLocalPinEvent,
|
|
|
|
removeLastNonLocalPinEvent,
|
|
|
|
room,
|
|
|
|
};
|
|
|
|
}
|
2022-03-05 07:15:03 +08: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",
|
|
|
|
});
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
it("should show spinner whilst loading", async () => {
|
|
|
|
const room = mkRoom([], [pin1]);
|
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={cli}>
|
|
|
|
<PinnedMessagesCard
|
|
|
|
room={room}
|
|
|
|
onClose={jest.fn()}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(room, room.roomId)}
|
|
|
|
/>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
await waitForElementToBeRemoved(() => screen.queryAllByRole("progressbar"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show the empty state when there are no pins", async () => {
|
|
|
|
const { asFragment } = await initPinnedMessagesCard([], []);
|
|
|
|
|
|
|
|
expect(screen.getByText("Pin important messages so that they can be easily discovered")).toBeInTheDocument();
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show two pinned messages", async () => {
|
|
|
|
//const room = mkRoom([pin1], [pin2]);
|
|
|
|
const { asFragment } = await initPinnedMessagesCard([pin1], [pin2]);
|
|
|
|
|
|
|
|
expect(screen.queryAllByRole("listitem")).toHaveLength(2);
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should updates when messages are pinned", async () => {
|
2022-03-05 07:15:03 +08:00
|
|
|
// Start with nothing pinned
|
2024-08-16 20:16:06 +08:00
|
|
|
const { addLocalPinEvent, addNonLocalPinEvent } = await initPinnedMessagesCard([], []);
|
|
|
|
|
|
|
|
expect(screen.queryAllByRole("listitem")).toHaveLength(0);
|
2022-03-05 07:15:03 +08:00
|
|
|
|
|
|
|
// Pin the first message
|
2024-08-16 20:16:06 +08:00
|
|
|
await addLocalPinEvent(pin1);
|
|
|
|
expect(screen.getAllByRole("listitem")).toHaveLength(1);
|
2022-03-05 07:15:03 +08:00
|
|
|
|
|
|
|
// Pin the second message
|
2024-08-16 20:16:06 +08:00
|
|
|
await addNonLocalPinEvent(pin2);
|
|
|
|
expect(screen.getAllByRole("listitem")).toHaveLength(2);
|
2022-03-05 07:15:03 +08:00
|
|
|
});
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
it("should updates when messages are unpinned", async () => {
|
2022-03-05 07:15:03 +08:00
|
|
|
// Start with two pins
|
2024-08-16 20:16:06 +08:00
|
|
|
const { removeLastLocalPinEvent, removeLastNonLocalPinEvent } = await initPinnedMessagesCard([pin1], [pin2]);
|
|
|
|
expect(screen.getAllByRole("listitem")).toHaveLength(2);
|
2022-03-05 07:15:03 +08:00
|
|
|
|
|
|
|
// Unpin the first message
|
2024-08-16 20:16:06 +08:00
|
|
|
await removeLastLocalPinEvent();
|
|
|
|
expect(screen.getAllByRole("listitem")).toHaveLength(1);
|
2022-03-05 07:15:03 +08:00
|
|
|
|
|
|
|
// Unpin the second message
|
2024-08-16 20:16:06 +08:00
|
|
|
await removeLastNonLocalPinEvent();
|
|
|
|
expect(screen.queryAllByRole("listitem")).toHaveLength(0);
|
2022-03-03 20:54:44 +08:00
|
|
|
});
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
it("should display an edited pinned event", async () => {
|
2022-03-21 17:03:03 +08: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-04 01:20:13 +08:00
|
|
|
},
|
2022-03-21 17:03:03 +08:00
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: RelationType.Replace,
|
|
|
|
event_id: pin1.getId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
cli.relations.mockResolvedValue({
|
|
|
|
originalEvent: pin1,
|
|
|
|
events: [messageEvent],
|
2022-03-04 01:20:13 +08:00
|
|
|
});
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
await initPinnedMessagesCard([], [pin1]);
|
|
|
|
expect(screen.getByText("First pinned message, edited")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("unpinnable event", () => {
|
|
|
|
it("should hide unpinnable events found in local timeline", async () => {
|
|
|
|
// Redacted messages are unpinnable
|
|
|
|
const pin = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
content: {},
|
|
|
|
unsigned: { redacted_because: {} as unknown as IEvent },
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
});
|
|
|
|
await initPinnedMessagesCard([pin], []);
|
|
|
|
expect(screen.queryAllByRole("listitem")).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("hides unpinnable events not found in local timeline", async () => {
|
|
|
|
// Redacted messages are unpinnable
|
|
|
|
const pin = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
content: {},
|
|
|
|
unsigned: { redacted_because: {} as unknown as IEvent },
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
});
|
|
|
|
await initPinnedMessagesCard([], [pin]);
|
|
|
|
expect(screen.queryAllByRole("listitem")).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("unpin all", () => {
|
|
|
|
it("should not allow to unpinall", async () => {
|
|
|
|
const room = mkRoom([pin1], [pin2]);
|
|
|
|
jest.spyOn(
|
|
|
|
room.getLiveTimeline().getState(EventTimeline.FORWARDS)!,
|
|
|
|
"mayClientSendStateEvent",
|
|
|
|
).mockReturnValue(false);
|
|
|
|
|
|
|
|
const { asFragment } = 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));
|
|
|
|
|
|
|
|
expect(screen.queryByText("Unpin all messages")).toBeNull();
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should allow unpinning all messages", async () => {
|
|
|
|
jest.spyOn(Modal, "createDialog");
|
|
|
|
|
|
|
|
const { room } = await initPinnedMessagesCard([pin1], [pin2]);
|
|
|
|
expect(screen.getByText("Unpin all messages")).toBeInTheDocument();
|
|
|
|
|
|
|
|
await userEvent.click(screen.getByText("Unpin all messages"));
|
|
|
|
// Should open the UnpinAllDialog dialog
|
|
|
|
expect(Modal.createDialog).toHaveBeenCalledWith(UnpinAllDialog, { roomId: room.roomId, matrixClient: cli });
|
|
|
|
});
|
2022-03-05 07:15:03 +08:00
|
|
|
});
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
it("should displays votes on polls not found in local timeline", async () => {
|
2022-03-05 07:15:03 +08:00
|
|
|
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 19:39:16 +08: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 07:15:03 +08:00
|
|
|
].map(([user, option], i) =>
|
|
|
|
mkEvent({
|
2023-02-03 04:22:26 +08:00
|
|
|
...PollResponseEvent.from([answers[option as number].id], poll.getId()!).serialize(),
|
2022-03-05 07:15:03 +08:00
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
2023-03-15 20:47:21 +08:00
|
|
|
user,
|
2022-03-05 07:15:03 +08:00
|
|
|
}),
|
|
|
|
);
|
2023-02-03 04:22:26 +08:00
|
|
|
|
2022-03-05 07:15:03 +08:00
|
|
|
const end = mkEvent({
|
2023-02-03 04:22:26 +08:00
|
|
|
...PollEndEvent.from(poll.getId()!, "Closing the poll").serialize(),
|
2022-03-05 07:15:03 +08:00
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the responses available
|
2023-02-03 04:22:26 +08:00
|
|
|
cli.relations.mockImplementation(async (roomId, eventId, relationType, eventType, opts) => {
|
2022-03-05 07:15:03 +08:00
|
|
|
if (eventId === poll.getId() && relationType === RelationType.Reference) {
|
2023-02-03 04:22:26 +08: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 07:15:03 +08:00
|
|
|
}
|
2022-03-21 17:03:03 +08: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 07:15:03 +08:00
|
|
|
});
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
const { room } = await initPinnedMessagesCard([], [poll]);
|
2023-02-03 04:22:26 +08:00
|
|
|
|
|
|
|
// two pages of results
|
|
|
|
await flushPromises();
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
const pollInstance = room.polls.get(poll.getId()!);
|
|
|
|
expect(pollInstance).toBeTruthy();
|
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
expect(screen.getByText("A poll")).toBeInTheDocument();
|
2023-03-15 20:47:21 +08:00
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
expect(screen.getByText("Option 1")).toBeInTheDocument();
|
|
|
|
expect(screen.getByText("2 votes")).toBeInTheDocument();
|
2023-03-15 20:47:21 +08:00
|
|
|
|
2024-08-16 20:16:06 +08:00
|
|
|
expect(screen.getByText("Option 2")).toBeInTheDocument();
|
|
|
|
expect(screen.getByText("1 vote")).toBeInTheDocument();
|
2023-03-15 20:47:21 +08:00
|
|
|
});
|
2022-03-03 20:54:44 +08:00
|
|
|
});
|