2022-02-08 12:14:52 +00: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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2022-08-02 13:10:43 +00:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mount } from "enzyme";
|
|
|
|
import { mocked } from "jest-mock";
|
2022-02-08 12:14:52 +00:00
|
|
|
import { act } from "react-dom/test-utils";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { Room, User, MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { Phase, VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
|
2022-02-08 12:14:52 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import UserInfo from "../../../../src/components/views/right_panel/UserInfo";
|
|
|
|
import { RightPanelPhases } from "../../../../src/stores/right-panel/RightPanelStorePhases";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import VerificationPanel from "../../../../src/components/views/right_panel/VerificationPanel";
|
|
|
|
import EncryptionInfo from "../../../../src/components/views/right_panel/EncryptionInfo";
|
2022-02-08 12:14:52 +00:00
|
|
|
|
|
|
|
const findByTestId = (component, id) => component.find(`[data-test-id="${id}"]`);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../../../src/utils/DMRoomMap", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const mock = {
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
getDMRoomsForUserId: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
shared: jest.fn().mockReturnValue(mock),
|
|
|
|
sharedInstance: mock,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("<UserInfo />", () => {
|
|
|
|
const defaultUserId = "@test:test";
|
2022-02-08 12:14:52 +00:00
|
|
|
const defaultUser = new User(defaultUserId);
|
|
|
|
|
2022-03-21 09:03:03 +00:00
|
|
|
const mockClient = mocked({
|
2022-02-08 12:14:52 +00:00
|
|
|
getUser: jest.fn(),
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
isUserIgnored: jest.fn(),
|
|
|
|
isCryptoEnabled: jest.fn(),
|
|
|
|
getUserId: jest.fn(),
|
|
|
|
on: jest.fn(),
|
|
|
|
isSynapseAdministrator: jest.fn().mockResolvedValue(false),
|
|
|
|
isRoomEncrypted: jest.fn().mockReturnValue(false),
|
|
|
|
doesServerSupportUnstableFeature: jest.fn().mockReturnValue(false),
|
2022-12-12 11:24:14 +00:00
|
|
|
mxcUrlToHttp: jest.fn().mockReturnValue("mock-mxcUrlToHttp"),
|
2022-05-04 14:37:41 +00:00
|
|
|
removeListener: jest.fn(),
|
2022-02-08 12:14:52 +00:00
|
|
|
currentState: {
|
|
|
|
on: jest.fn(),
|
|
|
|
},
|
2022-03-21 09:03:03 +00:00
|
|
|
} as unknown as MatrixClient);
|
2022-02-08 12:14:52 +00:00
|
|
|
|
|
|
|
const verificationRequest = {
|
2022-12-12 11:24:14 +00:00
|
|
|
pending: true,
|
|
|
|
on: jest.fn(),
|
|
|
|
phase: Phase.Ready,
|
2022-02-08 12:14:52 +00:00
|
|
|
channel: { transactionId: 1 },
|
|
|
|
otherPartySupportsMethod: jest.fn(),
|
|
|
|
} as unknown as VerificationRequest;
|
|
|
|
|
2022-03-21 09:03:03 +00:00
|
|
|
const defaultProps = {
|
|
|
|
user: defaultUser,
|
|
|
|
// idk what is wrong with this type
|
|
|
|
phase: RightPanelPhases.RoomMemberInfo as RightPanelPhases.RoomMemberInfo,
|
|
|
|
onClose: jest.fn(),
|
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const getComponent = (props = {}) =>
|
|
|
|
mount(<UserInfo {...defaultProps} {...props} />, {
|
2022-03-21 09:03:03 +00:00
|
|
|
wrappingComponent: MatrixClientContext.Provider,
|
|
|
|
wrappingComponentProps: { value: mockClient },
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2022-02-08 12:14:52 +00:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
2022-02-08 12:14:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-03-21 09:03:03 +00:00
|
|
|
mockClient.getUser.mockClear().mockReturnValue({} as unknown as User);
|
2022-02-08 12:14:52 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("closes on close button click", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const onClose = jest.fn();
|
|
|
|
const component = getComponent({ onClose });
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
findByTestId(component, "base-card-close-button").at(0).simulate("click");
|
2022-02-08 12:14:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(onClose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("without a room", () => {
|
|
|
|
it("does not render space header", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const component = getComponent();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(findByTestId(component, "space-header").length).toBeFalsy();
|
2022-02-08 12:14:52 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders user info", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const component = getComponent();
|
|
|
|
expect(component.find("BasicUserInfo").length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders encryption info panel without pending verification", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const phase = RightPanelPhases.EncryptionPanel;
|
|
|
|
const component = getComponent({ phase });
|
|
|
|
|
|
|
|
expect(component.find(EncryptionInfo).length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders encryption verification panel with pending verification", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const phase = RightPanelPhases.EncryptionPanel;
|
|
|
|
const component = getComponent({ phase, verificationRequest });
|
|
|
|
|
|
|
|
expect(component.find(EncryptionInfo).length).toBeFalsy();
|
|
|
|
expect(component.find(VerificationPanel).length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders close button correctly when encryption panel with a pending verification request", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const phase = RightPanelPhases.EncryptionPanel;
|
|
|
|
const component = getComponent({ phase, verificationRequest });
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(findByTestId(component, "base-card-close-button").at(0).props().title).toEqual("Cancel");
|
2022-02-08 12:14:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("with a room", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const room = {
|
2022-12-12 11:24:14 +00:00
|
|
|
roomId: "!fkfk",
|
2022-10-24 08:58:36 +00:00
|
|
|
getType: jest.fn().mockReturnValue(undefined),
|
2022-02-08 12:14:52 +00:00
|
|
|
isSpaceRoom: jest.fn().mockReturnValue(false),
|
|
|
|
getMember: jest.fn().mockReturnValue(undefined),
|
2022-12-12 11:24:14 +00:00
|
|
|
getMxcAvatarUrl: jest.fn().mockReturnValue("mock-avatar-url"),
|
|
|
|
name: "test room",
|
2022-02-08 12:14:52 +00:00
|
|
|
on: jest.fn(),
|
|
|
|
currentState: {
|
|
|
|
getStateEvents: jest.fn(),
|
|
|
|
on: jest.fn(),
|
|
|
|
},
|
|
|
|
} as unknown as Room;
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders user info", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const component = getComponent();
|
|
|
|
expect(component.find("BasicUserInfo").length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render space header when room is not a space room", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const component = getComponent({ room });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(findByTestId(component, "space-header").length).toBeFalsy();
|
2022-02-08 12:14:52 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders space header when room is a space room", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const spaceRoom = {
|
2022-12-12 11:24:14 +00:00
|
|
|
...room,
|
|
|
|
isSpaceRoom: jest.fn().mockReturnValue(true),
|
2022-02-08 12:14:52 +00:00
|
|
|
};
|
|
|
|
const component = getComponent({ room: spaceRoom });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(findByTestId(component, "space-header").length).toBeTruthy();
|
2022-02-08 12:14:52 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders encryption info panel without pending verification", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const phase = RightPanelPhases.EncryptionPanel;
|
|
|
|
const component = getComponent({ phase, room });
|
|
|
|
|
|
|
|
expect(component.find(EncryptionInfo).length).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders encryption verification panel with pending verification", () => {
|
2022-02-08 12:14:52 +00:00
|
|
|
const phase = RightPanelPhases.EncryptionPanel;
|
|
|
|
const component = getComponent({ phase, verificationRequest, room });
|
|
|
|
|
|
|
|
expect(component.find(EncryptionInfo).length).toBeFalsy();
|
|
|
|
expect(component.find(VerificationPanel).length).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|