* Module API experiments * Move ResizerNotifier into SDKContext so we don't have to pass it into RoomView * Add the MultiRoomViewStore * Make RoomViewStore able to take a roomId prop * Different interface to add space panel items A bit less flexible but probably simpler and will help keep things actually consistent rather than just allowing modules to stick any JSX into the space panel (which means they also have to worry about styling if they *do* want it to be consistent). * Allow space panel items to be updated and manage which one is selected, allowing module "spaces" to be considered spaces * Remove fetchRoomFn from SpaceNotificationStore which didn't really seem to have any point as it was only called from one place * Switch to using module api via .instance * Fairly awful workaround to actually break the dependency nightmare * Add test for multiroomviewstore * add test * Make room names deterministic So the tests don't fail if you add other tests or run them individually * Add test for builtinsapi * Update module api * RVS is not needed as prop anymore Since it's passed through context * Add roomId to prop * Remove RoomViewStore from state This is now accessed through class field * Fix test * No need to pass RVS from LoggedInView * Add RoomContextType * Implement new builtins api * Add tests * Fix import * Fix circular dependency issue * Fix import * Add more tests * Improve comment * room-id is optional * Update license * Add implementation for AccountDataApi * Add implementation for Room * Add implementation for ClientApi * Create ClientApi in Api.ts * Write tests * Use nullish coalescing assignment * Implement openRoom in NavigationApi * Write tests * Add implementation for StoresApi * Write tests * Fix circular dependency * Add comments in lieu of type and fix else block * Change to class field --------- Co-authored-by: R Midhun Suresh <hi@midhun.dev>
162 lines
6.2 KiB
TypeScript
162 lines
6.2 KiB
TypeScript
/*
|
||
Copyright 2024 New Vector Ltd.
|
||
Copyright 2015-2022 The Matrix.org Foundation C.I.C.
|
||
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
||
|
||
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 { render, screen } from "jest-matrix-react";
|
||
import { EventTimeline, type MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
||
import { KnownMembership } from "matrix-js-sdk/src/types";
|
||
|
||
import { LocalRoom } from "../../../../../src/models/LocalRoom";
|
||
import {
|
||
filterConsole,
|
||
mkEvent,
|
||
mkRoomMemberJoinEvent,
|
||
mkThirdPartyInviteEvent,
|
||
stubClient,
|
||
} from "../../../../test-utils";
|
||
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext";
|
||
import NewRoomIntro from "../../../../../src/components/views/rooms/NewRoomIntro";
|
||
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
|
||
import { DirectoryMember } from "../../../../../src/utils/direct-messages";
|
||
import { ScopedRoomContextProvider } from "../../../../../src/contexts/ScopedRoomContext.tsx";
|
||
import defaultDispatcher from "../../../../../src/dispatcher/dispatcher";
|
||
import type { RoomContextType } from "../../../../../src/contexts/RoomContext.ts";
|
||
|
||
const renderNewRoomIntro = (client: MatrixClient, room: Room | LocalRoom) => {
|
||
render(
|
||
<MatrixClientContext.Provider value={client}>
|
||
<ScopedRoomContextProvider {...({ room, roomId: room.roomId } as unknown as RoomContextType)}>
|
||
<NewRoomIntro />
|
||
</ScopedRoomContextProvider>
|
||
</MatrixClientContext.Provider>,
|
||
);
|
||
};
|
||
|
||
describe("NewRoomIntro", () => {
|
||
let client: MatrixClient;
|
||
const roomId = "!room:example.com";
|
||
const userId = "@user:example.com";
|
||
|
||
filterConsole("Room !room:example.com does not have an m.room.create event");
|
||
|
||
beforeEach(() => {
|
||
client = stubClient();
|
||
DMRoomMap.makeShared(client);
|
||
});
|
||
|
||
afterEach(() => {
|
||
jest.resetAllMocks();
|
||
});
|
||
|
||
describe("for a DM Room", () => {
|
||
beforeEach(() => {
|
||
jest.spyOn(DMRoomMap.shared(), "getUserIdForRoomId").mockReturnValue(userId);
|
||
const room = new Room(roomId, client, client.getUserId()!);
|
||
room.name = "test_room";
|
||
renderNewRoomIntro(client, room);
|
||
});
|
||
|
||
it("should render the expected intro", () => {
|
||
const expected = `This is the beginning of your direct message history with test_room.`;
|
||
screen.getByText((id, element) => element?.tagName === "SPAN" && element?.textContent === expected);
|
||
});
|
||
});
|
||
|
||
it("should render as expected for a DM room with a single third-party invite", () => {
|
||
const room = new Room(roomId, client, client.getSafeUserId());
|
||
room.currentState.setStateEvents([
|
||
mkRoomMemberJoinEvent(client.getSafeUserId(), room.roomId),
|
||
mkThirdPartyInviteEvent(client.getSafeUserId(), "user@example.com", room.roomId),
|
||
]);
|
||
jest.spyOn(DMRoomMap.shared(), "getUserIdForRoomId").mockReturnValue(userId);
|
||
jest.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([room.roomId]));
|
||
renderNewRoomIntro(client, room);
|
||
|
||
expect(screen.getByText("Once everyone has joined, you’ll be able to chat")).toBeInTheDocument();
|
||
expect(
|
||
screen.queryByText(
|
||
"Only the two of you are in this conversation, unless either of you invites anyone to join.",
|
||
),
|
||
).not.toBeInTheDocument();
|
||
});
|
||
|
||
describe("for a DM LocalRoom", () => {
|
||
beforeEach(() => {
|
||
jest.spyOn(DMRoomMap.shared(), "getUserIdForRoomId").mockReturnValue(userId);
|
||
const localRoom = new LocalRoom(roomId, client, client.getUserId()!);
|
||
localRoom.name = "test_room";
|
||
localRoom.targets.push(new DirectoryMember({ user_id: userId }));
|
||
renderNewRoomIntro(client, localRoom);
|
||
});
|
||
|
||
it("should render the expected intro", () => {
|
||
const expected = `Send your first message to invite test_room to chat`;
|
||
screen.getByText((id, element) => element?.tagName === "SPAN" && element?.textContent === expected);
|
||
});
|
||
});
|
||
|
||
describe("topic", () => {
|
||
let room: Room;
|
||
|
||
beforeEach(() => {
|
||
room = new Room(roomId, client, userId);
|
||
room.getLiveTimeline()
|
||
.getState(EventTimeline.FORWARDS)
|
||
?.setStateEvents([mkRoomMemberJoinEvent(client.getSafeUserId(), room.roomId)]);
|
||
jest.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([room.roomId]));
|
||
});
|
||
|
||
function addTopicToRoom(topic: string) {
|
||
const topicEvent = mkEvent({
|
||
type: "m.room.topic",
|
||
room: roomId,
|
||
user: userId,
|
||
content: {
|
||
topic,
|
||
},
|
||
ts: 123,
|
||
event: true,
|
||
});
|
||
|
||
room.addLiveEvents([topicEvent], { addToState: true });
|
||
}
|
||
|
||
it("should render the topic", () => {
|
||
addTopicToRoom("Test topic");
|
||
renderNewRoomIntro(client, room);
|
||
screen.getByText("Test topic");
|
||
});
|
||
|
||
it("should render a link in the topic", () => {
|
||
addTopicToRoom("This is a link: https://matrix.org/");
|
||
renderNewRoomIntro(client, room);
|
||
expect(screen.getByTestId("topic")).toMatchSnapshot();
|
||
});
|
||
|
||
it("should be able to add a topic", () => {
|
||
addTopicToRoom("Test topic");
|
||
jest.spyOn(room, "getMyMembership").mockReturnValue(KnownMembership.Join);
|
||
jest.spyOn(room.getLiveTimeline().getState(EventTimeline.FORWARDS)!, "maySendStateEvent").mockReturnValue(
|
||
true,
|
||
);
|
||
const spyDispatcher = jest.spyOn(defaultDispatcher, "dispatch");
|
||
|
||
renderNewRoomIntro(client, room);
|
||
screen.getByRole("button", { name: "edit" }).click();
|
||
expect(spyDispatcher).toHaveBeenCalledWith(
|
||
{
|
||
action: "open_room_settings",
|
||
room_id: room.roomId,
|
||
},
|
||
true,
|
||
);
|
||
});
|
||
});
|
||
});
|