mirror of
https://github.com/Novaloop-AG/healthchat-web.git
synced 2026-12-03 21:51:06 +00:00
62f1dfdefa
* Stop messages flickering through the file panel as they are sent A filtered timeline set is handed the room's pending events unless it is told otherwise, and those are the whole room's pending events rather than the ones the filter would have kept. So every message typed into the room appeared in the file panel for as long as it took the remote echo to arrive, at which point the filter finally threw it out again. The file panel now asks for its timeline set without them. Nothing is lost by that: a file sent from the panel still appears the moment it is sent, because the panel adds live events itself and checks the message type while doing so, which the pending list never did. Tests: the file panel asks for a timeline set which does not carry the room's pending events. * Filter the pending events a filtered timeline set shows Suppressing the file panel's pending events wholesale was the wrong lever: it is an all-or-nothing switch on the room's detached pending list, so it also hid files the user had just sent, and the e2e caught the panel staying empty. Filter them instead. A timeline set already carries the filter it was built with, so the pending events it hands the panel can be held to the same test as the events the server returns. Files being sent still appear immediately; the text messages that used to flicker through never qualify. Panels with no filter, which is every other caller, are untouched. --------- Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
126 lines
5.0 KiB
TypeScript
126 lines
5.0 KiB
TypeScript
/*
|
|
Copyright 2024 New Vector Ltd.
|
|
Copyright 2024 The Matrix.org Foundation C.I.C.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
import React from "react";
|
|
import { EventStatus, EventTimelineSet, Filter, PendingEventOrdering, Room, RoomEvent } from "matrix-js-sdk/src/matrix";
|
|
import { screen, render, waitFor } from "jest-matrix-react";
|
|
import { mocked } from "jest-mock";
|
|
|
|
import FilePanel from "../../../../src/components/structures/FilePanel";
|
|
import { clientAndSDKContextRenderOptions, mkEvent, stubClient } from "../../../test-utils";
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
import { SDKContextClass } from "../../../../src/contexts/SDKContextClass.ts";
|
|
|
|
jest.mock("matrix-js-sdk/src/matrix", () => ({
|
|
...jest.requireActual("matrix-js-sdk/src/matrix"),
|
|
TimelineWindow: jest.fn().mockReturnValue({
|
|
load: jest.fn().mockResolvedValue(null),
|
|
getEvents: jest.fn().mockReturnValue([]),
|
|
canPaginate: jest.fn().mockReturnValue(false),
|
|
}),
|
|
}));
|
|
|
|
describe("FilePanel", () => {
|
|
beforeEach(() => {
|
|
stubClient();
|
|
});
|
|
|
|
it("renders empty state", async () => {
|
|
const cli = MatrixClientPeg.safeGet();
|
|
const room = new Room("!room:server", cli, cli.getSafeUserId(), {
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
});
|
|
const timelineSet = new EventTimelineSet(room);
|
|
room.getOrCreateFilteredTimelineSet = jest.fn().mockReturnValue(timelineSet);
|
|
mocked(cli.getRoom).mockReturnValue(room);
|
|
|
|
const { asFragment } = render(
|
|
<FilePanel roomId={room.roomId} onClose={jest.fn()} />,
|
|
clientAndSDKContextRenderOptions(cli, SDKContextClass.instance),
|
|
);
|
|
await waitFor(() => {
|
|
expect(screen.getByText("No files visible in this room")).toBeInTheDocument();
|
|
});
|
|
expect(asFragment()).toMatchSnapshot();
|
|
});
|
|
|
|
it("does not show a pending message that its filter rejects", async () => {
|
|
const cli = MatrixClientPeg.safeGet();
|
|
const room = new Room("!room:server", cli, cli.getSafeUserId(), {
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
});
|
|
|
|
const filter = new Filter(cli.getSafeUserId());
|
|
filter.setDefinition({ room: { timeline: { contains_url: true, types: ["m.room.message"] } } });
|
|
const timelineSet = new EventTimelineSet(room, { filter });
|
|
room.getOrCreateFilteredTimelineSet = jest.fn().mockReturnValue(timelineSet);
|
|
mocked(cli.getRoom).mockReturnValue(room);
|
|
|
|
// A text message being sent carries no url, so the file filter rejects it. It is still one of
|
|
// the room's pending events, which the panel is handed in full.
|
|
const pending = mkEvent({
|
|
event: true,
|
|
type: "m.room.message",
|
|
user: cli.getSafeUserId(),
|
|
room: room.roomId,
|
|
content: { msgtype: "m.text", body: "a message being sent" },
|
|
});
|
|
pending.setStatus(EventStatus.SENDING);
|
|
room.addPendingEvent(pending, "txn-text");
|
|
|
|
render(
|
|
<FilePanel roomId={room.roomId} onClose={jest.fn()} />,
|
|
clientAndSDKContextRenderOptions(cli, SDKContextClass.instance),
|
|
);
|
|
await screen.findByText("No files visible in this room");
|
|
|
|
expect(screen.queryByText("a message being sent")).not.toBeInTheDocument();
|
|
});
|
|
|
|
describe("addEncryptedLiveEvent", () => {
|
|
it("should add file msgtype event to filtered timelineSet", async () => {
|
|
const cli = MatrixClientPeg.safeGet();
|
|
const room = new Room("!room:server", cli, cli.getSafeUserId(), {
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
});
|
|
cli.reEmitter.reEmit(room, [RoomEvent.Timeline]);
|
|
const timelineSet = new EventTimelineSet(room);
|
|
room.getOrCreateFilteredTimelineSet = jest.fn().mockReturnValue(timelineSet);
|
|
mocked(cli.getRoom).mockReturnValue(room);
|
|
|
|
let filePanel: FilePanel | null;
|
|
render(
|
|
<FilePanel
|
|
roomId={room.roomId}
|
|
onClose={jest.fn()}
|
|
ref={(ref) => {
|
|
filePanel = ref;
|
|
}}
|
|
/>,
|
|
clientAndSDKContextRenderOptions(cli, SDKContextClass.instance),
|
|
);
|
|
await screen.findByText("No files visible in this room");
|
|
|
|
const event = mkEvent({
|
|
type: "m.room.message",
|
|
user: cli.getSafeUserId(),
|
|
room: room.roomId,
|
|
content: {
|
|
body: "hello",
|
|
url: "mxc://matrix.org/1234",
|
|
msgtype: "m.file",
|
|
},
|
|
event: true,
|
|
});
|
|
filePanel!.addEncryptedLiveEvent(event);
|
|
|
|
expect(timelineSet.getLiveTimeline().getEvents()).toContain(event);
|
|
});
|
|
});
|
|
});
|