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 { 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(1)).toMatchSnapshot('Gradle: build.gradle');
83 expect(getCopyToClipboardValue(3)).toMatchSnapshot('Gradle: bitbucket-pipelines.yml');
86 await user.click(ui.dotnetBuildButton.get());
87 expect(getCopyToClipboardValue(1)).toMatchSnapshot('.NET: bitbucket-pipelines.yml');
90 await user.click(ui.cFamilyBuildButton.get());
91 expect(getCopyToClipboardValue()).toMatchSnapshot('CFamily: sonar-project.properties');
92 expect(getCopyToClipboardValue(2)).toMatchSnapshot('CFamily: bitbucket-pipelines.yml');
95 await user.click(ui.otherBuildButton.get());
96 expect(getCopyToClipboardValue()).toMatchSnapshot('Other: sonar-project.properties');
97 expect(getCopyToClipboardValue(2)).toMatchSnapshot('Other: .github/workflows/build.yml');
99 await user.click(ui.finishTutorialButton.get());
100 expect(ui.allSetSentence.get()).toBeInTheDocument();
103 it('should generate/delete a new token or use existing one', async () => {
104 const user = userEvent.setup();
105 renderBitbucketPipelinesTutorial();
107 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
110 await user.click(ui.genTokenDialogButton.get());
111 await user.click(ui.generateTokenButton.get());
112 expect(getCopyToClipboardValue(3)).toEqual('generatedtoken2');
114 // Revoke current token and create new one
115 await user.click(ui.deleteTokenButton.get());
116 await user.type(ui.tokenNameInput.get(), 'newtoken');
117 await selectEvent.select(ui.expiresInSelect.get(), 'users.tokens.expiration.365');
118 await user.click(ui.generateTokenButton.get());
119 expect(ui.tokenValue.get()).toBeInTheDocument();
120 await user.click(ui.continueButton.getAll()[1]);
121 expect(ui.tokenValue.query()).not.toBeInTheDocument();
124 it('navigates between steps', async () => {
125 const user = userEvent.setup();
126 renderBitbucketPipelinesTutorial({
127 almBinding: mockAlmSettingsInstance({
128 alm: AlmKeys.BitbucketCloud,
129 url: 'http://localhost/qube',
131 projectBinding: mockProjectAlmBindingResponse({
132 alm: AlmKeys.BitbucketCloud,
133 repository: 'my-project',
137 // If project is bound, link to repo is visible
138 expect(await ui.linkToRepo.find()).toBeInTheDocument();
140 await user.click(await ui.continueButton.find());
141 await user.click(ui.mavenBuildButton.get());
142 await user.click(ui.finishTutorialButton.get());
143 expect(ui.allSetSentence.get()).toBeInTheDocument();
145 await user.click(ui.ymlFileStepTitle.get());
146 expect(ui.mavenBuildButton.get()).toBeInTheDocument();
147 await user.click(ui.secretsStepTitle.get());
148 expect(ui.genTokenDialogButton.get()).toBeInTheDocument();
151 function renderBitbucketPipelinesTutorial(
152 overrides: Partial<BitbucketPipelinesTutorialProps> = {},
153 { languages = { c: mockLanguage({ key: 'c' }) } }: RenderContext = {}
157 <BitbucketPipelinesTutorial
158 baseUrl="http://localhost:9000"
159 mainBranchName="main"
160 component={mockComponent()}
161 currentUser={mockLoggedInUser()}
164 { languages, featureList: [Feature.BranchSupport] }