2022-10-24 08:50:21 +02: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-10-28 13:48:15 +02:00
|
|
|
import React from "react";
|
|
|
|
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
2022-10-24 08:50:21 +02:00
|
|
|
import { MatrixClient, PendingEventOrdering } from "matrix-js-sdk/src/client";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { NotificationCountType, Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
|
|
|
|
import EventTile, { EventTileProps } from "../../../../src/components/views/rooms/EventTile";
|
2022-10-28 13:48:15 +02:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2022-10-24 08:50:21 +02:00
|
|
|
import RoomContext, { TimelineRenderingType } from "../../../../src/contexts/RoomContext";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-10-28 13:48:15 +02:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { getRoomContext, mkEvent, mkMessage, stubClient } from "../../../test-utils";
|
2022-10-24 08:50:21 +02:00
|
|
|
import { mkThread } from "../../../test-utils/threads";
|
|
|
|
|
|
|
|
describe("EventTile", () => {
|
|
|
|
const ROOM_ID = "!roomId:example.org";
|
|
|
|
let mxEvent: MatrixEvent;
|
|
|
|
let room: Room;
|
|
|
|
let client: MatrixClient;
|
|
|
|
// let changeEvent: (event: MatrixEvent) => void;
|
|
|
|
|
|
|
|
function TestEventTile(props: Partial<EventTileProps>) {
|
|
|
|
// const [event] = useState(mxEvent);
|
|
|
|
// Give a way for a test to update the event prop.
|
|
|
|
// changeEvent = setEvent;
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
return <EventTile mxEvent={mxEvent} {...props} />;
|
2022-10-24 08:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getComponent(
|
|
|
|
overrides: Partial<EventTileProps> = {},
|
|
|
|
renderingType: TimelineRenderingType = TimelineRenderingType.Room,
|
|
|
|
) {
|
|
|
|
const context = getRoomContext(room, {
|
|
|
|
timelineRenderingType: renderingType,
|
|
|
|
});
|
|
|
|
return render(
|
2022-10-28 13:48:15 +02:00
|
|
|
<MatrixClientContext.Provider value={client}>
|
|
|
|
<RoomContext.Provider value={context}>
|
|
|
|
<TestEventTile {...overrides} />
|
2022-12-12 12:24:14 +01:00
|
|
|
</RoomContext.Provider>
|
|
|
|
,
|
2022-10-28 13:48:15 +02:00
|
|
|
</MatrixClientContext.Provider>,
|
2022-10-24 08:50:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
|
|
|
stubClient();
|
|
|
|
client = MatrixClientPeg.get();
|
|
|
|
|
|
|
|
room = new Room(ROOM_ID, client, client.getUserId(), {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.spyOn(client, "getRoom").mockReturnValue(room);
|
2022-10-28 13:48:15 +02:00
|
|
|
jest.spyOn(client, "decryptEventIfNeeded").mockResolvedValue();
|
2022-12-13 16:09:15 +01:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name) => name === "feature_threadstable");
|
2022-10-24 08:50:21 +02:00
|
|
|
|
|
|
|
mxEvent = mkMessage({
|
|
|
|
room: room.roomId,
|
|
|
|
user: "@alice:example.org",
|
|
|
|
msg: "Hello world!",
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-28 13:48:15 +02:00
|
|
|
describe("EventTile thread summary", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(client, "supportsExperimentalThreads").mockReturnValue(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes the thread summary when thread is deleted", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const {
|
|
|
|
rootEvent,
|
|
|
|
events: [, reply],
|
|
|
|
} = mkThread({
|
2022-10-28 13:48:15 +02:00
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: "@alice:example.org",
|
|
|
|
participantUserIds: ["@alice:example.org"],
|
|
|
|
length: 2, // root + 1 answer
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
getComponent(
|
|
|
|
{
|
|
|
|
mxEvent: rootEvent,
|
|
|
|
},
|
|
|
|
TimelineRenderingType.Room,
|
|
|
|
);
|
2022-10-28 13:48:15 +02:00
|
|
|
|
|
|
|
await waitFor(() => expect(screen.queryByTestId("thread-summary")).not.toBeNull());
|
|
|
|
|
|
|
|
const redaction = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: EventType.RoomRedaction,
|
|
|
|
user: "@alice:example.org",
|
|
|
|
room: room.roomId,
|
|
|
|
redacts: reply.getId(),
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
act(() => room.processThreadedEvents([redaction], false));
|
|
|
|
|
|
|
|
await waitFor(() => expect(screen.queryByTestId("thread-summary")).toBeNull());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-24 08:50:21 +02:00
|
|
|
describe("EventTile renderingType: ThreadsList", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const { rootEvent } = mkThread({
|
|
|
|
room,
|
|
|
|
client,
|
|
|
|
authorId: "@alice:example.org",
|
|
|
|
participantUserIds: ["@alice:example.org"],
|
|
|
|
});
|
|
|
|
mxEvent = rootEvent;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shows an unread notification bage", () => {
|
|
|
|
const { container } = getComponent({}, TimelineRenderingType.ThreadsList);
|
|
|
|
|
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge")).toHaveLength(0);
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
room.setThreadUnreadNotificationCount(mxEvent.getId(), NotificationCountType.Total, 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge")).toHaveLength(1);
|
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge_highlighted")).toHaveLength(0);
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
room.setThreadUnreadNotificationCount(mxEvent.getId(), NotificationCountType.Highlight, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge")).toHaveLength(1);
|
|
|
|
expect(container.getElementsByClassName("mx_NotificationBadge_highlighted")).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|