]> source.dussan.org Git - sonarqube.git/blob
4b3fad35c2494c46abca3a6e637fae119ee2ecb5
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 import { shallow } from 'enzyme';
21 import * as React from 'react';
22 import {
23   countBindedProjects,
24   deleteConfiguration,
25   getAlmDefinitions,
26   validateAlmSettings
27 } from '../../../../../api/alm-settings';
28 import { mockAppState, mockLocation, mockRouter } from '../../../../../helpers/testMocks';
29 import { waitAndUpdate } from '../../../../../helpers/testUtils';
30 import { AlmKeys, AlmSettingsBindingStatusType } from '../../../../../types/alm-settings';
31 import { AlmIntegration } from '../AlmIntegration';
32 import AlmIntegrationRenderer from '../AlmIntegrationRenderer';
33
34 jest.mock('../../../../../api/alm-settings', () => ({
35   countBindedProjects: jest.fn().mockResolvedValue(0),
36   deleteConfiguration: jest.fn().mockResolvedValue(undefined),
37   getAlmDefinitions: jest
38     .fn()
39     .mockResolvedValue({ azure: [], bitbucket: [], bitbucketcloud: [], github: [], gitlab: [] }),
40   validateAlmSettings: jest.fn().mockResolvedValue('')
41 }));
42
43 beforeEach(() => {
44   jest.clearAllMocks();
45 });
46
47 it('should render correctly', () => {
48   expect(shallowRender()).toMatchSnapshot();
49 });
50
51 it('should validate existing configurations', async () => {
52   (getAlmDefinitions as jest.Mock).mockResolvedValueOnce({
53     [AlmKeys.Azure]: [{ key: 'a1' }],
54     [AlmKeys.BitbucketServer]: [{ key: 'b1' }],
55     [AlmKeys.BitbucketCloud]: [{ key: 'bc1' }],
56     [AlmKeys.GitHub]: [{ key: 'gh1' }, { key: 'gh2' }],
57     [AlmKeys.GitLab]: [{ key: 'gl1' }]
58   });
59
60   const wrapper = shallowRender();
61
62   await waitAndUpdate(wrapper);
63
64   expect(validateAlmSettings).toBeCalledTimes(6);
65   expect(validateAlmSettings).toBeCalledWith('a1');
66   expect(validateAlmSettings).toBeCalledWith('b1');
67   expect(validateAlmSettings).toBeCalledWith('bc1');
68   expect(validateAlmSettings).toBeCalledWith('gh1');
69   expect(validateAlmSettings).toBeCalledWith('gh2');
70   expect(validateAlmSettings).toBeCalledWith('gl1');
71 });
72
73 it('should handle alm selection', async () => {
74   const router = mockRouter();
75   const wrapper = shallowRender({ router });
76
77   wrapper.setState({ currentAlmTab: AlmKeys.Azure });
78
79   wrapper.instance().handleSelectAlm(AlmKeys.GitHub);
80
81   await waitAndUpdate(wrapper);
82
83   expect(wrapper.state().currentAlmTab).toBe(AlmKeys.GitHub);
84   expect(router.push).toBeCalled();
85 });
86
87 it('should handle delete', async () => {
88   const toBeDeleted = '45672';
89   (countBindedProjects as jest.Mock).mockResolvedValueOnce(7);
90   const wrapper = shallowRender();
91
92   wrapper
93     .find(AlmIntegrationRenderer)
94     .props()
95     .onDelete(toBeDeleted);
96   await waitAndUpdate(wrapper);
97   expect(wrapper.state().projectCount).toBe(7);
98   expect(wrapper.state().definitionKeyForDeletion).toBe(toBeDeleted);
99
100   wrapper
101     .find(AlmIntegrationRenderer)
102     .props()
103     .onCancelDelete();
104   await waitAndUpdate(wrapper);
105   expect(wrapper.state().projectCount).toBeUndefined();
106   expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
107 });
108
109 it('should delete configuration', async () => {
110   (deleteConfiguration as jest.Mock).mockResolvedValueOnce(undefined);
111   const wrapper = shallowRender();
112   wrapper.instance().handleConfirmDelete('8345678');
113
114   await waitAndUpdate(wrapper);
115   expect(wrapper.state().projectCount).toBeUndefined();
116   expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
117 });
118
119 it('should validate a configuration', async () => {
120   const definitionKey = 'validated-key';
121   const failureMessage = 'an error occured';
122
123   const wrapper = shallowRender();
124   await waitAndUpdate(wrapper);
125
126   (validateAlmSettings as jest.Mock)
127     .mockRejectedValueOnce(undefined)
128     .mockResolvedValueOnce(failureMessage)
129     .mockResolvedValueOnce('')
130     .mockResolvedValueOnce('');
131
132   await wrapper.instance().handleCheck(definitionKey);
133
134   expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
135     alertSuccess: true,
136     failureMessage: '',
137     type: AlmSettingsBindingStatusType.Warning
138   });
139
140   await wrapper.instance().handleCheck(definitionKey);
141
142   expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
143     alertSuccess: true,
144     failureMessage,
145     type: AlmSettingsBindingStatusType.Failure
146   });
147
148   await wrapper.instance().handleCheck(definitionKey);
149
150   expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
151     alertSuccess: true,
152     failureMessage: '',
153     type: AlmSettingsBindingStatusType.Success
154   });
155 });
156
157 it('should fetch settings', async () => {
158   const definitions = {
159     [AlmKeys.Azure]: [{ key: 'a1' }],
160     [AlmKeys.BitbucketServer]: [{ key: 'b1' }],
161     [AlmKeys.BitbucketCloud]: [{ key: 'bc1' }],
162     [AlmKeys.GitHub]: [{ key: 'gh1' }],
163     [AlmKeys.GitLab]: [{ key: 'gl1' }]
164   };
165
166   const wrapper = shallowRender();
167   await waitAndUpdate(wrapper);
168
169   (getAlmDefinitions as jest.Mock).mockResolvedValueOnce(definitions);
170
171   await wrapper.instance().fetchPullRequestDecorationSetting();
172
173   expect(getAlmDefinitions).toBeCalled();
174   expect(wrapper.state().definitions).toEqual(definitions);
175   expect(wrapper.state().loadingAlmDefinitions).toBe(false);
176 });
177
178 it('should detect the current ALM from the query', () => {
179   let wrapper = shallowRender({ location: mockLocation() });
180   expect(wrapper.state().currentAlmTab).toBe(AlmKeys.GitHub);
181
182   wrapper = shallowRender({ location: mockLocation({ query: { alm: AlmKeys.BitbucketCloud } }) });
183   expect(wrapper.state().currentAlmTab).toBe(AlmKeys.BitbucketServer);
184
185   wrapper.setProps({ location: mockLocation({ query: { alm: AlmKeys.GitLab } }) });
186   expect(wrapper.state().currentAlmTab).toBe(AlmKeys.GitLab);
187 });
188
189 function shallowRender(props: Partial<AlmIntegration['props']> = {}) {
190   return shallow<AlmIntegration>(
191     <AlmIntegration
192       appState={mockAppState({ branchesEnabled: true })}
193       location={mockLocation()}
194       router={mockRouter()}
195       {...props}
196     />
197   );
198 }