3 * Copyright (C) 2009-2022 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';
27 } from '../../../../../api/alm-settings';
28 import { 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';
34 jest.mock('../../../../../api/alm-settings', () => ({
35 countBindedProjects: jest.fn().mockResolvedValue(0),
36 deleteConfiguration: jest.fn().mockResolvedValue(undefined),
37 getAlmDefinitions: jest
39 .mockResolvedValue({ azure: [], bitbucket: [], bitbucketcloud: [], github: [], gitlab: [] }),
40 validateAlmSettings: jest.fn().mockResolvedValue('')
47 it('should render correctly', () => {
48 expect(shallowRender()).toMatchSnapshot();
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' }]
60 const wrapper = shallowRender();
62 await waitAndUpdate(wrapper);
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');
73 it('should handle alm selection', async () => {
74 const router = mockRouter();
75 const wrapper = shallowRender({ router });
77 wrapper.setState({ currentAlmTab: AlmKeys.Azure });
79 wrapper.instance().handleSelectAlm(AlmKeys.GitHub);
81 await waitAndUpdate(wrapper);
83 expect(wrapper.state().currentAlmTab).toBe(AlmKeys.GitHub);
84 expect(router.push).toBeCalled();
87 it('should handle delete', async () => {
88 const toBeDeleted = '45672';
89 (countBindedProjects as jest.Mock).mockResolvedValueOnce(7);
90 const wrapper = shallowRender();
93 .find(AlmIntegrationRenderer)
95 .onDelete(toBeDeleted);
96 await waitAndUpdate(wrapper);
97 expect(wrapper.state().projectCount).toBe(7);
98 expect(wrapper.state().definitionKeyForDeletion).toBe(toBeDeleted);
101 .find(AlmIntegrationRenderer)
104 await waitAndUpdate(wrapper);
105 expect(wrapper.state().projectCount).toBeUndefined();
106 expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
109 it('should delete configuration', async () => {
110 (deleteConfiguration as jest.Mock).mockResolvedValueOnce(undefined);
111 const wrapper = shallowRender();
112 wrapper.instance().handleConfirmDelete('8345678');
114 await waitAndUpdate(wrapper);
115 expect(wrapper.state().projectCount).toBeUndefined();
116 expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
119 it('should validate a configuration', async () => {
120 const definitionKey = 'validated-key';
121 const failureMessage = 'an error occured';
123 const wrapper = shallowRender();
124 await waitAndUpdate(wrapper);
126 (validateAlmSettings as jest.Mock)
127 .mockRejectedValueOnce(undefined)
128 .mockResolvedValueOnce(failureMessage)
129 .mockResolvedValueOnce('')
130 .mockResolvedValueOnce('');
132 await wrapper.instance().handleCheck(definitionKey);
134 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
137 type: AlmSettingsBindingStatusType.Warning
140 await wrapper.instance().handleCheck(definitionKey);
142 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
145 type: AlmSettingsBindingStatusType.Failure
148 await wrapper.instance().handleCheck(definitionKey);
150 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
153 type: AlmSettingsBindingStatusType.Success
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' }]
166 const wrapper = shallowRender();
167 await waitAndUpdate(wrapper);
169 (getAlmDefinitions as jest.Mock).mockResolvedValueOnce(definitions);
171 await wrapper.instance().fetchPullRequestDecorationSetting();
173 expect(getAlmDefinitions).toBeCalled();
174 expect(wrapper.state().definitions).toEqual(definitions);
175 expect(wrapper.state().loadingAlmDefinitions).toBe(false);
178 it('should detect the current ALM from the query', () => {
179 let wrapper = shallowRender({ location: mockLocation() });
180 expect(wrapper.state().currentAlmTab).toBe(AlmKeys.GitHub);
182 wrapper = shallowRender({ location: mockLocation({ query: { alm: AlmKeys.BitbucketCloud } }) });
183 expect(wrapper.state().currentAlmTab).toBe(AlmKeys.BitbucketServer);
185 wrapper.setProps({ location: mockLocation({ query: { alm: AlmKeys.GitLab } }) });
186 expect(wrapper.state().currentAlmTab).toBe(AlmKeys.GitLab);
189 function shallowRender(props: Partial<AlmIntegration['props']> = {}) {
190 return shallow<AlmIntegration>(
192 appState={{ branchesEnabled: true }}
194 location={mockLocation()}
195 router={mockRouter()}