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 selectEvent from 'react-select-event';
23 import AlmSettingsServiceMock from '../../../../api/mocks/AlmSettingsServiceMock';
24 import UserTokensMock from '../../../../api/mocks/UserTokensMock';
25 import { mockAlmSettingsInstance } from '../../../../helpers/mocks/alm-settings';
26 import { mockComponent } from '../../../../helpers/mocks/component';
27 import { mockLanguage, mockLoggedInUser } from '../../../../helpers/testMocks';
28 import { RenderContext, renderApp } from '../../../../helpers/testReactTestingUtils';
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.otherBuildButton.get());
133 expect(getCopyToClipboardValue(0, 'Copy')).toMatchSnapshot(
134 'C++ (automatic) and other: sonar-project.properties',
136 expect(getCopyToClipboardValue(1, 'Copy')).toMatchSnapshot(
137 'C++ (automatic) and other: .github/workflows/build.yml',
140 expect(ui.allSetSentence.get()).toBeInTheDocument();
143 it('should generate/delete a new token or use existing one', async () => {
144 const user = userEvent.setup();
145 renderBitbucketPipelinesTutorial();
147 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
150 await user.click(ui.genTokenDialogButton.get());
151 await user.click(ui.generateTokenButton.get());
152 expect(getCopyToClipboardValue()).toEqual('generatedtoken2');
154 // Revoke current token and create new one
155 await user.click(ui.deleteTokenButton.get());
156 await user.type(ui.tokenNameInput.get(), 'newtoken');
157 await selectEvent.select(ui.expiresInSelect.get(), 'users.tokens.expiration.365');
158 await user.click(ui.generateTokenButton.get());
159 expect(ui.tokenValue.get()).toBeInTheDocument();
160 await user.click(ui.continueButton.getAll()[0]);
161 expect(ui.tokenValue.query()).not.toBeInTheDocument();
164 it('navigates between steps', async () => {
165 const user = userEvent.setup();
166 almMock.handleSetProjectBinding(AlmKeys.GitHub, {
167 almSetting: 'my-project',
168 project: 'my-project',
169 repository: 'my-project',
172 renderBitbucketPipelinesTutorial({
173 almBinding: mockAlmSettingsInstance({
174 alm: AlmKeys.BitbucketCloud,
175 url: 'http://localhost/qube',
179 // If project is bound, link to repo is visible
180 expect(await ui.linkToRepo.find()).toBeInTheDocument();
182 await user.click(ui.mavenBuildButton.get());
183 expect(ui.allSetSentence.get()).toBeInTheDocument();
185 await user.click(ui.ymlFileStepTitle.get());
186 expect(ui.mavenBuildButton.get()).toBeInTheDocument();
187 await user.click(ui.secretsStepTitle.get());
188 expect(ui.genTokenDialogButton.get()).toBeInTheDocument();
191 function renderBitbucketPipelinesTutorial(
192 overrides: Partial<BitbucketPipelinesTutorialProps> = {},
193 { languages = { c: mockLanguage({ key: 'c' }) } }: RenderContext = {},
197 <BitbucketPipelinesTutorial
198 baseUrl="http://localhost:9000"
199 mainBranchName="main"
200 component={mockComponent()}
201 currentUser={mockLoggedInUser()}
204 { languages, featureList: [Feature.BranchSupport] },