2022-09-16 10:03:17 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { render, screen } from "@testing-library/react";
|
2023-02-28 10:39:35 +01:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-10-24 10:58:36 +02:00
|
|
|
import { RoomType } from "matrix-js-sdk/src/@types/event";
|
2023-02-14 13:21:29 +01:00
|
|
|
import { Room } from "matrix-js-sdk/src/matrix";
|
2022-09-16 10:03:17 +02:00
|
|
|
|
|
|
|
import InviteDialog from "../../../../src/components/views/dialogs/InviteDialog";
|
2023-02-14 13:21:29 +01:00
|
|
|
import { KIND_DM, KIND_INVITE } from "../../../../src/components/views/dialogs/InviteDialogTypes";
|
|
|
|
import { getMockClientWithEventEmitter, mkMembership, mkMessage, mkRoomCreateEvent } from "../../../test-utils";
|
2022-09-16 10:03:17 +02:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
|
|
|
import SdkConfig from "../../../../src/SdkConfig";
|
|
|
|
import { ValidatedServerConfig } from "../../../../src/utils/ValidatedServerConfig";
|
|
|
|
import { IConfigOptions } from "../../../../src/IConfigOptions";
|
|
|
|
|
2022-10-12 19:59:07 +02:00
|
|
|
const mockGetAccessToken = jest.fn().mockResolvedValue("getAccessToken");
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.mock("../../../../src/IdentityAuthClient", () =>
|
|
|
|
jest.fn().mockImplementation(() => ({
|
|
|
|
getAccessToken: mockGetAccessToken,
|
|
|
|
})),
|
|
|
|
);
|
2022-10-12 19:59:07 +02:00
|
|
|
|
2022-09-16 10:03:17 +02:00
|
|
|
describe("InviteDialog", () => {
|
|
|
|
const roomId = "!111111111111111111:example.org";
|
|
|
|
const aliceId = "@alice:example.org";
|
2023-02-14 13:21:29 +01:00
|
|
|
const aliceEmail = "foobar@email.com";
|
|
|
|
const bobId = "@bob:example.org";
|
2022-09-16 10:03:17 +02:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2023-02-14 13:21:29 +01:00
|
|
|
getUserId: jest.fn().mockReturnValue(bobId),
|
|
|
|
getSafeUserId: jest.fn().mockReturnValue(bobId),
|
2022-09-16 10:03:17 +02:00
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getRooms: jest.fn(),
|
|
|
|
getAccountData: jest.fn(),
|
|
|
|
getPushActionsForEvent: jest.fn(),
|
2022-12-12 12:24:14 +01:00
|
|
|
mxcUrlToHttp: jest.fn().mockReturnValue(""),
|
2022-09-16 10:03:17 +02:00
|
|
|
isRoomEncrypted: jest.fn().mockReturnValue(false),
|
|
|
|
getProfileInfo: jest.fn().mockRejectedValue({ errcode: "" }),
|
|
|
|
getIdentityServerUrl: jest.fn(),
|
|
|
|
searchUserDirectory: jest.fn().mockResolvedValue({}),
|
2022-10-12 19:59:07 +02:00
|
|
|
lookupThreePid: jest.fn(),
|
|
|
|
registerWithIdentityServer: jest.fn().mockResolvedValue({
|
|
|
|
access_token: "access_token",
|
|
|
|
token: "token",
|
|
|
|
}),
|
|
|
|
getOpenIdToken: jest.fn().mockResolvedValue({}),
|
|
|
|
getIdentityAccount: jest.fn().mockResolvedValue({}),
|
|
|
|
getTerms: jest.fn().mockResolvedValue({ policies: [] }),
|
2023-02-14 13:21:29 +01:00
|
|
|
supportsThreads: jest.fn().mockReturnValue(false),
|
|
|
|
isInitialSyncComplete: jest.fn().mockReturnValue(true),
|
2022-09-16 10:03:17 +02:00
|
|
|
});
|
2023-02-14 13:21:29 +01:00
|
|
|
let room: Room;
|
2022-09-16 10:03:17 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
SdkConfig.put({ validated_server_config: {} as ValidatedServerConfig } as IConfigOptions);
|
|
|
|
DMRoomMap.makeShared();
|
|
|
|
jest.clearAllMocks();
|
2023-02-14 13:21:29 +01:00
|
|
|
mockClient.getUserId.mockReturnValue(bobId);
|
|
|
|
|
|
|
|
room = new Room(roomId, mockClient, mockClient.getSafeUserId());
|
|
|
|
room.addLiveEvents([
|
|
|
|
mkMessage({
|
|
|
|
msg: "Hello",
|
|
|
|
relatesTo: undefined,
|
|
|
|
event: true,
|
|
|
|
room: roomId,
|
|
|
|
user: mockClient.getSafeUserId(),
|
|
|
|
ts: Date.now(),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
room.currentState.setStateEvents([
|
|
|
|
mkRoomCreateEvent(bobId, roomId),
|
|
|
|
mkMembership({
|
|
|
|
event: true,
|
|
|
|
room: roomId,
|
|
|
|
mship: "join",
|
|
|
|
user: aliceId,
|
|
|
|
skey: aliceId,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
jest.spyOn(DMRoomMap.shared(), "getUniqueRoomsWithIndividuals").mockReturnValue({
|
|
|
|
[aliceId]: room,
|
|
|
|
});
|
2022-09-16 10:03:17 +02:00
|
|
|
mockClient.getRooms.mockReturnValue([room]);
|
|
|
|
mockClient.getRoom.mockReturnValue(room);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2023-02-14 13:21:29 +01:00
|
|
|
jest.restoreAllMocks();
|
2022-09-16 10:03:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should label with space name", () => {
|
2023-02-14 13:21:29 +01:00
|
|
|
room.isSpaceRoom = jest.fn().mockReturnValue(true);
|
|
|
|
room.getType = jest.fn().mockReturnValue(RoomType.Space);
|
|
|
|
room.name = "Space";
|
2022-12-12 12:24:14 +01:00
|
|
|
render(<InviteDialog kind={KIND_INVITE} roomId={roomId} onFinished={jest.fn()} />);
|
2022-09-16 10:03:17 +02:00
|
|
|
|
|
|
|
expect(screen.queryByText("Invite to Space")).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should label with room name", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
render(<InviteDialog kind={KIND_INVITE} roomId={roomId} onFinished={jest.fn()} />);
|
2023-02-14 13:21:29 +01:00
|
|
|
expect(screen.getByText(`Invite to ${roomId}`)).toBeInTheDocument();
|
2022-09-16 10:03:17 +02:00
|
|
|
});
|
|
|
|
|
2022-10-12 19:59:07 +02:00
|
|
|
it("should suggest valid MXIDs even if unknown", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
render(
|
2022-09-16 10:03:17 +02:00
|
|
|
<InviteDialog
|
|
|
|
kind={KIND_INVITE}
|
|
|
|
roomId={roomId}
|
|
|
|
onFinished={jest.fn()}
|
|
|
|
initialText="@localpart:server.tld"
|
2022-12-12 12:24:14 +01:00
|
|
|
/>,
|
|
|
|
);
|
2022-09-16 10:03:17 +02:00
|
|
|
|
2022-10-12 19:59:07 +02:00
|
|
|
await screen.findAllByText("@localpart:server.tld"); // Using findAllByText as the MXID is used for name too
|
2022-09-16 10:03:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not suggest invalid MXIDs", () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
render(
|
2022-09-16 10:03:17 +02:00
|
|
|
<InviteDialog
|
|
|
|
kind={KIND_INVITE}
|
|
|
|
roomId={roomId}
|
|
|
|
onFinished={jest.fn()}
|
|
|
|
initialText="@localpart:server:tld"
|
2022-12-12 12:24:14 +01:00
|
|
|
/>,
|
|
|
|
);
|
2022-09-16 10:03:17 +02:00
|
|
|
|
|
|
|
expect(screen.queryByText("@localpart:server:tld")).toBeFalsy();
|
|
|
|
});
|
2022-10-12 19:59:07 +02:00
|
|
|
|
2023-02-14 13:21:29 +01:00
|
|
|
it.each([[KIND_DM], [KIND_INVITE]] as [typeof KIND_DM | typeof KIND_INVITE][])(
|
|
|
|
"should lookup inputs which look like email addresses (%s)",
|
|
|
|
async (kind: typeof KIND_DM | typeof KIND_INVITE) => {
|
|
|
|
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
|
|
|
|
mockClient.lookupThreePid.mockResolvedValue({
|
|
|
|
address: aliceEmail,
|
|
|
|
medium: "email",
|
|
|
|
mxid: aliceId,
|
|
|
|
});
|
|
|
|
mockClient.getProfileInfo.mockResolvedValue({
|
|
|
|
displayname: "Mrs Alice",
|
|
|
|
avatar_url: "mxc://foo/bar",
|
|
|
|
});
|
|
|
|
|
|
|
|
render(
|
|
|
|
<InviteDialog
|
|
|
|
kind={kind}
|
|
|
|
roomId={kind === KIND_INVITE ? roomId : ""}
|
|
|
|
onFinished={jest.fn()}
|
|
|
|
initialText={aliceEmail}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
await screen.findByText("Mrs Alice");
|
|
|
|
// expect the email and MXID to be visible
|
|
|
|
await screen.findByText(aliceId);
|
|
|
|
await screen.findByText(aliceEmail);
|
|
|
|
expect(mockClient.lookupThreePid).toHaveBeenCalledWith("email", aliceEmail, expect.anything());
|
|
|
|
expect(mockClient.getProfileInfo).toHaveBeenCalledWith(aliceId);
|
|
|
|
},
|
|
|
|
);
|
2022-10-12 19:59:07 +02:00
|
|
|
|
|
|
|
it("should suggest e-mail even if lookup fails", async () => {
|
|
|
|
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
|
|
|
|
mockClient.lookupThreePid.mockResolvedValue({});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
render(
|
|
|
|
<InviteDialog kind={KIND_INVITE} roomId={roomId} onFinished={jest.fn()} initialText="foobar@email.com" />,
|
|
|
|
);
|
2022-10-12 19:59:07 +02:00
|
|
|
|
|
|
|
await screen.findByText("foobar@email.com");
|
|
|
|
await screen.findByText("Invite by email");
|
|
|
|
});
|
2023-02-28 10:39:35 +01:00
|
|
|
|
|
|
|
it("should add pasted values", async () => {
|
|
|
|
mockClient.getIdentityServerUrl.mockReturnValue("https://identity-server");
|
|
|
|
mockClient.lookupThreePid.mockResolvedValue({});
|
|
|
|
|
|
|
|
render(<InviteDialog kind={KIND_INVITE} roomId={roomId} onFinished={jest.fn()} />);
|
|
|
|
|
|
|
|
const input = screen.getByTestId("invite-dialog-input");
|
|
|
|
input.focus();
|
|
|
|
await userEvent.paste(`${bobId} ${aliceEmail}`);
|
|
|
|
|
|
|
|
await screen.findByText(bobId);
|
|
|
|
await screen.findByText(aliceEmail);
|
|
|
|
expect(input).toHaveValue("");
|
|
|
|
});
|
2022-09-16 10:03:17 +02:00
|
|
|
});
|