3 * Copyright (C) 2009-2023 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 { ResetButtonLink } from '../../../../../components/controls/buttons';
23 import { mockGithubBindingDefinition } from '../../../../../helpers/mocks/alm-settings';
24 import { click, mockEvent } from '../../../../../helpers/testUtils';
25 import { AlmKeys } from '../../../../../types/alm-settings';
26 import AlmBindingDefinitionFormRenderer, {
27 AlmBindingDefinitionFormProps,
28 } from '../AlmBindingDefinitionFormRenderer';
29 import GithubForm from '../GithubForm';
31 it('should render correctly', () => {
32 expect(shallowRender()).toMatchSnapshot();
33 expect(shallowRender({ alreadyHaveInstanceConfigured: true })).toMatchSnapshot('second instance');
34 expect(shallowRender({ submitting: true })).toMatchSnapshot('submitting');
35 expect(shallowRender({ isUpdate: true })).toMatchSnapshot('editing');
36 expect(shallowRender({ validationError: 'this is a validation error' })).toMatchSnapshot(
37 'with validation error'
41 it.each([[AlmKeys.Azure], [AlmKeys.GitHub], [AlmKeys.GitLab], [AlmKeys.BitbucketServer]])(
42 'should render correctly for %s',
44 expect(shallowRender({ alm })).toMatchSnapshot();
48 it('should cancel properly', () => {
49 const onCancel = jest.fn();
50 const wrapper = shallowRender({ onCancel });
52 click(wrapper.find(ResetButtonLink));
53 expect(onCancel).toHaveBeenCalled();
56 it('should submit properly', () => {
57 const onSubmit = jest.fn();
58 const wrapper = shallowRender({ onSubmit });
60 const event: React.SyntheticEvent<HTMLFormElement> = mockEvent({ preventDefault: jest.fn() });
62 wrapper.find('form').simulate('submit', event);
64 expect(event.preventDefault).toHaveBeenCalled();
65 expect(onSubmit).toHaveBeenCalled();
68 it('should handle field change', () => {
69 const onFieldChange = jest.fn();
70 const wrapper = shallowRender({ onFieldChange });
72 wrapper.find(GithubForm).props().onFieldChange('key', 'test');
74 expect(onFieldChange).toHaveBeenCalledWith('key', 'test');
77 function shallowRender(props: Partial<AlmBindingDefinitionFormProps> = {}) {
79 <AlmBindingDefinitionFormRenderer
83 alreadyHaveInstanceConfigured={false}
85 formData={mockGithubBindingDefinition()}
88 onFieldChange={jest.fn()}
89 bitbucketVariant={AlmKeys.BitbucketServer}
90 onBitbucketVariantChange={jest.fn()}