Skip to content

Commit 30d1ac1

Browse files
author
Kerry
authored
unit test basic paths in UserInfo (matrix-org#7740)
* unit test main paths in UserInfo component Signed-off-by: Kerry Archibald <[email protected]> * one more test case Signed-off-by: Kerry Archibald <[email protected]> * remove BasicUserInfo export Signed-off-by: Kerry Archibald <[email protected]>
1 parent af0b21d commit 30d1ac1

File tree

5 files changed

+190
-4
lines changed

5 files changed

+190
-4
lines changed

src/components/views/right_panel/BaseCard.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const BaseCard: React.FC<IProps> = ({
6969
let closeButton;
7070
if (onClose) {
7171
closeButton = <AccessibleButton
72+
data-test-id='base-card-close-button'
7273
className="mx_BaseCard_close"
7374
onClick={onClose}
7475
title={closeLabel || _t("Close")}

src/components/views/right_panel/EncryptionInfo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const EncryptionInfo: React.FC<IProps> = ({
101101
}
102102

103103
return <React.Fragment>
104-
<div className="mx_UserInfo_container">
104+
<div data-test-id='encryption-info-description' className="mx_UserInfo_container">
105105
<h3>{ _t("Encryption") }</h3>
106106
{ description }
107107
</div>

src/components/views/right_panel/EncryptionPanel.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const EncryptionPanel: React.FC<IProps> = (props: IProps) => {
106106
setPhase(request.phase);
107107
}
108108
}, [onClose, request]);
109+
109110
useEventEmitter(request, "change", changeHandler);
110111

111112
const onStartVerification = useCallback(async () => {
@@ -131,6 +132,7 @@ const EncryptionPanel: React.FC<IProps> = (props: IProps) => {
131132
const isSelfVerification = request ?
132133
request.isSelfVerification :
133134
member.userId === MatrixClientPeg.get().getUserId();
135+
134136
if (!request || requested) {
135137
const initiatedByMe = (!request && isRequesting) || (request && request.initiatedByMe);
136138
return (

src/components/views/right_panel/UserInfo.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ function useRoomPermissions(cli: MatrixClient, room: Room, user: RoomMember): IR
10961096
modifyLevelMax,
10971097
});
10981098
}, [cli, user, room]);
1099+
10991100
useEventEmitter(cli, "RoomState.members", updateRoomPermissions);
11001101
useEffect(() => {
11011102
updateRoomPermissions();
@@ -1702,16 +1703,16 @@ const UserInfo: React.FC<IProps> = ({
17021703

17031704
let scopeHeader;
17041705
if (SpaceStore.spacesEnabled && room?.isSpaceRoom()) {
1705-
scopeHeader = <div className="mx_RightPanel_scopeHeader">
1706+
scopeHeader = <div data-test-id='space-header' className="mx_RightPanel_scopeHeader">
17061707
<RoomAvatar room={room} height={32} width={32} />
17071708
<RoomName room={room} />
17081709
</div>;
17091710
}
17101711

1711-
const header = <React.Fragment>
1712+
const header = <>
17121713
{ scopeHeader }
17131714
<UserInfoHeader member={member} e2eStatus={e2eStatus} roomId={room?.roomId} />
1714-
</React.Fragment>;
1715+
</>;
17151716
return <BaseCard
17161717
className={classes.join(" ")}
17171718
header={header}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from 'react';
18+
import { mount } from 'enzyme';
19+
import { act } from "react-dom/test-utils";
20+
import { Room, User } from 'matrix-js-sdk';
21+
import { Phase, VerificationRequest } from 'matrix-js-sdk/src/crypto/verification/request/VerificationRequest';
22+
23+
import "../../../skinned-sdk";
24+
import UserInfo from '../../../../src/components/views/right_panel/UserInfo';
25+
import { RightPanelPhases } from '../../../../src/stores/right-panel/RightPanelStorePhases';
26+
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
27+
import MatrixClientContext from '../../../../src/contexts/MatrixClientContext';
28+
import VerificationPanel from '../../../../src/components/views/right_panel/VerificationPanel';
29+
import EncryptionInfo from '../../../../src/components/views/right_panel/EncryptionInfo';
30+
31+
const findByTestId = (component, id) => component.find(`[data-test-id="${id}"]`);
32+
33+
jest.mock('../../../../src/utils/DMRoomMap', () => {
34+
const mock = {
35+
getUserIdForRoomId: jest.fn(),
36+
getDMRoomsForUserId: jest.fn(),
37+
};
38+
39+
return {
40+
shared: jest.fn().mockReturnValue(mock),
41+
sharedInstance: mock,
42+
};
43+
});
44+
45+
describe('<UserInfo />', () => {
46+
const defaultUserId = '@test:test';
47+
const defaultUser = new User(defaultUserId);
48+
49+
const defaultProps = {
50+
user: defaultUser,
51+
phase: RightPanelPhases.RoomMemberInfo,
52+
onClose: jest.fn(),
53+
};
54+
55+
const mockClient = {
56+
getUser: jest.fn(),
57+
isGuest: jest.fn().mockReturnValue(false),
58+
isUserIgnored: jest.fn(),
59+
isCryptoEnabled: jest.fn(),
60+
getUserId: jest.fn(),
61+
on: jest.fn(),
62+
isSynapseAdministrator: jest.fn().mockResolvedValue(false),
63+
isRoomEncrypted: jest.fn().mockReturnValue(false),
64+
doesServerSupportUnstableFeature: jest.fn().mockReturnValue(false),
65+
mxcUrlToHttp: jest.fn().mockReturnValue('mock-mxcUrlToHttp'),
66+
removeListerner: jest.fn(),
67+
currentState: {
68+
on: jest.fn(),
69+
},
70+
};
71+
72+
const verificationRequest = {
73+
pending: true, on: jest.fn(), phase: Phase.Ready,
74+
channel: { transactionId: 1 },
75+
otherPartySupportsMethod: jest.fn(),
76+
} as unknown as VerificationRequest;
77+
78+
const getComponent = (props = {}) => mount(<UserInfo {...defaultProps} {...props} />, {
79+
wrappingComponent: MatrixClientContext.Provider,
80+
wrappingComponentProps: { value: mockClient },
81+
});
82+
83+
beforeAll(() => {
84+
jest.spyOn(MatrixClientPeg, 'get').mockReturnValue(mockClient);
85+
});
86+
87+
beforeEach(() => {
88+
mockClient.getUser.mockClear().mockResolvedValue(undefined);
89+
});
90+
91+
it('closes on close button click', () => {
92+
const onClose = jest.fn();
93+
const component = getComponent({ onClose });
94+
act(() => {
95+
findByTestId(component, 'base-card-close-button').at(0).simulate('click');
96+
});
97+
98+
expect(onClose).toHaveBeenCalled();
99+
});
100+
101+
describe('without a room', () => {
102+
it('does not render space header', () => {
103+
const component = getComponent();
104+
expect(findByTestId(component, 'space-header').length).toBeFalsy();
105+
});
106+
107+
it('renders user info', () => {
108+
const component = getComponent();
109+
expect(component.find("BasicUserInfo").length).toBeTruthy();
110+
});
111+
112+
it('renders encryption info panel without pending verification', () => {
113+
const phase = RightPanelPhases.EncryptionPanel;
114+
const component = getComponent({ phase });
115+
116+
expect(component.find(EncryptionInfo).length).toBeTruthy();
117+
});
118+
119+
it('renders encryption verification panel with pending verification', () => {
120+
const phase = RightPanelPhases.EncryptionPanel;
121+
const component = getComponent({ phase, verificationRequest });
122+
123+
expect(component.find(EncryptionInfo).length).toBeFalsy();
124+
expect(component.find(VerificationPanel).length).toBeTruthy();
125+
});
126+
127+
it('renders close button correctly when encryption panel with a pending verification request', () => {
128+
const phase = RightPanelPhases.EncryptionPanel;
129+
const component = getComponent({ phase, verificationRequest });
130+
131+
expect(findByTestId(component, 'base-card-close-button').at(0).props().title).toEqual('Cancel');
132+
});
133+
});
134+
135+
describe('with a room', () => {
136+
const room = {
137+
roomId: '!fkfk',
138+
isSpaceRoom: jest.fn().mockReturnValue(false),
139+
getMember: jest.fn().mockReturnValue(undefined),
140+
getMxcAvatarUrl: jest.fn().mockReturnValue('mock-avatar-url'),
141+
name: 'test room',
142+
on: jest.fn(),
143+
currentState: {
144+
getStateEvents: jest.fn(),
145+
on: jest.fn(),
146+
},
147+
} as unknown as Room;
148+
149+
it('renders user info', () => {
150+
const component = getComponent();
151+
expect(component.find("BasicUserInfo").length).toBeTruthy();
152+
});
153+
154+
it('does not render space header when room is not a space room', () => {
155+
const component = getComponent({ room });
156+
expect(findByTestId(component, 'space-header').length).toBeFalsy();
157+
});
158+
159+
it('renders space header when room is a space room', () => {
160+
const spaceRoom = {
161+
...room, isSpaceRoom: jest.fn().mockReturnValue(true),
162+
};
163+
const component = getComponent({ room: spaceRoom });
164+
expect(findByTestId(component, 'space-header').length).toBeTruthy();
165+
});
166+
167+
it('renders encryption info panel without pending verification', () => {
168+
const phase = RightPanelPhases.EncryptionPanel;
169+
const component = getComponent({ phase, room });
170+
171+
expect(component.find(EncryptionInfo).length).toBeTruthy();
172+
});
173+
174+
it('renders encryption verification panel with pending verification', () => {
175+
const phase = RightPanelPhases.EncryptionPanel;
176+
const component = getComponent({ phase, verificationRequest, room });
177+
178+
expect(component.find(EncryptionInfo).length).toBeFalsy();
179+
expect(component.find(VerificationPanel).length).toBeTruthy();
180+
});
181+
});
182+
});

0 commit comments

Comments
 (0)