You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Azure-it.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. import { screen } from '@testing-library/react';
  21. import userEvent from '@testing-library/user-event';
  22. import * as React from 'react';
  23. import selectEvent from 'react-select-event';
  24. import { searchAzureRepositories } from '../../../../api/alm-integrations';
  25. import AlmIntegrationsServiceMock from '../../../../api/mocks/AlmIntegrationsServiceMock';
  26. import DopTranslationServiceMock from '../../../../api/mocks/DopTranslationServiceMock';
  27. import NewCodeDefinitionServiceMock from '../../../../api/mocks/NewCodeDefinitionServiceMock';
  28. import { renderApp } from '../../../../helpers/testReactTestingUtils';
  29. import { byLabelText, byRole, byText } from '../../../../helpers/testSelector';
  30. import CreateProjectPage from '../CreateProjectPage';
  31. jest.mock('../../../../api/alm-integrations');
  32. jest.mock('../../../../api/alm-settings');
  33. let almIntegrationHandler: AlmIntegrationsServiceMock;
  34. let dopTranslationHandler: DopTranslationServiceMock;
  35. let newCodePeriodHandler: NewCodeDefinitionServiceMock;
  36. const ui = {
  37. azureCreateProjectButton: byText('onboarding.create_project.select_method.azure'),
  38. personalAccessTokenInput: byRole('textbox', {
  39. name: /onboarding.create_project.enter_pat/,
  40. }),
  41. instanceSelector: byLabelText(/alm.configuration.selector.label/),
  42. };
  43. const original = window.location;
  44. beforeAll(() => {
  45. Object.defineProperty(window, 'location', {
  46. configurable: true,
  47. value: { replace: jest.fn() },
  48. });
  49. almIntegrationHandler = new AlmIntegrationsServiceMock();
  50. dopTranslationHandler = new DopTranslationServiceMock();
  51. newCodePeriodHandler = new NewCodeDefinitionServiceMock();
  52. });
  53. beforeEach(() => {
  54. jest.clearAllMocks();
  55. almIntegrationHandler.reset();
  56. dopTranslationHandler.reset();
  57. newCodePeriodHandler.reset();
  58. });
  59. afterAll(() => {
  60. Object.defineProperty(window, 'location', { configurable: true, value: original });
  61. });
  62. it('should ask for PAT when it is not set yet and show the import project feature afterwards', async () => {
  63. const user = userEvent.setup();
  64. renderCreateProject();
  65. expect(await screen.findByText('onboarding.create_project.azure.title')).toBeInTheDocument();
  66. expect(screen.getByText('alm.configuration.selector.label.alm.azure.long')).toBeInTheDocument();
  67. expect(screen.getByText('onboarding.create_project.enter_pat')).toBeInTheDocument();
  68. expect(screen.getByText('onboarding.create_project.pat_form.title')).toBeInTheDocument();
  69. expect(screen.getByRole('button', { name: 'save' })).toBeInTheDocument();
  70. await user.click(ui.personalAccessTokenInput.get());
  71. await user.keyboard('secret');
  72. await user.click(screen.getByRole('button', { name: 'save' }));
  73. expect(screen.getByText('Azure project')).toBeInTheDocument();
  74. expect(screen.getByText('Azure project 2')).toBeInTheDocument();
  75. // eslint-disable-next-line jest-dom/prefer-in-document
  76. expect(screen.getAllByText('onboarding.create_project.repository_imported')).toHaveLength(1);
  77. });
  78. it('should show import project feature when PAT is already set', async () => {
  79. const user = userEvent.setup();
  80. renderCreateProject();
  81. expect(await screen.findByText('onboarding.create_project.azure.title')).toBeInTheDocument();
  82. await selectEvent.select(ui.instanceSelector.get(), [/conf-azure-2/]);
  83. expect(await screen.findByText('Azure project')).toBeInTheDocument();
  84. expect(screen.getByText('Azure project 2')).toBeInTheDocument();
  85. expect(
  86. screen.getByRole('listitem', {
  87. name: 'Azure repo 1',
  88. }),
  89. ).toBeInTheDocument();
  90. expect(
  91. screen.getByRole('listitem', {
  92. name: 'Azure repo 2',
  93. }),
  94. ).toBeInTheDocument();
  95. const importButton = screen.getByText('onboarding.create_project.import');
  96. await user.click(importButton);
  97. expect(
  98. screen.getByRole('heading', { name: 'onboarding.create_x_project.new_code_definition.title1' }),
  99. ).toBeInTheDocument();
  100. await user.click(screen.getByRole('radio', { name: 'new_code_definition.global_setting' }));
  101. await user.click(
  102. screen.getByRole('button', {
  103. name: 'onboarding.create_project.new_code_definition.create_x_projects1',
  104. }),
  105. );
  106. expect(await screen.findByText('/dashboard?id=key')).toBeInTheDocument();
  107. });
  108. it('should show search filter when PAT is already set', async () => {
  109. const user = userEvent.setup();
  110. renderCreateProject();
  111. expect(await screen.findByText('onboarding.create_project.azure.title')).toBeInTheDocument();
  112. await selectEvent.select(ui.instanceSelector.get(), [/conf-azure-2/]);
  113. // Should search with positive results
  114. const inputSearch = await screen.findByPlaceholderText(
  115. 'onboarding.create_project.search_projects_repositories',
  116. );
  117. await user.click(inputSearch);
  118. await user.keyboard('s');
  119. expect(searchAzureRepositories).toHaveBeenCalledWith('conf-azure-2', 's');
  120. // Should search with empty results
  121. almIntegrationHandler.setSearchAzureRepositories([]);
  122. await user.keyboard('f');
  123. expect(screen.getByText('onboarding.create_project.azure.no_results')).toBeInTheDocument();
  124. });
  125. function renderCreateProject() {
  126. renderApp('project/create', <CreateProjectPage />, {
  127. navigateTo: 'project/create?mode=azure',
  128. });
  129. }