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.
20 import { shallow } from 'enzyme';
21 import * as React from 'react';
22 import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
23 import { mockGithubBindingDefinition } from '../../../../../helpers/mocks/alm-settings';
24 import { GithubBindingDefinition } from '../../../../../types/alm-settings';
25 import AlmBindingDefinitionForm from '../AlmBindingDefinitionForm';
27 it('should render correctly', () => {
28 expect(shallowRender()).toMatchSnapshot();
31 it('should reset if the props change', () => {
32 const bindingDefinition = mockGithubBindingDefinition();
33 const wrapper = shallowRender({ bindingDefinition });
35 wrapper.setState({ formData: { ...bindingDefinition, appId: 'newAppId' }, touched: true });
36 wrapper.setProps({ bindingDefinition: { ...bindingDefinition } });
37 expect(wrapper.state('touched')).toBe(true);
39 wrapper.setProps({ bindingDefinition: mockGithubBindingDefinition({ key: 'diffKey' }) });
40 expect(wrapper.state('touched')).toBe(false);
43 it('should handle field changes', () => {
44 const wrapper = shallowRender();
47 key: 'github - example',
48 url: 'http://github.com',
50 privateKey: 'gs7df9g7d9fsg7x9df7g9xdg'
53 wrapper.instance().handleFieldChange('key', formData.key);
54 wrapper.instance().handleFieldChange('url', formData.url);
55 wrapper.instance().handleFieldChange('appId', formData.appId);
56 wrapper.instance().handleFieldChange('privateKey', formData.privateKey);
57 expect(wrapper.state().formData).toEqual(formData);
60 it('should handle form submit', async () => {
61 const onSubmit = jest.fn();
62 const wrapper = shallowRender({
64 bindingDefinition: { key: 'originalKey', appId: '', privateKey: '', url: '' }
67 key: 'github instance',
68 url: 'http://github.enterprise.com',
70 privateKey: 'gs7df9g7d9fsg7x9df7g9xdg'
72 wrapper.setState({ formData });
73 await waitAndUpdate(wrapper);
75 wrapper.instance().handleFormSubmit();
77 expect(onSubmit).toHaveBeenCalledWith(formData, 'originalKey');
80 it('should handle cancelling', () => {
81 const onCancel = jest.fn();
82 const bindingDefinition = {
86 url: 'http://github.enterprise.com'
88 const wrapper = shallowRender({
93 wrapper.setState({ formData: mockGithubBindingDefinition() });
94 wrapper.instance().handleCancel();
96 expect(wrapper.state().formData).toBe(bindingDefinition);
97 expect(onCancel).toHaveBeenCalled();
100 it('should handle deleting', () => {
101 const onDelete = jest.fn();
102 const bindingDefinition = mockGithubBindingDefinition();
103 const wrapper = shallowRender({
108 wrapper.instance().handleDelete();
109 expect(onDelete).toHaveBeenCalledWith(bindingDefinition.key);
112 it('should (dis)allow submit by validating its state', () => {
113 const wrapper = shallowRender();
114 expect(wrapper.instance().canSubmit()).toBe(false);
116 wrapper.setState({ formData: mockGithubBindingDefinition(), touched: true });
117 expect(wrapper.instance().canSubmit()).toBe(true);
119 wrapper.setState({ formData: mockGithubBindingDefinition({ key: '' }), touched: true });
120 wrapper.setProps({ hideKeyField: true });
121 expect(wrapper.instance().canSubmit()).toBe(true);
123 wrapper.setState({ formData: mockGithubBindingDefinition({ url: '' }), touched: true });
124 wrapper.setProps({ optionalFields: ['url'] });
125 expect(wrapper.instance().canSubmit()).toBe(true);
128 function shallowRender(
129 props: Partial<AlmBindingDefinitionForm<GithubBindingDefinition>['props']> = {}
131 return shallow<AlmBindingDefinitionForm<GithubBindingDefinition>>(
132 <AlmBindingDefinitionForm
133 bindingDefinition={{ appId: '', key: '', privateKey: '', url: '' }}
138 </AlmBindingDefinitionForm>