2022-03-03 12:54:44 +00: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-03-21 09:03:03 +00:00
|
|
|
import React, { ComponentProps } from "react";
|
2022-08-02 13:10:43 +00:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-03-21 09:03:03 +00:00
|
|
|
import { mount, ReactWrapper } from "enzyme";
|
|
|
|
import { mocked } from "jest-mock";
|
2022-03-03 12:54:44 +00:00
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2022-03-03 17:20:13 +00:00
|
|
|
import { EventType, RelationType, MsgType } from "matrix-js-sdk/src/@types/event";
|
2022-03-04 23:15:03 +00:00
|
|
|
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
2022-03-21 09:03:03 +00:00
|
|
|
import { IEvent, Room, EventTimelineSet, IMinimalEvent } from "matrix-js-sdk/src/matrix";
|
2023-02-02 20:22:26 +00:00
|
|
|
import { M_POLL_KIND_DISCLOSED } from "matrix-js-sdk/src/@types/polls";
|
2023-01-13 17:02:33 +00: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";
|
2022-03-03 12:54:44 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
import { stubClient, mkEvent, mkMessage, flushPromises } from "../../../test-utils";
|
2022-03-03 12:54:44 +00:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-03-21 09:03:03 +00:00
|
|
|
import PinnedMessagesCard from "../../../../src/components/views/right_panel/PinnedMessagesCard";
|
2022-03-03 12:54:44 +00:00
|
|
|
import PinnedEventTile from "../../../../src/components/views/rooms/PinnedEventTile";
|
2022-03-21 09:03:03 +00:00
|
|
|
import MPollBody from "../../../../src/components/views/messages/MPollBody";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2022-07-12 06:27:45 +00:00
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2022-03-03 12:54:44 +00:00
|
|
|
|
|
|
|
describe("<PinnedMessagesCard />", () => {
|
|
|
|
stubClient();
|
2022-03-21 09:03:03 +00:00
|
|
|
const cli = mocked(MatrixClientPeg.get());
|
2022-03-04 23:15:03 +00:00
|
|
|
cli.getUserId.mockReturnValue("@alice:example.org");
|
2023-02-02 20:22:26 +00:00
|
|
|
cli.setRoomAccountData.mockResolvedValue({});
|
2022-03-21 09:03:03 +00:00
|
|
|
cli.relations.mockResolvedValue({ originalEvent: {} as unknown as MatrixEvent, events: [] });
|
2022-03-03 12:54:44 +00:00
|
|
|
|
2022-03-04 23:15:03 +00:00
|
|
|
const mkRoom = (localPins: MatrixEvent[], nonLocalPins: MatrixEvent[]): Room => {
|
2023-02-02 20:22:26 +00:00
|
|
|
const room = new Room("!room:example.org", cli, "@me:example.org");
|
2022-03-04 23:15:03 +00:00
|
|
|
// Deferred since we may be adding or removing pins later
|
|
|
|
const pins = () => [...localPins, ...nonLocalPins];
|
2022-03-03 12:54:44 +00:00
|
|
|
|
|
|
|
// Insert pin IDs into room state
|
2023-02-02 20:22:26 +00:00
|
|
|
jest.spyOn(room.currentState, "getStateEvents").mockImplementation((): any =>
|
2022-12-12 11:24:14 +00:00
|
|
|
mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomPinnedEvents,
|
|
|
|
content: {
|
|
|
|
pinned: pins().map((e) => e.getId()),
|
|
|
|
},
|
|
|
|
user: "@user:example.org",
|
|
|
|
room: "!room:example.org",
|
|
|
|
}),
|
|
|
|
);
|
2022-03-03 12:54:44 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
jest.spyOn(room.currentState, "on");
|
|
|
|
|
2022-03-03 12:54:44 +00:00
|
|
|
// Insert local pins into local timeline set
|
2022-12-12 11:24:14 +00:00
|
|
|
room.getUnfilteredTimelineSet = () =>
|
|
|
|
({
|
|
|
|
getTimelineForEvent: () => ({
|
|
|
|
getEvents: () => localPins,
|
|
|
|
}),
|
|
|
|
} as unknown as EventTimelineSet);
|
2022-03-03 12:54:44 +00:00
|
|
|
|
|
|
|
// Return all pins over fetchRoomEvent
|
2022-03-21 09:03:03 +00:00
|
|
|
cli.fetchRoomEvent.mockImplementation((roomId, eventId) => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const event = pins().find((e) => e.getId() === eventId)?.event;
|
2022-03-21 09:03:03 +00:00
|
|
|
return Promise.resolve(event as IMinimalEvent);
|
|
|
|
});
|
2022-03-03 12:54:44 +00:00
|
|
|
|
2023-02-02 20:22:26 +00:00
|
|
|
cli.getRoom.mockReturnValue(room);
|
|
|
|
|
2022-03-03 12:54:44 +00:00
|
|
|
return room;
|
|
|
|
};
|
|
|
|
|
2022-03-21 09:03:03 +00:00
|
|
|
const mountPins = async (room: Room): Promise<ReactWrapper<ComponentProps<typeof PinnedMessagesCard>>> => {
|
2023-02-13 17:01:43 +00:00
|
|
|
let pins!: ReactWrapper<ComponentProps<typeof PinnedMessagesCard>>;
|
2022-03-04 23:15:03 +00:00
|
|
|
await act(async () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
pins = mount(
|
|
|
|
<PinnedMessagesCard
|
|
|
|
room={room}
|
|
|
|
onClose={jest.fn()}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(room, room.roomId)}
|
|
|
|
/>,
|
|
|
|
{
|
|
|
|
wrappingComponent: MatrixClientContext.Provider,
|
|
|
|
wrappingComponentProps: { value: cli },
|
|
|
|
},
|
|
|
|
);
|
2022-03-04 23:15:03 +00:00
|
|
|
// Wait a tick for state updates
|
2022-12-12 11:24:14 +00:00
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
2022-03-04 23:15:03 +00:00
|
|
|
});
|
|
|
|
pins.update();
|
|
|
|
|
|
|
|
return pins;
|
|
|
|
};
|
|
|
|
|
2022-03-21 09:03:03 +00:00
|
|
|
const emitPinUpdates = async (pins: ReactWrapper<ComponentProps<typeof PinnedMessagesCard>>) => {
|
2022-03-04 23:15:03 +00:00
|
|
|
const room = pins.props().room;
|
2022-12-12 11:24:14 +00:00
|
|
|
const pinListener = mocked(room.currentState).on.mock.calls.find(
|
|
|
|
([eventName, listener]) => eventName === RoomStateEvent.Events,
|
|
|
|
)[1];
|
2022-03-04 23:15:03 +00:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
// Emit the update
|
2022-03-21 09:03:03 +00:00
|
|
|
// @ts-ignore what is going on here?
|
2022-03-04 23:15:03 +00:00
|
|
|
pinListener(room.currentState.getStateEvents());
|
|
|
|
// Wait a tick for state updates
|
2022-12-12 11:24:14 +00:00
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
2022-03-04 23:15:03 +00:00
|
|
|
});
|
|
|
|
pins.update();
|
|
|
|
};
|
|
|
|
|
|
|
|
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 20:22:26 +00:00
|
|
|
const localPins: MatrixEvent[] = [];
|
|
|
|
const nonLocalPins: MatrixEvent[] = [];
|
2022-03-04 23:15:03 +00:00
|
|
|
const pins = await mountPins(mkRoom(localPins, nonLocalPins));
|
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(0);
|
|
|
|
|
|
|
|
// Pin the first message
|
|
|
|
localPins.push(pin1);
|
|
|
|
await emitPinUpdates(pins);
|
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(1);
|
|
|
|
|
|
|
|
// Pin the second message
|
|
|
|
nonLocalPins.push(pin2);
|
|
|
|
await emitPinUpdates(pins);
|
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates when messages are unpinned", async () => {
|
|
|
|
// Start with two pins
|
|
|
|
const localPins = [pin1];
|
|
|
|
const nonLocalPins = [pin2];
|
|
|
|
const pins = await mountPins(mkRoom(localPins, nonLocalPins));
|
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(2);
|
|
|
|
|
|
|
|
// Unpin the first message
|
|
|
|
localPins.pop();
|
|
|
|
await emitPinUpdates(pins);
|
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(1);
|
|
|
|
|
|
|
|
// Unpin the second message
|
|
|
|
nonLocalPins.pop();
|
|
|
|
await emitPinUpdates(pins);
|
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2022-03-03 12:54:44 +00: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 09:03:03 +00:00
|
|
|
unsigned: { redacted_because: {} as unknown as IEvent },
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
2022-03-03 12:54:44 +00:00
|
|
|
});
|
|
|
|
|
2022-03-04 23:15:03 +00:00
|
|
|
const pins = await mountPins(mkRoom([pin], []));
|
2022-03-03 12:54:44 +00:00
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
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 09:03:03 +00:00
|
|
|
unsigned: { redacted_because: {} as unknown as IEvent },
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
2022-03-03 12:54:44 +00:00
|
|
|
});
|
|
|
|
|
2022-03-04 23:15:03 +00:00
|
|
|
const pins = await mountPins(mkRoom([], [pin]));
|
2022-03-03 12:54:44 +00:00
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(0);
|
|
|
|
});
|
2022-03-03 17:20:13 +00:00
|
|
|
|
|
|
|
it("accounts for edits", async () => {
|
2022-03-21 09:03:03 +00: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 17:20:13 +00:00
|
|
|
},
|
2022-03-21 09:03:03 +00:00
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: RelationType.Replace,
|
|
|
|
event_id: pin1.getId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
cli.relations.mockResolvedValue({
|
|
|
|
originalEvent: pin1,
|
|
|
|
events: [messageEvent],
|
2022-03-03 17:20:13 +00:00
|
|
|
});
|
|
|
|
|
2022-03-04 23:15:03 +00:00
|
|
|
const pins = await mountPins(mkRoom([], [pin1]));
|
2022-03-03 17:20:13 +00:00
|
|
|
const pinTile = pins.find(PinnedEventTile);
|
|
|
|
expect(pinTile.length).toBe(1);
|
2022-03-04 23:15:03 +00:00
|
|
|
expect(pinTile.find(".mx_EventTile_body").text()).toEqual("First pinned message, edited");
|
|
|
|
});
|
|
|
|
|
|
|
|
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 11:39:16 +00:00
|
|
|
["@alice:example.org", 0] as [string, number],
|
|
|
|
["@bob:example.org", 0] as [string, number],
|
|
|
|
["@eve:example.org", 1] as [string, number],
|
2022-12-12 11:24:14 +00:00
|
|
|
].map(([user, option], i) =>
|
|
|
|
mkEvent({
|
2023-02-02 20:22:26 +00:00
|
|
|
...PollResponseEvent.from([answers[option as number].id], poll.getId()!).serialize(),
|
2022-12-12 11:24:14 +00:00
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: user as string,
|
|
|
|
}),
|
|
|
|
);
|
2023-02-02 20:22:26 +00:00
|
|
|
|
2022-03-04 23:15:03 +00:00
|
|
|
const end = mkEvent({
|
2023-02-02 20:22:26 +00:00
|
|
|
...PollEndEvent.from(poll.getId()!, "Closing the poll").serialize(),
|
2022-03-04 23:15:03 +00:00
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the responses available
|
2023-02-02 20:22:26 +00:00
|
|
|
cli.relations.mockImplementation(async (roomId, eventId, relationType, eventType, opts) => {
|
2022-03-04 23:15:03 +00:00
|
|
|
if (eventId === poll.getId() && relationType === RelationType.Reference) {
|
2023-02-02 20:22:26 +00: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-04 23:15:03 +00:00
|
|
|
}
|
2022-03-21 09:03:03 +00: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-04 23:15:03 +00:00
|
|
|
});
|
|
|
|
|
2023-02-02 20:22:26 +00: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();
|
|
|
|
|
2022-03-04 23:15:03 +00:00
|
|
|
const pinTile = pins.find(MPollBody);
|
2023-02-02 20:22:26 +00:00
|
|
|
|
2022-03-04 23:15:03 +00:00
|
|
|
expect(pinTile.length).toEqual(1);
|
2023-02-13 02:55:39 +00:00
|
|
|
expect(pinTile.find(".mx_PollOption_ended").length).toEqual(2);
|
|
|
|
expect(pinTile.find(".mx_PollOption_optionVoteCount").first().text()).toEqual("2 votes");
|
|
|
|
expect(pinTile.find(".mx_PollOption_optionVoteCount").last().text()).toEqual("1 vote");
|
2022-03-03 17:20:13 +00:00
|
|
|
});
|
2022-03-03 12:54:44 +00:00
|
|
|
});
|