2022-03-03 13:54:44 +01: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";
|
|
|
|
import { mount } from "enzyme";
|
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2022-03-03 18:20:13 +01:00
|
|
|
import { EventType, RelationType, MsgType } from "matrix-js-sdk/src/@types/event";
|
2022-03-05 00:15:03 +01:00
|
|
|
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
|
|
|
import {
|
|
|
|
M_POLL_RESPONSE,
|
|
|
|
M_POLL_END,
|
|
|
|
M_POLL_KIND_DISCLOSED,
|
|
|
|
PollStartEvent,
|
|
|
|
PollResponseEvent,
|
|
|
|
PollEndEvent,
|
|
|
|
} from "matrix-events-sdk";
|
2022-03-03 13:54:44 +01:00
|
|
|
|
|
|
|
import "../../../skinned-sdk";
|
2022-03-03 18:20:13 +01:00
|
|
|
import {
|
|
|
|
stubClient,
|
|
|
|
wrapInMatrixClientContext,
|
|
|
|
mkStubRoom,
|
|
|
|
mkEvent,
|
|
|
|
mkMessage,
|
|
|
|
} from "../../../test-utils";
|
2022-03-03 13:54:44 +01:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import _PinnedMessagesCard from "../../../../src/components/views/right_panel/PinnedMessagesCard";
|
|
|
|
import PinnedEventTile from "../../../../src/components/views/rooms/PinnedEventTile";
|
2022-03-05 00:15:03 +01:00
|
|
|
import MPollBody from "../../../../src/components/views/messages/MPollBody.tsx";
|
|
|
|
|
|
|
|
const PinnedMessagesCard = wrapInMatrixClientContext(_PinnedMessagesCard);
|
2022-03-03 13:54:44 +01:00
|
|
|
|
|
|
|
describe("<PinnedMessagesCard />", () => {
|
|
|
|
stubClient();
|
|
|
|
const cli = MatrixClientPeg.get();
|
2022-03-05 00:15:03 +01:00
|
|
|
cli.getUserId.mockReturnValue("@alice:example.org");
|
2022-03-03 13:54:44 +01:00
|
|
|
cli.setRoomAccountData = () => {};
|
|
|
|
cli.relations = jest.fn().mockResolvedValue({ events: [] });
|
|
|
|
|
2022-03-05 00:15:03 +01:00
|
|
|
const mkRoom = (localPins: MatrixEvent[], nonLocalPins: MatrixEvent[]): Room => {
|
2022-03-03 18:20:13 +01:00
|
|
|
const room = mkStubRoom("!room:example.org");
|
2022-03-05 00:15:03 +01:00
|
|
|
// Deferred since we may be adding or removing pins later
|
|
|
|
const pins = () => [...localPins, ...nonLocalPins];
|
2022-03-03 13:54:44 +01:00
|
|
|
|
|
|
|
// Insert pin IDs into room state
|
2022-03-05 00:15:03 +01:00
|
|
|
room.currentState.getStateEvents.mockImplementation(() => mkEvent({
|
2022-03-03 13:54:44 +01:00
|
|
|
event: true,
|
|
|
|
type: EventType.RoomPinnedEvents,
|
|
|
|
content: {
|
2022-03-05 00:15:03 +01:00
|
|
|
pinned: pins().map(e => e.getId()),
|
2022-03-03 13:54:44 +01:00
|
|
|
},
|
2022-03-05 00:15:03 +01:00
|
|
|
}));
|
2022-03-03 13:54:44 +01:00
|
|
|
|
|
|
|
// Insert local pins into local timeline set
|
|
|
|
room.getUnfilteredTimelineSet = () => ({
|
|
|
|
getTimelineForEvent: () => ({
|
|
|
|
getEvents: () => localPins,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return all pins over fetchRoomEvent
|
2022-03-05 00:15:03 +01:00
|
|
|
cli.fetchRoomEvent = (roomId, eventId) => pins().find(e => e.getId() === eventId)?.event;
|
2022-03-03 13:54:44 +01:00
|
|
|
|
|
|
|
return room;
|
|
|
|
};
|
|
|
|
|
2022-03-05 00:15:03 +01:00
|
|
|
const mountPins = async (room: Room): ReactWrapper => {
|
|
|
|
let pins;
|
|
|
|
await act(async () => {
|
|
|
|
pins = mount(<PinnedMessagesCard room={room} onClose={() => {}} />);
|
|
|
|
// Wait a tick for state updates
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
});
|
|
|
|
pins.update();
|
|
|
|
|
|
|
|
return pins;
|
|
|
|
};
|
|
|
|
|
|
|
|
const emitPinUpdates = async (pins: ReactWrapper) => {
|
|
|
|
const room = pins.props().room;
|
|
|
|
const pinListener = room.currentState.on.mock.calls
|
|
|
|
.find(([eventName, listener]) => eventName === RoomStateEvent.Events)[1];
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
// Emit the update
|
|
|
|
pinListener(room.currentState.getStateEvents());
|
|
|
|
// Wait a tick for state updates
|
|
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
|
|
});
|
|
|
|
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
|
|
|
|
const localPins = [];
|
|
|
|
const nonLocalPins = [];
|
|
|
|
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 13:54:44 +01:00
|
|
|
it("hides unpinnable events found in local timeline", async () => {
|
|
|
|
// Redacted messages are unpinnable
|
|
|
|
const pin = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
content: {},
|
|
|
|
unsigned: { redacted_because: {} },
|
|
|
|
});
|
|
|
|
|
2022-03-05 00:15:03 +01:00
|
|
|
const pins = await mountPins(mkRoom([pin], []));
|
2022-03-03 13:54:44 +01: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: {},
|
|
|
|
unsigned: { redacted_because: {} },
|
|
|
|
});
|
|
|
|
|
2022-03-05 00:15:03 +01:00
|
|
|
const pins = await mountPins(mkRoom([], [pin]));
|
2022-03-03 13:54:44 +01:00
|
|
|
expect(pins.find(PinnedEventTile).length).toBe(0);
|
|
|
|
});
|
2022-03-03 18:20:13 +01:00
|
|
|
|
|
|
|
it("accounts for edits", async () => {
|
|
|
|
cli.relations.mockResolvedValue({
|
|
|
|
events: [mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomMessage,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
content: {
|
|
|
|
"msgtype": MsgType.Text,
|
2022-03-05 00:15:03 +01:00
|
|
|
"body": " * First pinned message, edited",
|
2022-03-03 18:20:13 +01:00
|
|
|
"m.new_content": {
|
|
|
|
msgtype: MsgType.Text,
|
2022-03-05 00:15:03 +01:00
|
|
|
body: "First pinned message, edited",
|
2022-03-03 18:20:13 +01:00
|
|
|
},
|
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: RelationType.Replace,
|
2022-03-05 00:15:03 +01:00
|
|
|
event_id: pin1.getId(),
|
2022-03-03 18:20:13 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})],
|
|
|
|
});
|
|
|
|
|
2022-03-05 00:15:03 +01:00
|
|
|
const pins = await mountPins(mkRoom([], [pin1]));
|
2022-03-03 18:20:13 +01:00
|
|
|
const pinTile = pins.find(PinnedEventTile);
|
|
|
|
expect(pinTile.length).toBe(1);
|
2022-03-05 00:15:03 +01: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 = [
|
|
|
|
["@alice:example.org", 0],
|
|
|
|
["@bob:example.org", 0],
|
|
|
|
["@eve:example.org", 1],
|
|
|
|
].map(([user, option], i) => mkEvent({
|
|
|
|
...PollResponseEvent.from([answers[option].id], poll.getId()).serialize(),
|
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user,
|
|
|
|
}));
|
|
|
|
const end = mkEvent({
|
|
|
|
...PollEndEvent.from(poll.getId(), "Closing the poll").serialize(),
|
|
|
|
event: true,
|
|
|
|
room: "!room:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the responses available
|
2022-03-10 19:14:34 +01:00
|
|
|
cli.relations.mockImplementation((roomId, eventId, relationType, eventType, { from }) => {
|
2022-03-05 00:15:03 +01:00
|
|
|
if (eventId === poll.getId() && relationType === RelationType.Reference) {
|
|
|
|
switch (eventType) {
|
2022-03-10 19:14:34 +01:00
|
|
|
case M_POLL_RESPONSE.name:
|
|
|
|
// Paginate the results, for added challenge
|
|
|
|
return (from === "page2") ?
|
|
|
|
{ events: responses.slice(2) } :
|
|
|
|
{ events: responses.slice(0, 2), nextBatch: "page2" };
|
|
|
|
case M_POLL_END.name:
|
|
|
|
return { events: [end] };
|
2022-03-05 00:15:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return { events: [] };
|
|
|
|
});
|
|
|
|
|
|
|
|
const pins = await mountPins(mkRoom([], [poll]));
|
|
|
|
const pinTile = pins.find(MPollBody);
|
|
|
|
expect(pinTile.length).toEqual(1);
|
|
|
|
expect(pinTile.find(".mx_MPollBody_option_ended").length).toEqual(2);
|
|
|
|
expect(pinTile.find(".mx_MPollBody_optionVoteCount").first().text()).toEqual("2 votes");
|
|
|
|
expect(pinTile.find(".mx_MPollBody_optionVoteCount").last().text()).toEqual("1 vote");
|
2022-03-03 18:20:13 +01:00
|
|
|
});
|
2022-03-03 13:54:44 +01:00
|
|
|
});
|