3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import { shallow } from 'enzyme';
21 import * as React from 'react';
22 import { mockLocation } from '../../../helpers/testMocks';
23 import { change, submit } from '../../../helpers/testUtils';
24 import ChangeAdminPasswordAppRenderer, {
25 ChangeAdminPasswordAppRendererProps
26 } from '../ChangeAdminPasswordAppRenderer';
27 import { DEFAULT_ADMIN_PASSWORD } from '../constants';
29 it('should render correctly', () => {
30 expect(shallowRender()).toMatchSnapshot('default');
31 expect(shallowRender({ canAdmin: false })).toMatchSnapshot('access denied');
32 expect(shallowRender({ canSubmit: false })).toMatchSnapshot('cannot submit');
33 expect(shallowRender({ submitting: true })).toMatchSnapshot('submitting');
36 passwordValue: DEFAULT_ADMIN_PASSWORD,
37 confirmPasswordValue: DEFAULT_ADMIN_PASSWORD
39 ).toMatchSnapshot('trying to use default admin password');
40 expect(shallowRender({ success: true })).toMatchSnapshot('success');
43 it('should correctly react to input changes', () => {
44 const onConfirmPasswordChange = jest.fn();
45 const onPasswordChange = jest.fn();
46 const wrapper = shallowRender({ onConfirmPasswordChange, onPasswordChange });
48 change(wrapper.find('#user-password'), 'new pass');
49 change(wrapper.find('#confirm-user-password'), 'confirm pass');
50 expect(onPasswordChange).toBeCalledWith('new pass');
51 expect(onConfirmPasswordChange).toBeCalledWith('confirm pass');
54 it('should correctly submit the form', () => {
55 const onSubmit = jest.fn();
56 const wrapper = shallowRender({ onSubmit });
58 submit(wrapper.find('form'));
59 expect(onSubmit).toBeCalled();
62 function shallowRender(props: Partial<ChangeAdminPasswordAppRendererProps> = {}) {
63 return shallow<ChangeAdminPasswordAppRendererProps>(
64 <ChangeAdminPasswordAppRenderer
67 passwordValue="password"
68 confirmPasswordValue="confirm"
69 onConfirmPasswordChange={jest.fn()}
70 onPasswordChange={jest.fn()}
74 location={mockLocation()}