3 * Copyright (C) 2009-2023 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.
21 import userEvent from '@testing-library/user-event';
22 import React from 'react';
23 import selectEvent from 'react-select-event';
24 import UserTokensMock from '../../../../api/mocks/UserTokensMock';
26 mockAlmSettingsInstance,
27 mockProjectAlmBindingResponse,
28 } from '../../../../helpers/mocks/alm-settings';
29 import { mockComponent } from '../../../../helpers/mocks/component';
30 import { mockLanguage, mockLoggedInUser } from '../../../../helpers/testMocks';
31 import { renderApp, RenderContext } from '../../../../helpers/testReactTestingUtils';
32 import { AlmKeys } from '../../../../types/alm-settings';
33 import { Feature } from '../../../../types/features';
36 getCopyToClipboardValue,
37 getTutorialActionButtons,
38 getTutorialBuildButtons,
39 } from '../../test-utils';
40 import { GradleBuildDSL, TutorialModes } from '../../types';
41 import BitbucketPipelinesTutorial, {
42 BitbucketPipelinesTutorialProps,
43 } from '../BitbucketPipelinesTutorial';
45 jest.mock('../../../../api/user-tokens');
47 jest.mock('../../../../api/settings', () => ({
48 getAllValues: jest.fn().mockResolvedValue([]),
51 const tokenMock = new UserTokensMock();
58 ...getCommonNodes(TutorialModes.BitbucketPipelines),
59 ...getTutorialActionButtons(),
60 ...getTutorialBuildButtons(),
63 it('should follow and complete all steps', async () => {
64 const user = userEvent.setup();
65 renderBitbucketPipelinesTutorial();
67 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
70 expect(getCopyToClipboardValue()).toMatchSnapshot('sonar token key');
71 expect(getCopyToClipboardValue(1)).toMatchSnapshot('sonarqube host url key');
72 expect(getCopyToClipboardValue(2)).toMatchSnapshot('sonarqube host url value');
73 await user.click(ui.continueButton.get());
75 // Create/update configuration file step
77 await user.click(ui.mavenBuildButton.get());
78 expect(getCopyToClipboardValue(1)).toMatchSnapshot('Maven: bitbucket-pipelines.yml');
81 await user.click(ui.gradleBuildButton.get());
82 expect(getCopyToClipboardValue(2)).toMatchSnapshot('Groovy: build.gradle');
83 await user.click(ui.gradleDSLButton(GradleBuildDSL.Kotlin).get());
84 expect(getCopyToClipboardValue(2)).toMatchSnapshot('Kotlin: build.gradle.kts');
85 expect(getCopyToClipboardValue(4)).toMatchSnapshot('Gradle: bitbucket-pipelines.yml');
88 await user.click(ui.dotnetBuildButton.get());
89 expect(getCopyToClipboardValue(1)).toMatchSnapshot('.NET: bitbucket-pipelines.yml');
92 await user.click(ui.cFamilyBuildButton.get());
93 expect(getCopyToClipboardValue()).toMatchSnapshot('CFamily: sonar-project.properties');
94 expect(getCopyToClipboardValue(2)).toMatchSnapshot('CFamily: bitbucket-pipelines.yml');
97 await user.click(ui.otherBuildButton.get());
98 expect(getCopyToClipboardValue()).toMatchSnapshot('Other: sonar-project.properties');
99 expect(getCopyToClipboardValue(2)).toMatchSnapshot('Other: .github/workflows/build.yml');
101 await user.click(ui.finishTutorialButton.get());
102 expect(ui.allSetSentence.get()).toBeInTheDocument();
105 it('should generate/delete a new token or use existing one', async () => {
106 const user = userEvent.setup();
107 renderBitbucketPipelinesTutorial();
109 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
112 await user.click(ui.genTokenDialogButton.get());
113 await user.click(ui.generateTokenButton.get());
114 expect(getCopyToClipboardValue(3)).toEqual('generatedtoken2');
116 // Revoke current token and create new one
117 await user.click(ui.deleteTokenButton.get());
118 await user.type(ui.tokenNameInput.get(), 'newtoken');
119 await selectEvent.select(ui.expiresInSelect.get(), 'users.tokens.expiration.365');
120 await user.click(ui.generateTokenButton.get());
121 expect(ui.tokenValue.get()).toBeInTheDocument();
122 await user.click(ui.continueButton.getAll()[1]);
123 expect(ui.tokenValue.query()).not.toBeInTheDocument();
126 it('navigates between steps', async () => {
127 const user = userEvent.setup();
128 renderBitbucketPipelinesTutorial({
129 almBinding: mockAlmSettingsInstance({
130 alm: AlmKeys.BitbucketCloud,
131 url: 'http://localhost/qube',
133 projectBinding: mockProjectAlmBindingResponse({
134 alm: AlmKeys.BitbucketCloud,
135 repository: 'my-project',
139 // If project is bound, link to repo is visible
140 expect(await ui.linkToRepo.find()).toBeInTheDocument();
142 await user.click(await ui.continueButton.find());
143 await user.click(ui.mavenBuildButton.get());
144 await user.click(ui.finishTutorialButton.get());
145 expect(ui.allSetSentence.get()).toBeInTheDocument();
147 await user.click(ui.ymlFileStepTitle.get());
148 expect(ui.mavenBuildButton.get()).toBeInTheDocument();
149 await user.click(ui.secretsStepTitle.get());
150 expect(ui.genTokenDialogButton.get()).toBeInTheDocument();
153 function renderBitbucketPipelinesTutorial(
154 overrides: Partial<BitbucketPipelinesTutorialProps> = {},
155 { languages = { c: mockLanguage({ key: 'c' }) } }: RenderContext = {}
159 <BitbucketPipelinesTutorial
160 baseUrl="http://localhost:9000"
161 mainBranchName="main"
162 component={mockComponent()}
163 currentUser={mockLoggedInUser()}
166 { languages, featureList: [Feature.BranchSupport] }