3 * Copyright (C) 2009-2020 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.
21 import { shallow } from 'enzyme';
22 import * as React from 'react';
23 import { SubmitButton } from 'sonar-ui-common/components/controls/buttons';
24 import { change, submit } from 'sonar-ui-common/helpers/testUtils';
25 import { mockAlmSettingsInstance } from '../../../../helpers/mocks/alm-settings';
26 import { AlmKeys } from '../../../../types/alm-settings';
27 import BitbucketPersonalAccessTokenForm, {
28 BitbucketPersonalAccessTokenFormProps
29 } from '../BitbucketPersonalAccessTokenForm';
31 it('should render correctly', () => {
32 expect(shallowRender()).toMatchSnapshot('default');
33 expect(shallowRender({ submitting: true })).toMatchSnapshot('submitting');
34 expect(shallowRender({ validationFailed: true })).toMatchSnapshot('validation failed');
37 it('should correctly handle form interactions', () => {
38 const onPersonalAccessTokenCreate = jest.fn();
39 const wrapper = shallowRender({ onPersonalAccessTokenCreate });
41 // Submit button disabled by default.
42 expect(wrapper.find(SubmitButton).prop('disabled')).toBe(true);
44 // Submit button enabled if there's a value.
45 change(wrapper.find('input'), 'token');
46 expect(wrapper.find(SubmitButton).prop('disabled')).toBe(false);
48 // Expect correct calls to be made when submitting.
49 submit(wrapper.find('form'));
50 expect(onPersonalAccessTokenCreate).toBeCalled();
52 // If validation fails, we toggle the submitting flag and call useEffect()
53 // to set the `touched` flag to false again. Trigger a re-render, and mock
54 // useEffect(). This should de-activate the submit button again.
55 jest.spyOn(React, 'useEffect').mockImplementationOnce(f => f());
56 wrapper.setProps({ submitting: false });
57 expect(wrapper.find(SubmitButton).prop('disabled')).toBe(true);
60 function shallowRender(props: Partial<BitbucketPersonalAccessTokenFormProps> = {}) {
61 return shallow<BitbucketPersonalAccessTokenFormProps>(
62 <BitbucketPersonalAccessTokenForm
63 bitbucketSetting={mockAlmSettingsInstance({
64 alm: AlmKeys.Bitbucket,
65 url: 'http://www.example.com'
67 onPersonalAccessTokenCreate={jest.fn()}
68 validationFailed={false}