]> source.dussan.org Git - sonarqube.git/blob
2bb0d8445805c7df153161e11f6a1a2a6690b141
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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 { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
23 import {
24   countBindedProjects,
25   deleteConfiguration,
26   getAlmDefinitions,
27   validateAlmSettings
28 } from '../../../../../api/alm-settings';
29 import { AlmKeys, AlmSettingsBindingStatusType } from '../../../../../types/alm-settings';
30 import { AlmIntegration } from '../AlmIntegration';
31
32 jest.mock('../../../../../api/alm-settings', () => ({
33   countBindedProjects: jest.fn().mockResolvedValue(0),
34   deleteConfiguration: jest.fn().mockResolvedValue(undefined),
35   getAlmDefinitions: jest.fn().mockResolvedValue({ bitbucket: [], github: [], gitlab: [] }),
36   validateAlmSettings: jest.fn().mockResolvedValue('')
37 }));
38
39 beforeEach(() => {
40   jest.clearAllMocks();
41 });
42
43 it('should render correctly', () => {
44   expect(shallowRender()).toMatchSnapshot();
45 });
46
47 it('should validate existing configurations', async () => {
48   (getAlmDefinitions as jest.Mock).mockResolvedValueOnce({
49     [AlmKeys.Azure]: [{ key: 'a1' }],
50     [AlmKeys.Bitbucket]: [{ key: 'b1' }],
51     [AlmKeys.GitHub]: [{ key: 'gh1' }, { key: 'gh2' }],
52     [AlmKeys.GitLab]: [{ key: 'gl1' }]
53   });
54
55   const wrapper = shallowRender();
56
57   await waitAndUpdate(wrapper);
58
59   expect(validateAlmSettings).toBeCalledTimes(4);
60   expect(validateAlmSettings).toBeCalledWith('b1');
61   expect(validateAlmSettings).toBeCalledWith('gh1');
62   expect(validateAlmSettings).toBeCalledWith('gh2');
63   expect(validateAlmSettings).toBeCalledWith('gl1');
64 });
65
66 it('should handle alm selection', async () => {
67   const wrapper = shallowRender();
68
69   wrapper.setState({ currentAlm: AlmKeys.Azure });
70
71   wrapper.instance().handleSelectAlm(AlmKeys.GitHub);
72
73   await waitAndUpdate(wrapper);
74
75   expect(wrapper.state().currentAlm).toBe(AlmKeys.GitHub);
76 });
77
78 it('should handle delete', async () => {
79   const toBeDeleted = '45672';
80   (countBindedProjects as jest.Mock).mockResolvedValueOnce(7);
81   const wrapper = shallowRender();
82
83   wrapper.instance().handleDelete(toBeDeleted);
84   await waitAndUpdate(wrapper);
85
86   expect(wrapper.state().projectCount).toBe(7);
87   expect(wrapper.state().definitionKeyForDeletion).toBe(toBeDeleted);
88 });
89
90 it('should delete configuration', async () => {
91   (deleteConfiguration as jest.Mock).mockResolvedValueOnce(undefined);
92   const wrapper = shallowRender();
93   wrapper.instance().deleteConfiguration('8345678');
94
95   await waitAndUpdate(wrapper);
96   expect(wrapper.state().projectCount).toBeUndefined();
97   expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
98 });
99
100 it('should validate a configuration', async () => {
101   const definitionKey = 'validated-key';
102   const failureMessage = 'an error occured';
103
104   const wrapper = shallowRender();
105   await waitAndUpdate(wrapper);
106
107   (validateAlmSettings as jest.Mock)
108     .mockRejectedValueOnce(undefined)
109     .mockResolvedValueOnce(failureMessage)
110     .mockResolvedValueOnce('');
111
112   await wrapper.instance().handleCheck(definitionKey);
113
114   expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
115     alertSuccess: true,
116     failureMessage: '',
117     type: AlmSettingsBindingStatusType.Warning
118   });
119
120   await wrapper.instance().handleCheck(definitionKey);
121
122   expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
123     alertSuccess: true,
124     failureMessage,
125     type: AlmSettingsBindingStatusType.Failure
126   });
127
128   await wrapper.instance().handleCheck(definitionKey);
129
130   expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
131     alertSuccess: true,
132     failureMessage: '',
133     type: AlmSettingsBindingStatusType.Success
134   });
135 });
136
137 it('should fetch settings', async () => {
138   const definitions = {
139     [AlmKeys.Azure]: [{ key: 'a1' }],
140     [AlmKeys.Bitbucket]: [{ key: 'b1' }],
141     [AlmKeys.GitHub]: [{ key: 'gh1' }],
142     [AlmKeys.GitLab]: [{ key: 'gl1' }]
143   };
144
145   const wrapper = shallowRender();
146   await waitAndUpdate(wrapper);
147
148   (getAlmDefinitions as jest.Mock).mockResolvedValueOnce(definitions);
149
150   await wrapper.instance().fetchPullRequestDecorationSetting();
151
152   expect(getAlmDefinitions).toBeCalled();
153   expect(wrapper.state().definitions).toEqual(definitions);
154   expect(wrapper.state().loadingAlmDefinitions).toBe(false);
155 });
156
157 function shallowRender() {
158   return shallow<AlmIntegration>(<AlmIntegration appState={{ branchesEnabled: true }} />);
159 }