2021-12-09 18:48:58 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-05-22 07:53:23 +08:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
2021-12-09 18:48:58 +08:00
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-12-09 18:48:58 +08:00
|
|
|
*/
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
import React, { ComponentProps } from "react";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { render, fireEvent, RenderResult, waitFor, waitForElementToBeRemoved } from "jest-matrix-react";
|
2024-03-18 22:40:52 +08:00
|
|
|
import { Room, RoomMember, MatrixError, IContent } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2021-12-09 18:48:58 +08:00
|
|
|
|
2023-06-15 15:46:19 +08:00
|
|
|
import { withClientContextRenderOptions, stubClient } from "../../../test-utils";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
|
|
|
import RoomPreviewBar from "../../../../src/components/views/rooms/RoomPreviewBar";
|
2022-10-24 16:58:36 +08:00
|
|
|
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
2021-12-09 18:48:58 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.mock("../../../../src/IdentityAuthClient", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
return jest.fn().mockImplementation(() => {
|
2022-12-12 19:24:14 +08:00
|
|
|
return { getAccessToken: jest.fn().mockResolvedValue("mock-token") };
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-02 20:02:17 +08:00
|
|
|
jest.useRealTimers();
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
const createRoom = (roomId: string, userId: string): Room => {
|
2023-06-06 01:12:23 +08:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2023-05-23 23:24:12 +08:00
|
|
|
const newRoom = new Room(roomId, cli, userId, {});
|
|
|
|
DMRoomMap.makeShared(cli).start();
|
2021-12-09 18:48:58 +08:00
|
|
|
return newRoom;
|
|
|
|
};
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const makeMockRoomMember = ({
|
2021-12-09 18:48:58 +08:00
|
|
|
userId,
|
2022-12-12 19:24:14 +08:00
|
|
|
isKicked,
|
2021-12-09 18:48:58 +08:00
|
|
|
membership,
|
2022-12-12 19:24:14 +08:00
|
|
|
content,
|
|
|
|
memberContent,
|
2023-09-07 16:07:43 +08:00
|
|
|
oldMembership,
|
2022-12-12 19:24:14 +08:00
|
|
|
}: {
|
|
|
|
userId?: string;
|
|
|
|
isKicked?: boolean;
|
2024-03-12 22:52:54 +08:00
|
|
|
membership?: KnownMembership.Invite | KnownMembership.Ban | KnownMembership.Leave;
|
2022-12-12 19:24:14 +08:00
|
|
|
content?: Partial<IContent>;
|
|
|
|
memberContent?: Partial<IContent>;
|
2024-03-12 22:52:54 +08:00
|
|
|
oldMembership?: KnownMembership.Join | KnownMembership.Knock;
|
2022-12-12 19:24:14 +08:00
|
|
|
}) =>
|
|
|
|
({
|
|
|
|
userId,
|
|
|
|
rawDisplayName: `${userId} name`,
|
|
|
|
isKicked: jest.fn().mockReturnValue(!!isKicked),
|
|
|
|
getContent: jest.fn().mockReturnValue(content || {}),
|
2023-09-07 16:07:43 +08:00
|
|
|
getPrevContent: jest.fn().mockReturnValue(content || {}),
|
2022-12-12 19:24:14 +08:00
|
|
|
membership,
|
|
|
|
events: {
|
|
|
|
member: {
|
|
|
|
getSender: jest.fn().mockReturnValue("@kicker:test.com"),
|
|
|
|
getContent: jest.fn().mockReturnValue({ reason: "test reason", ...memberContent }),
|
2023-09-07 16:07:43 +08:00
|
|
|
getPrevContent: jest.fn().mockReturnValue({ membership: oldMembership, ...memberContent }),
|
2022-12-12 19:24:14 +08:00
|
|
|
},
|
2021-12-09 18:48:58 +08:00
|
|
|
},
|
2024-01-03 02:56:39 +08:00
|
|
|
}) as unknown as RoomMember;
|
2021-12-09 18:48:58 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("<RoomPreviewBar />", () => {
|
|
|
|
const roomId = "RoomPreviewBar-test-room";
|
|
|
|
const userId = "@tester:test.com";
|
|
|
|
const inviterUserId = "@inviter:test.com";
|
|
|
|
const otherUserId = "@othertester:test.com";
|
2021-12-09 18:48:58 +08:00
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
const getComponent = (props: ComponentProps<typeof RoomPreviewBar> = {}) => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const defaultProps = {
|
2023-02-23 18:29:21 +08:00
|
|
|
roomId,
|
2021-12-09 18:48:58 +08:00
|
|
|
room: createRoom(roomId, userId),
|
|
|
|
};
|
2023-06-15 15:46:19 +08:00
|
|
|
return render(
|
|
|
|
<RoomPreviewBar {...defaultProps} {...props} />,
|
|
|
|
withClientContextRenderOptions(MatrixClientPeg.safeGet()),
|
|
|
|
);
|
2021-12-09 18:48:58 +08:00
|
|
|
};
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const isSpinnerRendered = (wrapper: RenderResult) => !!wrapper.container.querySelector(".mx_Spinner");
|
2022-10-24 16:58:36 +08:00
|
|
|
const getMessage = (wrapper: RenderResult) =>
|
2022-12-12 19:24:14 +08:00
|
|
|
wrapper.container.querySelector<HTMLDivElement>(".mx_RoomPreviewBar_message");
|
2022-10-24 16:58:36 +08:00
|
|
|
const getActions = (wrapper: RenderResult) =>
|
2022-12-12 19:24:14 +08:00
|
|
|
wrapper.container.querySelector<HTMLDivElement>(".mx_RoomPreviewBar_actions");
|
2022-10-24 16:58:36 +08:00
|
|
|
const getPrimaryActionButton = (wrapper: RenderResult) =>
|
2023-02-14 01:01:43 +08:00
|
|
|
getActions(wrapper)?.querySelector(".mx_AccessibleButton_kind_primary");
|
2022-10-24 16:58:36 +08:00
|
|
|
const getSecondaryActionButton = (wrapper: RenderResult) =>
|
2023-02-14 01:01:43 +08:00
|
|
|
getActions(wrapper)?.querySelector(".mx_AccessibleButton_kind_secondary");
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
stubClient();
|
2023-06-22 00:29:44 +08:00
|
|
|
MatrixClientPeg.get()!.getUserId = jest.fn().mockReturnValue(userId);
|
|
|
|
MatrixClientPeg.get()!.getSafeUserId = jest.fn().mockReturnValue(userId);
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getUserId = jest.fn().mockReturnValue(userId);
|
2023-06-15 15:46:19 +08:00
|
|
|
MatrixClientPeg.safeGet().getSafeUserId = jest.fn().mockReturnValue(userId);
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
const container = document.body.firstChild;
|
|
|
|
container && document.body.removeChild(container);
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders joining message", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ joining: true });
|
|
|
|
|
|
|
|
expect(isSpinnerRendered(component)).toBeTruthy();
|
2023-02-14 17:05:01 +08:00
|
|
|
expect(getMessage(component)?.textContent).toEqual("Joining…");
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders rejecting message", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ rejecting: true });
|
|
|
|
expect(isSpinnerRendered(component)).toBeTruthy();
|
2023-02-14 17:05:01 +08:00
|
|
|
expect(getMessage(component)?.textContent).toEqual("Rejecting invite…");
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders loading message", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ loading: true });
|
|
|
|
expect(isSpinnerRendered(component)).toBeTruthy();
|
2023-02-14 17:05:01 +08:00
|
|
|
expect(getMessage(component)?.textContent).toEqual("Loading…");
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders not logged in message", () => {
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().isGuest = jest.fn().mockReturnValue(true);
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ loading: true });
|
|
|
|
|
|
|
|
expect(isSpinnerRendered(component)).toBeFalsy();
|
2023-02-14 01:01:43 +08:00
|
|
|
expect(getMessage(component)?.textContent).toEqual("Join the conversation with an account");
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-10-24 16:58:36 +08:00
|
|
|
it("should send room oob data to start login", async () => {
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().isGuest = jest.fn().mockReturnValue(true);
|
2022-10-24 16:58:36 +08:00
|
|
|
const component = getComponent({
|
|
|
|
oobData: {
|
|
|
|
name: "Room Name",
|
|
|
|
avatarUrl: "mxc://foo/bar",
|
|
|
|
inviterName: "Charlie",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const dispatcherSpy = jest.fn();
|
|
|
|
const dispatcherRef = defaultDispatcher.register(dispatcherSpy);
|
|
|
|
|
2023-02-14 01:01:43 +08:00
|
|
|
expect(getMessage(component)?.textContent).toEqual("Join the conversation with an account");
|
|
|
|
fireEvent.click(getPrimaryActionButton(component)!);
|
2022-10-24 16:58:36 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
await waitFor(() =>
|
|
|
|
expect(dispatcherSpy).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
screenAfterLogin: {
|
|
|
|
screen: "room",
|
|
|
|
params: expect.objectContaining({
|
|
|
|
room_name: "Room Name",
|
|
|
|
room_avatar_url: "mxc://foo/bar",
|
|
|
|
inviter_name: "Charlie",
|
|
|
|
}),
|
|
|
|
},
|
2022-10-24 16:58:36 +08:00
|
|
|
}),
|
2022-12-12 19:24:14 +08:00
|
|
|
),
|
|
|
|
);
|
2022-10-24 16:58:36 +08:00
|
|
|
|
|
|
|
defaultDispatcher.unregister(dispatcherRef);
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders kicked message", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const room = createRoom(roomId, otherUserId);
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(makeMockRoomMember({ isKicked: true }));
|
2023-12-04 18:20:24 +08:00
|
|
|
const component = getComponent({ room, canAskToJoinAndMembershipIsLeave: true, promptAskToJoin: false });
|
2023-09-07 16:07:43 +08:00
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders denied request message", () => {
|
|
|
|
const room = createRoom(roomId, otherUserId);
|
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(
|
2024-03-12 22:52:54 +08:00
|
|
|
makeMockRoomMember({
|
|
|
|
isKicked: true,
|
|
|
|
membership: KnownMembership.Leave,
|
|
|
|
oldMembership: KnownMembership.Knock,
|
|
|
|
}),
|
2023-09-07 16:07:43 +08:00
|
|
|
);
|
|
|
|
const component = getComponent({ room, promptAskToJoin: true });
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2023-09-07 16:07:43 +08:00
|
|
|
it("triggers the primary action callback for denied request", () => {
|
|
|
|
const onForgetClick = jest.fn();
|
|
|
|
const room = createRoom(roomId, otherUserId);
|
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(
|
2024-03-12 22:52:54 +08:00
|
|
|
makeMockRoomMember({
|
|
|
|
isKicked: true,
|
|
|
|
membership: KnownMembership.Leave,
|
|
|
|
oldMembership: KnownMembership.Knock,
|
|
|
|
}),
|
2023-09-07 16:07:43 +08:00
|
|
|
);
|
|
|
|
const component = getComponent({ room, promptAskToJoin: true, onForgetClick });
|
|
|
|
|
|
|
|
fireEvent.click(getPrimaryActionButton(component)!);
|
|
|
|
expect(onForgetClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders banned message", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const room = createRoom(roomId, otherUserId);
|
2024-03-12 22:52:54 +08:00
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(makeMockRoomMember({ membership: KnownMembership.Ban }));
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ loading: true, room });
|
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("with an error", () => {
|
|
|
|
it("renders room not found error", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const error = new MatrixError({
|
2022-12-12 19:24:14 +08:00
|
|
|
errcode: "M_NOT_FOUND",
|
2021-12-09 18:48:58 +08:00
|
|
|
error: "Room not found",
|
|
|
|
});
|
|
|
|
const component = getComponent({ error });
|
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders other errors", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const error = new MatrixError({
|
2022-12-12 19:24:14 +08:00
|
|
|
errcode: "Something_else",
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
const component = getComponent({ error });
|
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders viewing room message when room an be previewed", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ canPreview: true });
|
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders viewing room message when room can not be previewed", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ canPreview: false });
|
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("with an invite", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const inviterName = inviterUserId;
|
|
|
|
const userMember = makeMockRoomMember({ userId });
|
|
|
|
const userMemberWithDmInvite = makeMockRoomMember({
|
2022-12-12 19:24:14 +08:00
|
|
|
userId,
|
2024-03-12 22:52:54 +08:00
|
|
|
membership: KnownMembership.Invite,
|
|
|
|
memberContent: { is_direct: true, membership: KnownMembership.Invite },
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
const inviterMember = makeMockRoomMember({
|
|
|
|
userId: inviterUserId,
|
|
|
|
content: {
|
2022-12-12 19:24:14 +08:00
|
|
|
"reason": "test",
|
|
|
|
"io.element.html_reason": "<h3>hello</h3>",
|
2021-12-09 18:48:58 +08:00
|
|
|
},
|
|
|
|
});
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("without an invited email", () => {
|
|
|
|
describe("for a non-dm room", () => {
|
2023-02-13 19:39:16 +08:00
|
|
|
const mockGetMember = (id: string) => {
|
2021-12-09 18:48:58 +08:00
|
|
|
if (id === userId) return userMember;
|
|
|
|
return inviterMember;
|
|
|
|
};
|
|
|
|
const onJoinClick = jest.fn();
|
|
|
|
const onRejectClick = jest.fn();
|
2023-02-13 19:39:16 +08:00
|
|
|
let room: Room;
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
room = createRoom(roomId, userId);
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(room, "getMember").mockImplementation(mockGetMember);
|
|
|
|
jest.spyOn(room.currentState, "getMember").mockImplementation(mockGetMember);
|
2021-12-09 18:48:58 +08:00
|
|
|
onJoinClick.mockClear();
|
|
|
|
onRejectClick.mockClear();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders invite message", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, room });
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders join and reject action buttons correctly", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick });
|
|
|
|
expect(getActions(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders reject and ignore action buttons when handler is provided", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const onRejectAndIgnoreClick = jest.fn();
|
|
|
|
const component = getComponent({
|
2022-12-12 19:24:14 +08:00
|
|
|
inviterName,
|
|
|
|
room,
|
|
|
|
onJoinClick,
|
|
|
|
onRejectClick,
|
|
|
|
onRejectAndIgnoreClick,
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
expect(getActions(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders join and reject action buttons in reverse order when room can previewed", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
// when room is previewed action buttons are rendered left to right, with primary on the right
|
|
|
|
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick, canPreview: true });
|
|
|
|
expect(getActions(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("joins room on primary button click", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick });
|
2023-02-14 01:01:43 +08:00
|
|
|
fireEvent.click(getPrimaryActionButton(component)!);
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(onJoinClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("rejects invite on secondary button click", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, room, onJoinClick, onRejectClick });
|
2023-02-14 01:01:43 +08:00
|
|
|
fireEvent.click(getSecondaryActionButton(component)!);
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(onRejectClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("for a dm room", () => {
|
2023-02-13 19:39:16 +08:00
|
|
|
const mockGetMember = (id: string) => {
|
2021-12-09 18:48:58 +08:00
|
|
|
if (id === userId) return userMemberWithDmInvite;
|
|
|
|
return inviterMember;
|
|
|
|
};
|
|
|
|
const onJoinClick = jest.fn();
|
|
|
|
const onRejectClick = jest.fn();
|
2023-02-13 19:39:16 +08:00
|
|
|
let room: Room;
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
room = createRoom(roomId, userId);
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(room, "getMember").mockImplementation(mockGetMember);
|
|
|
|
jest.spyOn(room.currentState, "getMember").mockImplementation(mockGetMember);
|
2021-12-09 18:48:58 +08:00
|
|
|
onJoinClick.mockClear();
|
|
|
|
onRejectClick.mockClear();
|
|
|
|
});
|
|
|
|
|
2023-05-22 07:53:23 +08:00
|
|
|
it("renders invite message", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, room });
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders join and reject action buttons with correct labels", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const onRejectAndIgnoreClick = jest.fn();
|
|
|
|
const component = getComponent({
|
2022-12-12 19:24:14 +08:00
|
|
|
inviterName,
|
|
|
|
room,
|
|
|
|
onJoinClick,
|
|
|
|
onRejectAndIgnoreClick,
|
|
|
|
onRejectClick,
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
expect(getActions(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("with an invited email", () => {
|
|
|
|
const invitedEmail = "test@test.com";
|
2021-12-09 18:48:58 +08:00
|
|
|
const mockThreePids = [
|
2022-12-12 19:24:14 +08:00
|
|
|
{ medium: "email", address: invitedEmail },
|
|
|
|
{ medium: "not-email", address: "address 2" },
|
2021-12-09 18:48:58 +08:00
|
|
|
];
|
|
|
|
|
2023-07-04 21:49:27 +08:00
|
|
|
const testJoinButton =
|
|
|
|
(props: ComponentProps<typeof RoomPreviewBar>, expectSecondaryButton = false) =>
|
|
|
|
async () => {
|
|
|
|
const onJoinClick = jest.fn();
|
|
|
|
const onRejectClick = jest.fn();
|
|
|
|
const component = getComponent({ ...props, onJoinClick, onRejectClick });
|
2024-08-07 01:22:02 +08:00
|
|
|
await waitFor(() => expect(getPrimaryActionButton(component)).toBeTruthy());
|
2023-07-04 21:49:27 +08:00
|
|
|
if (expectSecondaryButton) expect(getSecondaryActionButton(component)).toBeFalsy();
|
|
|
|
fireEvent.click(getPrimaryActionButton(component)!);
|
|
|
|
expect(onJoinClick).toHaveBeenCalled();
|
|
|
|
};
|
2021-12-09 18:48:58 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("when client fails to get 3PIDs", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
beforeEach(() => {
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThreePids = jest.fn().mockRejectedValue({ errCode: "TEST_ERROR" });
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders error message", async () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, invitedEmail });
|
2024-08-07 01:22:02 +08:00
|
|
|
await waitForElementToBeRemoved(() => component.queryByRole("progressbar"));
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders join button", testJoinButton({ inviterName, invitedEmail }));
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("when invitedEmail is not associated with current account", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
beforeEach(() => {
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThreePids = jest
|
2022-12-12 19:24:14 +08:00
|
|
|
.fn()
|
|
|
|
.mockResolvedValue({ threepids: mockThreePids.slice(1) });
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders invite message with invited email", async () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, invitedEmail });
|
2024-08-07 01:22:02 +08:00
|
|
|
await waitForElementToBeRemoved(() => component.queryByRole("progressbar"));
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders join button", testJoinButton({ inviterName, invitedEmail }));
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("when client has no identity server connected", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
beforeEach(() => {
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
|
|
|
|
MatrixClientPeg.safeGet().getIdentityServerUrl = jest.fn().mockReturnValue(false);
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders invite message with invited email", async () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, invitedEmail });
|
2024-08-07 01:22:02 +08:00
|
|
|
await waitForElementToBeRemoved(() => component.queryByRole("progressbar"));
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders join button", testJoinButton({ inviterName, invitedEmail }));
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("when client has an identity server connected", () => {
|
2021-12-09 18:48:58 +08:00
|
|
|
beforeEach(() => {
|
2023-06-06 01:12:23 +08:00
|
|
|
MatrixClientPeg.safeGet().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
|
|
|
|
MatrixClientPeg.safeGet().getIdentityServerUrl = jest.fn().mockReturnValue("identity.test");
|
|
|
|
MatrixClientPeg.safeGet().lookupThreePid = jest.fn().mockResolvedValue("identity.test");
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders email mismatch message when invite email mxid doesnt match", async () => {
|
2023-07-04 21:49:27 +08:00
|
|
|
MatrixClientPeg.safeGet().lookupThreePid = jest.fn().mockReturnValue({ mxid: "not userid" });
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, invitedEmail });
|
2024-08-07 01:22:02 +08:00
|
|
|
await waitForElementToBeRemoved(() => component.queryByRole("progressbar"));
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
2023-06-06 01:12:23 +08:00
|
|
|
expect(MatrixClientPeg.safeGet().lookupThreePid).toHaveBeenCalledWith(
|
2022-12-12 19:24:14 +08:00
|
|
|
"email",
|
|
|
|
invitedEmail,
|
|
|
|
"mock-token",
|
2021-12-09 18:48:58 +08:00
|
|
|
);
|
|
|
|
await testJoinButton({ inviterName, invitedEmail })();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders invite message when invite email mxid match", async () => {
|
2023-07-04 21:49:27 +08:00
|
|
|
MatrixClientPeg.safeGet().lookupThreePid = jest.fn().mockReturnValue({ mxid: userId });
|
2021-12-09 18:48:58 +08:00
|
|
|
const component = getComponent({ inviterName, invitedEmail });
|
2024-08-07 01:22:02 +08:00
|
|
|
await waitForElementToBeRemoved(() => component.queryByRole("progressbar"));
|
2021-12-09 18:48:58 +08:00
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
2023-07-04 21:49:27 +08:00
|
|
|
await testJoinButton({ inviterName, invitedEmail }, false)();
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-08-07 14:27:09 +08:00
|
|
|
|
|
|
|
describe("message case AskToJoin", () => {
|
|
|
|
it("renders the corresponding message", () => {
|
|
|
|
const component = getComponent({ promptAskToJoin: true });
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2023-12-04 18:20:24 +08:00
|
|
|
it("renders the corresponding message when kicked", () => {
|
|
|
|
const room = createRoom(roomId, otherUserId);
|
|
|
|
jest.spyOn(room, "getMember").mockReturnValue(makeMockRoomMember({ isKicked: true }));
|
|
|
|
const component = getComponent({ room, promptAskToJoin: true });
|
|
|
|
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2023-08-07 14:27:09 +08:00
|
|
|
it("renders the corresponding message with a generic title", () => {
|
|
|
|
const component = render(<RoomPreviewBar promptAskToJoin />);
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders the corresponding actions", () => {
|
|
|
|
const component = getComponent({ promptAskToJoin: true });
|
|
|
|
expect(getActions(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("triggers the primary action callback", () => {
|
|
|
|
const onSubmitAskToJoin = jest.fn();
|
|
|
|
const component = getComponent({ promptAskToJoin: true, onSubmitAskToJoin });
|
|
|
|
|
|
|
|
fireEvent.click(getPrimaryActionButton(component)!);
|
|
|
|
expect(onSubmitAskToJoin).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("triggers the primary action callback with a reason", () => {
|
|
|
|
const onSubmitAskToJoin = jest.fn();
|
|
|
|
const reason = "some reason";
|
|
|
|
const component = getComponent({ promptAskToJoin: true, onSubmitAskToJoin });
|
|
|
|
|
|
|
|
fireEvent.change(component.container.querySelector("textarea")!, { target: { value: reason } });
|
|
|
|
fireEvent.click(getPrimaryActionButton(component)!);
|
|
|
|
|
|
|
|
expect(onSubmitAskToJoin).toHaveBeenCalledWith(reason);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("message case Knocked", () => {
|
|
|
|
it("renders the corresponding message", () => {
|
|
|
|
const component = getComponent({ knocked: true });
|
|
|
|
expect(getMessage(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders the corresponding actions", () => {
|
|
|
|
const component = getComponent({ knocked: true, onCancelAskToJoin: () => {} });
|
|
|
|
expect(getActions(component)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("triggers the secondary action callback", () => {
|
|
|
|
const onCancelAskToJoin = jest.fn();
|
|
|
|
const component = getComponent({ knocked: true, onCancelAskToJoin });
|
|
|
|
|
|
|
|
fireEvent.click(getSecondaryActionButton(component)!);
|
|
|
|
expect(onCancelAskToJoin).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2021-12-09 18:48:58 +08:00
|
|
|
});
|