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 } 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({ 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).toBeCalledWith('gh1');
60 expect(validateAlmSettings).toBeCalledWith('gh2');
61 expect(validateAlmSettings).toBeCalledWith('gl1');
64 it('should handle alm selection', async () => {
65 const wrapper = shallowRender();
67 wrapper.setState({ currentAlm: AlmKeys.Azure });
69 wrapper.instance().handleSelectAlm(AlmKeys.GitHub);
71 await waitAndUpdate(wrapper);
73 expect(wrapper.state().currentAlm).toBe(AlmKeys.GitHub);
76 it('should handle delete', async () => {
77 const toBeDeleted = '45672';
78 (countBindedProjects as jest.Mock).mockResolvedValueOnce(7);
79 const wrapper = shallowRender();
81 wrapper.instance().handleDelete(toBeDeleted);
82 await waitAndUpdate(wrapper);
84 expect(wrapper.state().projectCount).toBe(7);
85 expect(wrapper.state().definitionKeyForDeletion).toBe(toBeDeleted);
88 it('should delete configuration', async () => {
89 (deleteConfiguration as jest.Mock).mockResolvedValueOnce(undefined);
90 const wrapper = shallowRender();
91 wrapper.instance().deleteConfiguration('8345678');
93 await waitAndUpdate(wrapper);
94 expect(wrapper.state().projectCount).toBeUndefined();
95 expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
98 it('should validate a configuration', async () => {
99 const definitionKey = 'validated-key';
100 const errorMessage = 'an error occured';
102 const wrapper = shallowRender();
103 await waitAndUpdate(wrapper);
105 (validateAlmSettings as jest.Mock)
106 .mockResolvedValueOnce(undefined)
107 .mockResolvedValueOnce(errorMessage)
108 .mockResolvedValueOnce('');
110 await wrapper.instance().handleCheck(definitionKey);
112 expect(wrapper.state().definitionStatus[definitionKey]).toEqual(
113 expect.objectContaining({
118 await wrapper.instance().handleCheck(definitionKey);
120 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
126 await wrapper.instance().handleCheck(definitionKey);
128 expect(wrapper.state().definitionStatus[definitionKey]).toEqual({
135 it('should fetch settings', async () => {
136 const definitions = {
137 [AlmKeys.Azure]: [{ key: 'a1' }],
138 [AlmKeys.Bitbucket]: [{ key: 'b1' }],
139 [AlmKeys.GitHub]: [{ key: 'gh1' }],
140 [AlmKeys.GitLab]: [{ key: 'gl1' }]
143 const wrapper = shallowRender();
144 await waitAndUpdate(wrapper);
146 (getAlmDefinitions as jest.Mock).mockResolvedValueOnce(definitions);
148 await wrapper.instance().fetchPullRequestDecorationSetting();
150 expect(getAlmDefinitions).toBeCalled();
151 expect(wrapper.state().definitions).toEqual(definitions);
152 expect(wrapper.state().loadingAlmDefinitions).toBe(false);
155 function shallowRender() {
156 return shallow<AlmIntegration>(<AlmIntegration appState={{ branchesEnabled: true }} />);