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';
24 deleteProjectAlmBinding,
27 setProjectAzureBinding,
28 setProjectBitbucketBinding,
29 setProjectGithubBinding
30 } from '../../../../../api/alm-settings';
31 import { mockComponent } from '../../../../../helpers/testMocks';
32 import { AlmKeys } from '../../../../../types/alm-settings';
33 import PRDecorationBinding from '../PRDecorationBinding';
35 jest.mock('../../../../../api/alm-settings', () => ({
36 getAlmSettings: jest.fn().mockResolvedValue([]),
37 getProjectAlmBinding: jest.fn().mockResolvedValue(undefined),
38 setProjectAzureBinding: jest.fn().mockResolvedValue(undefined),
39 setProjectBitbucketBinding: jest.fn().mockResolvedValue(undefined),
40 setProjectGithubBinding: jest.fn().mockResolvedValue(undefined),
41 deleteProjectAlmBinding: jest.fn().mockResolvedValue(undefined)
44 const PROJECT_KEY = 'project-key';
50 it('should render correctly', () => {
51 expect(shallowRender()).toMatchSnapshot();
54 it('should fill selects and fill formdata', async () => {
55 const url = 'github.com';
56 const instances = [{ key: 'instance1', url, alm: AlmKeys.GitHub }];
59 repository: 'account/repo'
61 (getAlmSettings as jest.Mock).mockResolvedValueOnce(instances);
62 (getProjectAlmBinding as jest.Mock).mockResolvedValueOnce(formdata);
64 const wrapper = shallowRender();
65 await waitAndUpdate(wrapper);
67 expect(wrapper.state().loading).toBe(false);
68 expect(wrapper.state().formData).toEqual(formdata);
69 expect(wrapper.state().originalData).toEqual(formdata);
72 it('should handle reset', async () => {
73 const wrapper = shallowRender();
74 await waitAndUpdate(wrapper);
78 repository: 'something/else'
82 wrapper.instance().handleReset();
83 await waitAndUpdate(wrapper);
85 expect(deleteProjectAlmBinding).toBeCalledWith(PROJECT_KEY);
86 expect(wrapper.state().formData).toEqual({ key: '', repository: '', slug: '' });
87 expect(wrapper.state().originalData).toBeUndefined();
90 describe('handleSubmit', () => {
92 { key: 'github', alm: AlmKeys.GitHub },
93 { key: 'azure', alm: AlmKeys.Azure },
94 { key: 'bitbucket', alm: AlmKeys.Bitbucket }
97 it('should work for github', async () => {
98 const wrapper = shallowRender();
99 await waitAndUpdate(wrapper);
100 const githubKey = 'github';
101 const repository = 'repo/path';
102 wrapper.setState({ formData: { key: githubKey, repository }, instances });
103 wrapper.instance().handleSubmit();
104 await waitAndUpdate(wrapper);
106 expect(setProjectGithubBinding).toBeCalledWith({
107 almSetting: githubKey,
108 project: PROJECT_KEY,
111 expect(wrapper.state().success).toBe(true);
114 it('should work for azure', async () => {
115 const wrapper = shallowRender();
116 await waitAndUpdate(wrapper);
117 const azureKey = 'azure';
118 wrapper.setState({ formData: { key: azureKey }, instances });
119 wrapper.instance().handleSubmit();
120 await waitAndUpdate(wrapper);
122 expect(setProjectAzureBinding).toBeCalledWith({
123 almSetting: azureKey,
126 expect(wrapper.state().success).toBe(true);
129 it('should work for bitbucket', async () => {
130 const wrapper = shallowRender();
131 await waitAndUpdate(wrapper);
132 const bitbucketKey = 'bitbucket';
133 const repository = 'repoKey';
134 const slug = 'repoSlug';
135 wrapper.setState({ formData: { key: bitbucketKey, repository, slug }, instances });
136 wrapper.instance().handleSubmit();
137 await waitAndUpdate(wrapper);
139 expect(setProjectBitbucketBinding).toBeCalledWith({
140 almSetting: bitbucketKey,
141 project: PROJECT_KEY,
145 expect(wrapper.state().success).toBe(true);
149 it('should handle failures gracefully', async () => {
150 (getProjectAlmBinding as jest.Mock).mockRejectedValueOnce({ status: 500 });
151 (setProjectGithubBinding as jest.Mock).mockRejectedValueOnce({ status: 500 });
152 (deleteProjectAlmBinding as jest.Mock).mockRejectedValueOnce({ status: 500 });
154 const wrapper = shallowRender();
155 await waitAndUpdate(wrapper);
159 repository: 'something/else'
163 wrapper.instance().handleSubmit();
164 await waitAndUpdate(wrapper);
165 wrapper.instance().handleReset();
168 it('should handle field changes', async () => {
169 const url = 'git.enterprise.com';
170 const repository = 'my/repo';
172 { key: 'instance1', url, alm: 'github' },
173 { key: 'instance2', url, alm: 'github' },
174 { key: 'instance3', url: 'otherurl', alm: 'github' }
176 (getAlmSettings as jest.Mock).mockResolvedValueOnce(instances);
177 const wrapper = shallowRender();
178 await waitAndUpdate(wrapper);
180 wrapper.instance().handleFieldChange('key', 'instance2');
181 await waitAndUpdate(wrapper);
182 expect(wrapper.state().formData).toEqual({
186 wrapper.instance().handleFieldChange('repository', repository);
187 await waitAndUpdate(wrapper);
188 expect(wrapper.state().formData).toEqual({
194 it('should validate form', async () => {
195 const wrapper = shallowRender();
196 await waitAndUpdate(wrapper);
198 expect(wrapper.instance().validateForm({ key: '', repository: '' })).toBe(false);
199 expect(wrapper.instance().validateForm({ key: '', repository: 'c' })).toBe(false);
203 { key: 'azure', alm: AlmKeys.Azure },
204 { key: 'bitbucket', alm: AlmKeys.Bitbucket },
205 { key: 'github', alm: AlmKeys.GitHub },
206 { key: 'gitlab', alm: AlmKeys.GitLab }
210 expect(wrapper.instance().validateForm({ key: 'azure' })).toBe(true);
212 expect(wrapper.instance().validateForm({ key: 'github', repository: '' })).toBe(false);
213 expect(wrapper.instance().validateForm({ key: 'github', repository: 'asdf' })).toBe(true);
215 expect(wrapper.instance().validateForm({ key: 'bitbucket', repository: 'key' })).toBe(false);
217 wrapper.instance().validateForm({ key: 'bitbucket', repository: 'key', slug: 'slug' })
220 expect(wrapper.instance().validateForm({ key: 'gitlab' })).toBe(true);
221 expect(wrapper.instance().validateForm({ key: 'gitlab', repository: 'key' })).toBe(true);
224 function shallowRender(props: Partial<PRDecorationBinding['props']> = {}) {
225 return shallow<PRDecorationBinding>(
226 <PRDecorationBinding component={mockComponent({ key: PROJECT_KEY })} {...props} />