|
| 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