From b6b676836f59ff97712671942dd9676738f10aea Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Tue, 8 Feb 2022 11:58:37 +0100 Subject: [PATCH 1/3] unit test main paths in UserInfo component Signed-off-by: Kerry Archibald --- src/components/views/right_panel/BaseCard.tsx | 1 + .../views/right_panel/EncryptionInfo.tsx | 2 +- .../views/right_panel/EncryptionPanel.tsx | 2 + src/components/views/right_panel/UserInfo.tsx | 9 +- .../views/right_panel/UserInfo-test.tsx | 177 ++++++++++++++++++ 5 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 test/components/views/right_panel/UserInfo-test.tsx diff --git a/src/components/views/right_panel/BaseCard.tsx b/src/components/views/right_panel/BaseCard.tsx index 8226b420726..0ecf0e5e6bd 100644 --- a/src/components/views/right_panel/BaseCard.tsx +++ b/src/components/views/right_panel/BaseCard.tsx @@ -69,6 +69,7 @@ const BaseCard: React.FC = ({ let closeButton; if (onClose) { closeButton = = ({ } return -
+

{ _t("Encryption") }

{ description }
diff --git a/src/components/views/right_panel/EncryptionPanel.tsx b/src/components/views/right_panel/EncryptionPanel.tsx index a5e34ad94e9..808ae12f253 100644 --- a/src/components/views/right_panel/EncryptionPanel.tsx +++ b/src/components/views/right_panel/EncryptionPanel.tsx @@ -106,6 +106,7 @@ const EncryptionPanel: React.FC = (props: IProps) => { setPhase(request.phase); } }, [onClose, request]); + useEventEmitter(request, "change", changeHandler); const onStartVerification = useCallback(async () => { @@ -131,6 +132,7 @@ const EncryptionPanel: React.FC = (props: IProps) => { const isSelfVerification = request ? request.isSelfVerification : member.userId === MatrixClientPeg.get().getUserId(); + if (!request || requested) { const initiatedByMe = (!request && isRequesting) || (request && request.initiatedByMe); return ( diff --git a/src/components/views/right_panel/UserInfo.tsx b/src/components/views/right_panel/UserInfo.tsx index 86c59287216..99cecb5c140 100644 --- a/src/components/views/right_panel/UserInfo.tsx +++ b/src/components/views/right_panel/UserInfo.tsx @@ -1096,6 +1096,7 @@ function useRoomPermissions(cli: MatrixClient, room: Room, user: RoomMember): IR modifyLevelMax, }); }, [cli, user, room]); + useEventEmitter(cli, "RoomState.members", updateRoomPermissions); useEffect(() => { updateRoomPermissions(); @@ -1282,7 +1283,7 @@ export const useDevices = (userId: string) => { return devices; }; -const BasicUserInfo: React.FC<{ +export const BasicUserInfo: React.FC<{ room: Room; member: User | RoomMember; groupId: string; @@ -1702,16 +1703,16 @@ const UserInfo: React.FC = ({ let scopeHeader; if (SpaceStore.spacesEnabled && room?.isSpaceRoom()) { - scopeHeader =
+ scopeHeader =
; } - const header = + const header = <> { scopeHeader } - ; + ; return component.find(`[data-test-id="${id}"]`); + +jest.mock('../../../../src/utils/DMRoomMap', () => { + const mock = { + getUserIdForRoomId: jest.fn(), + getDMRoomsForUserId: jest.fn(), + }; + + return { + shared: jest.fn().mockReturnValue(mock), + sharedInstance: mock, + }; +}); + +describe('', () => { + const defaultUserId = '@test:test'; + const defaultUser = new User(defaultUserId); + + const defaultProps = { + user: defaultUser, + phase: RightPanelPhases.RoomMemberInfo, + onClose: jest.fn(), + }; + + const mockClient = { + 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), + mxcUrlToHttp: jest.fn().mockReturnValue('mock-mxcUrlToHttp'), + removeListerner: jest.fn(), + currentState: { + on: jest.fn(), + }, + }; + + const verificationRequest = { + pending: true, on: jest.fn(), phase: Phase.Ready, + channel: { transactionId: 1 }, + otherPartySupportsMethod: jest.fn(), + } as unknown as VerificationRequest; + + const getComponent = (props = {}) => mount(, { + wrappingComponent: MatrixClientContext.Provider, + wrappingComponentProps: { value: mockClient }, + }); + + beforeAll(() => { + jest.spyOn(MatrixClientPeg, 'get').mockReturnValue(mockClient); + }); + + beforeEach(() => { + mockClient.getUser.mockClear().mockResolvedValue(undefined); + }); + + it('closes on close button click', () => { + const onClose = jest.fn(); + const component = getComponent({ onClose }); + act(() => { + findByTestId(component, 'base-card-close-button').at(0).simulate('click'); + }); + + expect(onClose).toHaveBeenCalled(); + }); + + describe('without a room', () => { + it('does not render space header', () => { + const component = getComponent(); + expect(findByTestId(component, 'space-header').length).toBeFalsy(); + }); + + it('renders user info correctly', () => { + const component = getComponent(); + expect(component.find(BasicUserInfo).length).toBeTruthy(); + }); + + it('renders encryption info panel without pending verification', () => { + const phase = RightPanelPhases.EncryptionPanel; + const component = getComponent({ phase }); + + expect(component.find(EncryptionInfo).length).toBeTruthy(); + }); + + it('renders encryption verification panel with pending verification', () => { + const phase = RightPanelPhases.EncryptionPanel; + const component = getComponent({ phase, verificationRequest }); + + expect(component.find(EncryptionInfo).length).toBeFalsy(); + expect(component.find(VerificationPanel).length).toBeTruthy(); + }); + + it('renders close button correctly when encryption panel with a pending verification request', () => { + const phase = RightPanelPhases.EncryptionPanel; + const component = getComponent({ phase, verificationRequest }); + + expect(findByTestId(component, 'base-card-close-button').at(0).props().title).toEqual('Cancel'); + }); + }); + + describe('with a room', () => { + const room = { + roomId: '!fkfk', + isSpaceRoom: jest.fn().mockReturnValue(false), + getMember: jest.fn().mockReturnValue(undefined), + getMxcAvatarUrl: jest.fn().mockReturnValue('mock-avatar-url'), + name: 'test room', + on: jest.fn(), + currentState: { + getStateEvents: jest.fn(), + on: jest.fn(), + }, + } as unknown as Room; + + it('does not render space header when room is not a space room', () => { + const component = getComponent({ room }); + expect(findByTestId(component, 'space-header').length).toBeFalsy(); + }); + + it('renders space header when room is a space room', () => { + const spaceRoom = { + ...room, isSpaceRoom: jest.fn().mockReturnValue(true), + }; + const component = getComponent({ room: spaceRoom }); + expect(findByTestId(component, 'space-header').length).toBeTruthy(); + }); + + it('renders encryption info panel without pending verification', () => { + const phase = RightPanelPhases.EncryptionPanel; + const component = getComponent({ phase, room }); + + expect(component.find(EncryptionInfo).length).toBeTruthy(); + }); + + it('renders encryption verification panel with pending verification', () => { + const phase = RightPanelPhases.EncryptionPanel; + const component = getComponent({ phase, verificationRequest, room }); + + expect(component.find(EncryptionInfo).length).toBeFalsy(); + expect(component.find(VerificationPanel).length).toBeTruthy(); + }); + }); +}); From b6c39826316386447c6004bfcb5cfd20044fa80b Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Tue, 8 Feb 2022 11:59:37 +0100 Subject: [PATCH 2/3] one more test case Signed-off-by: Kerry Archibald --- test/components/views/right_panel/UserInfo-test.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/components/views/right_panel/UserInfo-test.tsx b/test/components/views/right_panel/UserInfo-test.tsx index e4d556afb71..e40ed13dc7d 100644 --- a/test/components/views/right_panel/UserInfo-test.tsx +++ b/test/components/views/right_panel/UserInfo-test.tsx @@ -104,7 +104,7 @@ describe('', () => { expect(findByTestId(component, 'space-header').length).toBeFalsy(); }); - it('renders user info correctly', () => { + it('renders user info', () => { const component = getComponent(); expect(component.find(BasicUserInfo).length).toBeTruthy(); }); @@ -146,6 +146,11 @@ describe('', () => { }, } as unknown as Room; + it('renders user info', () => { + const component = getComponent(); + expect(component.find(BasicUserInfo).length).toBeTruthy(); + }); + it('does not render space header when room is not a space room', () => { const component = getComponent({ room }); expect(findByTestId(component, 'space-header').length).toBeFalsy(); From 8078e26a36afc4d8c370b4452d10cb5a913e1a22 Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Tue, 8 Feb 2022 13:07:19 +0100 Subject: [PATCH 3/3] remove BasicUserInfo export Signed-off-by: Kerry Archibald --- src/components/views/right_panel/UserInfo.tsx | 2 +- test/components/views/right_panel/UserInfo-test.tsx | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/views/right_panel/UserInfo.tsx b/src/components/views/right_panel/UserInfo.tsx index 99cecb5c140..476ffc763b0 100644 --- a/src/components/views/right_panel/UserInfo.tsx +++ b/src/components/views/right_panel/UserInfo.tsx @@ -1283,7 +1283,7 @@ export const useDevices = (userId: string) => { return devices; }; -export const BasicUserInfo: React.FC<{ +const BasicUserInfo: React.FC<{ room: Room; member: User | RoomMember; groupId: string; diff --git a/test/components/views/right_panel/UserInfo-test.tsx b/test/components/views/right_panel/UserInfo-test.tsx index e40ed13dc7d..0e086b57999 100644 --- a/test/components/views/right_panel/UserInfo-test.tsx +++ b/test/components/views/right_panel/UserInfo-test.tsx @@ -21,7 +21,7 @@ import { Room, User } from 'matrix-js-sdk'; import { Phase, VerificationRequest } from 'matrix-js-sdk/src/crypto/verification/request/VerificationRequest'; import "../../../skinned-sdk"; -import UserInfo, { BasicUserInfo } from '../../../../src/components/views/right_panel/UserInfo'; +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'; @@ -106,7 +106,7 @@ describe('', () => { it('renders user info', () => { const component = getComponent(); - expect(component.find(BasicUserInfo).length).toBeTruthy(); + expect(component.find("BasicUserInfo").length).toBeTruthy(); }); it('renders encryption info panel without pending verification', () => { @@ -148,7 +148,7 @@ describe('', () => { it('renders user info', () => { const component = getComponent(); - expect(component.find(BasicUserInfo).length).toBeTruthy(); + expect(component.find("BasicUserInfo").length).toBeTruthy(); }); it('does not render space header when room is not a space room', () => {