3 * Copyright (C) 2009-2024 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 userEvent from '@testing-library/user-event';
21 import React from 'react';
22 import AlmSettingsServiceMock from '../../../../api/mocks/AlmSettingsServiceMock';
23 import UserTokensMock from '../../../../api/mocks/UserTokensMock';
24 import { mockAlmSettingsInstance } from '../../../../helpers/mocks/alm-settings';
25 import { mockComponent } from '../../../../helpers/mocks/component';
26 import { mockLanguage, mockLoggedInUser } from '../../../../helpers/testMocks';
27 import { RenderContext, renderApp } from '../../../../helpers/testReactTestingUtils';
28 import { byRole } from '../../../../sonar-aligned/helpers/testSelector';
29 import { AlmKeys } from '../../../../types/alm-settings';
30 import { Feature } from '../../../../types/features';
33 getCopyToClipboardValue,
34 getTutorialActionButtons,
35 getTutorialBuildButtons,
36 } from '../../test-utils';
37 import { GradleBuildDSL, TutorialModes } from '../../types';
38 import BitbucketPipelinesTutorial, {
39 BitbucketPipelinesTutorialProps,
40 } from '../BitbucketPipelinesTutorial';
42 jest.mock('../../../../api/settings', () => ({
43 getAllValues: jest.fn().mockResolvedValue([]),
46 const tokenMock = new UserTokensMock();
47 const almMock = new AlmSettingsServiceMock();
55 ...getCommonNodes(TutorialModes.BitbucketPipelines),
56 ...getTutorialActionButtons(),
57 ...getTutorialBuildButtons(),
60 it('should follow and complete all steps', async () => {
61 const user = userEvent.setup();
62 renderBitbucketPipelinesTutorial();
64 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
67 expect(getCopyToClipboardValue(0, 'Copy to clipboard')).toMatchSnapshot('sonar token key');
68 expect(getCopyToClipboardValue(1, 'Copy to clipboard')).toMatchSnapshot('sonarqube host url key');
69 expect(getCopyToClipboardValue(2, 'Copy to clipboard')).toMatchSnapshot(
70 'sonarqube host url value',
73 // Create/update configuration file step
75 await user.click(ui.mavenBuildButton.get());
76 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot('Maven: bitbucket-pipelines.yml');
79 await user.click(ui.gradleBuildButton.get());
80 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot('Groovy: build.gradle');
81 await user.click(ui.gradleDSLButton(GradleBuildDSL.Kotlin).get());
82 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot('Kotlin: build.gradle.kts');
83 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot('Gradle: bitbucket-pipelines.yml');
86 await user.click(ui.dotnetBuildButton.get());
87 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot('.NET: bitbucket-pipelines.yml');
90 await user.click(ui.cppBuildButton.get());
91 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot(
92 'C++ (automatic) and other: sonar-project.properties',
94 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot(
95 'C++ (automatic) and other: bitbucket-pipelines.yml',
99 await user.click(ui.autoConfigManual.get());
100 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot(
101 'C++ (manual) and Objective-C: sonar-project.properties',
103 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot(
104 'C++ (manual) and Objective-C: bitbucket-pipelines.yml',
106 await user.click(ui.arm64Button.get());
107 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot(
108 'C++ (manual arm64) and Objective-C: sonar-project.properties',
110 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot(
111 'C++ (manual arm64) and Objective-C: bitbucket-pipelines.yml',
115 await user.click(ui.objCBuildButton.get());
116 await user.click(ui.x86_64Button.get());
117 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot(
118 'C++ (manual) and Objective-C: bitbucket-pipelines.yml',
120 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot(
121 'C++ (manual) and Objective-C: sonar-project.properties',
123 await user.click(ui.arm64Button.get());
124 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot(
125 'C++ (manual arm64) and Objective-C: bitbucket-pipelines.yml',
127 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot(
128 'C++ (manual arm64) and Objective-C: sonar-project.properties',
132 await user.click(ui.dartBuildButton.get());
133 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot('Dart: sonar-project.properties');
134 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot('Dart: bitbucket-pipelines.yml');
137 await user.click(ui.otherBuildButton.get());
138 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot(
139 'C++ (automatic) and other: sonar-project.properties',
141 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot(
142 'C++ (automatic) and other: .github/workflows/build.yml',
145 expect(ui.allSetSentence.get()).toBeInTheDocument();
148 it('should generate/delete a new token or use existing one', async () => {
149 const user = userEvent.setup();
150 renderBitbucketPipelinesTutorial();
152 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
155 await user.click(ui.genTokenDialogButton.get());
156 await user.click(ui.generateTokenButton.get());
157 expect(getCopyToClipboardValue()).toEqual('generatedtoken2');
159 // Revoke current token and create new one
160 await user.click(ui.deleteTokenButton.get());
161 await user.type(ui.tokenNameInput.get(), 'newtoken');
163 await user.click(ui.expiresInSelect.get());
164 await user.click(byRole('option', { name: 'users.tokens.expiration.365' }).get());
166 await user.click(ui.generateTokenButton.get());
167 expect(ui.tokenValue.get()).toBeInTheDocument();
168 await user.click(ui.continueButton.getAll()[0]);
169 expect(ui.tokenValue.query()).not.toBeInTheDocument();
172 it('navigates between steps', async () => {
173 const user = userEvent.setup();
174 almMock.handleSetProjectBinding(AlmKeys.GitHub, {
175 almSetting: 'my-project',
176 project: 'my-project',
177 repository: 'my-project',
180 renderBitbucketPipelinesTutorial({
181 almBinding: mockAlmSettingsInstance({
182 alm: AlmKeys.BitbucketCloud,
183 url: 'http://localhost/qube',
187 // If project is bound, link to repo is visible
188 expect(await ui.linkToRepo.find()).toBeInTheDocument();
190 await user.click(ui.mavenBuildButton.get());
191 expect(ui.allSetSentence.get()).toBeInTheDocument();
193 await user.click(ui.ymlFileStepTitle.get());
194 expect(ui.mavenBuildButton.get()).toBeInTheDocument();
195 await user.click(ui.secretsStepTitle.get());
196 expect(ui.genTokenDialogButton.get()).toBeInTheDocument();
199 function renderBitbucketPipelinesTutorial(
200 overrides: Partial<BitbucketPipelinesTutorialProps> = {},
201 { languages = { c: mockLanguage({ key: 'c' }) } }: RenderContext = {},
205 <BitbucketPipelinesTutorial
206 baseUrl="http://localhost:9000"
207 mainBranchName="main"
208 component={mockComponent()}
209 currentUser={mockLoggedInUser()}
212 { languages, featureList: [Feature.BranchSupport] },