Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com> Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Florian Duros <florian.duros@ormaz.fr> Co-authored-by: Kim Brose <kim.brose@nordeck.net> Co-authored-by: Florian Duros <florianduros@element.io> Co-authored-by: R Midhun Suresh <hi@midhun.dev> Co-authored-by: dbkr <986903+dbkr@users.noreply.github.com> Co-authored-by: ElementRobot <releases@riot.im> Co-authored-by: dbkr <dbkr@users.noreply.github.com> Co-authored-by: David Baker <dbkr@users.noreply.github.com> Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Co-authored-by: David Langley <davidl@element.io> Co-authored-by: Michael Weimann <michaelw@matrix.org> Co-authored-by: Timshel <Timshel@users.noreply.github.com> Co-authored-by: Sahil Silare <32628578+sahil9001@users.noreply.github.com> Co-authored-by: Will Hunt <will@half-shot.uk> Co-authored-by: Hubert Chathi <hubert@uhoreg.ca> Co-authored-by: Andrew Ferrazzutti <andrewf@element.io> Co-authored-by: Robin <robin@robin.town> Co-authored-by: Tulir Asokan <tulir@maunium.net>
151 lines
5.1 KiB
TypeScript
151 lines
5.1 KiB
TypeScript
/*
|
|
Copyright 2024 New Vector Ltd.
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
import React from "react";
|
|
import { render } from "jest-matrix-react";
|
|
import {
|
|
MatrixClient,
|
|
PendingEventOrdering,
|
|
EventStatus,
|
|
MatrixEvent,
|
|
Room,
|
|
MatrixError,
|
|
} from "matrix-js-sdk/src/matrix";
|
|
|
|
import RoomStatusBar, { getUnsentMessages } from "../../../../src/components/structures/RoomStatusBar";
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
import { mkEvent, stubClient } from "../../../test-utils/test-utils";
|
|
import { mkThread } from "../../../test-utils/threads";
|
|
|
|
describe("RoomStatusBar", () => {
|
|
const ROOM_ID = "!roomId:example.org";
|
|
let room: Room;
|
|
let client: MatrixClient;
|
|
let event: MatrixEvent;
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
|
|
stubClient();
|
|
client = MatrixClientPeg.safeGet();
|
|
client.getSyncStateData = jest.fn().mockReturnValue({});
|
|
room = new Room(ROOM_ID, client, client.getUserId()!, {
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
});
|
|
event = mkEvent({
|
|
event: true,
|
|
type: "m.room.message",
|
|
user: "@user1:server",
|
|
room: "!room1:server",
|
|
content: {},
|
|
});
|
|
event.status = EventStatus.NOT_SENT;
|
|
});
|
|
|
|
const getComponent = () =>
|
|
render(<RoomStatusBar room={room} />, {
|
|
wrapper: ({ children }) => (
|
|
<MatrixClientContext.Provider value={client}>{children}</MatrixClientContext.Provider>
|
|
),
|
|
});
|
|
|
|
describe("getUnsentMessages", () => {
|
|
it("returns no unsent messages", () => {
|
|
expect(getUnsentMessages(room)).toHaveLength(0);
|
|
});
|
|
|
|
it("checks the event status", () => {
|
|
room.addPendingEvent(event, "123");
|
|
|
|
expect(getUnsentMessages(room)).toHaveLength(1);
|
|
event.status = EventStatus.SENT;
|
|
|
|
expect(getUnsentMessages(room)).toHaveLength(0);
|
|
});
|
|
|
|
it("only returns events related to a thread", () => {
|
|
room.addPendingEvent(event, "123");
|
|
|
|
const { rootEvent, events } = mkThread({
|
|
room,
|
|
client,
|
|
authorId: "@alice:example.org",
|
|
participantUserIds: ["@alice:example.org"],
|
|
length: 2,
|
|
});
|
|
rootEvent.status = EventStatus.NOT_SENT;
|
|
room.addPendingEvent(rootEvent, rootEvent.getId()!);
|
|
for (const event of events) {
|
|
event.status = EventStatus.NOT_SENT;
|
|
room.addPendingEvent(event, Date.now() + Math.random() + "");
|
|
}
|
|
|
|
const pendingEvents = getUnsentMessages(room, rootEvent.getId());
|
|
|
|
expect(pendingEvents[0].threadRootId).toBe(rootEvent.getId());
|
|
expect(pendingEvents[1].threadRootId).toBe(rootEvent.getId());
|
|
expect(pendingEvents[2].threadRootId).toBe(rootEvent.getId());
|
|
|
|
// Filters out the non thread events
|
|
expect(pendingEvents.every((ev) => ev.getId() !== event.getId())).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("<RoomStatusBar />", () => {
|
|
it("should render nothing when room has no error or unsent messages", () => {
|
|
const { container } = getComponent();
|
|
expect(container.firstChild).toBe(null);
|
|
});
|
|
|
|
describe("unsent messages", () => {
|
|
it("should render warning when messages are unsent due to consent", () => {
|
|
const unsentMessage = mkEvent({
|
|
event: true,
|
|
type: "m.room.message",
|
|
user: "@user1:server",
|
|
room: "!room1:server",
|
|
content: {},
|
|
});
|
|
unsentMessage.status = EventStatus.NOT_SENT;
|
|
unsentMessage.error = new MatrixError({
|
|
errcode: "M_CONSENT_NOT_GIVEN",
|
|
data: { consent_uri: "terms.com" },
|
|
});
|
|
|
|
room.addPendingEvent(unsentMessage, "123");
|
|
|
|
const { container } = getComponent();
|
|
|
|
expect(container).toMatchSnapshot();
|
|
});
|
|
|
|
it("should render warning when messages are unsent due to resource limit", () => {
|
|
const unsentMessage = mkEvent({
|
|
event: true,
|
|
type: "m.room.message",
|
|
user: "@user1:server",
|
|
room: "!room1:server",
|
|
content: {},
|
|
});
|
|
unsentMessage.status = EventStatus.NOT_SENT;
|
|
unsentMessage.error = new MatrixError({
|
|
errcode: "M_RESOURCE_LIMIT_EXCEEDED",
|
|
data: { limit_type: "monthly_active_user" },
|
|
});
|
|
|
|
room.addPendingEvent(unsentMessage, "123");
|
|
|
|
const { container } = getComponent();
|
|
|
|
expect(container).toMatchSnapshot();
|
|
});
|
|
});
|
|
});
|
|
});
|