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';
28 } from '../../../../../api/alm-settings';
29 import { AlmKeys, AlmSettingsBindingStatusType } from '../../../../../types/alm-settings';
30 import { AlmIntegration } from '../AlmIntegration';
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('')
43 it('should render correctly', () => {
44 expect(shallowRender()).toMatchSnapshot();
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' }]
55 const wrapper = shallowRender();
57 await waitAndUpdate(wrapper);
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');
66 it('should handle alm selection', async () => {
67 const wrapper = shallowRender();
69 wrapper.setState({ currentAlm: AlmKeys.Azure });
71 wrapper.instance().handleSelectAlm(AlmKeys.GitHub);
73 await waitAndUpdate(wrapper);
75 expect(wrapper.state().currentAlm).toBe(AlmKeys.GitHub);
78 it('should handle delete', async () => {
79 const toBeDeleted = '45672';
80 (countBindedProjects as jest.Mock).mockResolvedValueOnce(7);
81 const wrapper = shallowRender();
83 wrapper.instance().handleDelete(toBeDeleted);
84 await waitAndUpdate(wrapper);
86 expect(wrapper.state().projectCount).toBe(7);
87 expect(wrapper.state().definitionKeyForDeletion).toBe(toBeDeleted);
90 it('should delete configuration', async () => {
91 (deleteConfiguration as jest.Mock).mockResolvedValueOnce(undefined);
92 const wrapper = shallowRender();
93 wrapper.instance().deleteConfiguration('8345678');
95 await waitAndUpdate(wrapper);
96 expect(wrapper.state().projectCount).toBeUndefined();
97 expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
100 it('should validate a configuration', async () => {
101 const definitionKey = 'validated-key';
102 const failureMessage = 'an error occured';
104 const wrapper = shallowRender();
105 await waitAndUpdate(wrapper);
107 (validateAlmSettings as jest.Mock)
108 .mockRejectedValueOnce(undefined)
109 .mockResolvedValueOnce(failureMessage)
110 .mockResolvedValueOnce('');
112 await wrapper.instance().handleCheck(definitionKey);
114 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
117 type: AlmSettingsBindingStatusType.Warning
120 await wrapper.instance().handleCheck(definitionKey);
122 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
125 type: AlmSettingsBindingStatusType.Failure
128 await wrapper.instance().handleCheck(definitionKey);
130 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
133 type: AlmSettingsBindingStatusType.Success
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' }]
145 const wrapper = shallowRender();
146 await waitAndUpdate(wrapper);
148 (getAlmDefinitions as jest.Mock).mockResolvedValueOnce(definitions);
150 await wrapper.instance().fetchPullRequestDecorationSetting();
152 expect(getAlmDefinitions).toBeCalled();
153 expect(wrapper.state().definitions).toEqual(definitions);
154 expect(wrapper.state().loadingAlmDefinitions).toBe(false);
157 function shallowRender() {
158 return shallow<AlmIntegration>(<AlmIntegration appState={{ branchesEnabled: true }} />);