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 GitHubActionTutorial, { GitHubActionTutorialProps } from '../GitHubActionTutorial';
43 jest.mock('../../../../api/user-tokens');
45 jest.mock('../../../../api/settings', () => ({
46 getAllValues: jest.fn().mockResolvedValue([]),
49 const tokenMock = new UserTokensMock();
56 ...getCommonNodes(TutorialModes.GitHubActions),
57 ...getTutorialActionButtons(),
58 ...getTutorialBuildButtons(),
61 it('should follow and complete all steps', async () => {
62 const user = userEvent.setup();
63 renderGithubActionTutorial();
65 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
68 expect(getCopyToClipboardValue()).toMatchSnapshot('sonar token key');
69 expect(getCopyToClipboardValue(1)).toMatchSnapshot('sonarqube host url key');
70 expect(getCopyToClipboardValue(2)).toMatchSnapshot('sonarqube host url value');
71 await user.click(ui.continueButton.get());
73 // Create/update configuration file step
75 await user.click(ui.mavenBuildButton.get());
76 expect(getCopyToClipboardValue(1)).toMatchSnapshot('Maven: .github/workflows/build.yml');
79 await user.click(ui.gradleBuildButton.get());
80 expect(getCopyToClipboardValue(2)).toMatchSnapshot('Groovy: build.gradle');
81 await user.click(ui.gradleDSLButton(GradleBuildDSL.Kotlin).get());
82 expect(getCopyToClipboardValue(2)).toMatchSnapshot('Kotlin: build.gradle.kts');
83 expect(getCopyToClipboardValue(4)).toMatchSnapshot('Gradle: .github/workflows/build.yml');
86 await user.click(ui.dotnetBuildButton.get());
87 expect(getCopyToClipboardValue(1)).toMatchSnapshot('.NET: .github/workflows/build.yml');
90 await user.click(ui.cFamilyBuildButton.get());
91 expect(getCopyToClipboardValue()).toMatchSnapshot('CFamily: sonar-project.properties');
92 await user.click(ui.linuxButton.get());
93 expect(getCopyToClipboardValue(2)).toMatchSnapshot('CFamily Linux: .github/workflows/build.yml');
94 await user.click(ui.windowsButton.get());
95 expect(getCopyToClipboardValue(2)).toMatchSnapshot(
96 'CFamily Windows: .github/workflows/build.yml'
98 await user.click(ui.macosButton.get());
99 expect(getCopyToClipboardValue(2)).toMatchSnapshot('CFamily MacOS: .github/workflows/build.yml');
102 await user.click(ui.otherBuildButton.get());
103 expect(getCopyToClipboardValue()).toMatchSnapshot('Other: sonar-project.properties');
104 expect(getCopyToClipboardValue(2)).toMatchSnapshot('Other: .github/workflows/build.yml');
106 await user.click(ui.finishTutorialButton.get());
107 expect(ui.allSetSentence.get()).toBeInTheDocument();
110 it('should generate/delete a new token or use existing one', async () => {
111 const user = userEvent.setup();
112 renderGithubActionTutorial();
114 expect(await ui.secretsStepTitle.find()).toBeInTheDocument();
117 await user.click(ui.genTokenDialogButton.get());
118 await user.click(ui.generateTokenButton.get());
119 expect(getCopyToClipboardValue(3)).toEqual('generatedtoken2');
121 // Revoke current token and create new one
122 await user.click(ui.deleteTokenButton.get());
123 await user.type(ui.tokenNameInput.get(), 'newtoken');
124 await selectEvent.select(ui.expiresInSelect.get(), 'users.tokens.expiration.365');
125 await user.click(ui.generateTokenButton.get());
126 expect(ui.tokenValue.get()).toBeInTheDocument();
127 await user.click(ui.continueButton.getAll()[1]);
128 expect(ui.tokenValue.query()).not.toBeInTheDocument();
131 it('navigates between steps', async () => {
132 const user = userEvent.setup();
133 renderGithubActionTutorial({
134 almBinding: mockAlmSettingsInstance({
136 url: 'http://localhost/qube',
138 projectBinding: mockProjectAlmBindingResponse({ alm: AlmKeys.GitHub }),
141 // If project is bound, link to repo is visible
142 expect(await ui.linkToRepo.find()).toBeInTheDocument();
144 await user.click(await ui.continueButton.find());
145 await user.click(ui.mavenBuildButton.get());
146 await user.click(ui.finishTutorialButton.get());
147 expect(ui.allSetSentence.get()).toBeInTheDocument();
149 await user.click(ui.ymlFileStepTitle.get());
150 expect(ui.mavenBuildButton.get()).toBeInTheDocument();
151 await user.click(ui.secretsStepTitle.get());
152 expect(ui.genTokenDialogButton.get()).toBeInTheDocument();
155 function renderGithubActionTutorial(
156 overrides: Partial<GitHubActionTutorialProps> = {},
157 { languages = { c: mockLanguage({ key: 'c' }) } }: RenderContext = {}
161 <GitHubActionTutorial
162 baseUrl="http://localhost:9000"
163 mainBranchName="main"
164 component={mockComponent()}
165 currentUser={mockLoggedInUser()}
168 { languages, featureList: [Feature.BranchSupport] }