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.

BitbucketCloud-it.tsx 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, waitFor, within } 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 { searchForBitbucketCloudRepositories } 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. import { BITBUCKET_CLOUD_PROJECTS_PAGESIZE } from '../constants';
  32. jest.mock('../../../../api/alm-integrations');
  33. jest.mock('../../../../api/alm-settings');
  34. let almIntegrationHandler: AlmIntegrationsServiceMock;
  35. let dopTranslationHandler: DopTranslationServiceMock;
  36. let newCodePeriodHandler: NewCodeDefinitionServiceMock;
  37. const ui = {
  38. bitbucketCloudCreateProjectButton: byText(
  39. 'onboarding.create_project.select_method.bitbucketcloud',
  40. ),
  41. personalAccessTokenInput: byRole('textbox', {
  42. name: /onboarding.create_project.enter_pat/,
  43. }),
  44. instanceSelector: byLabelText(/alm.configuration.selector.label/),
  45. };
  46. const original = window.location;
  47. beforeAll(() => {
  48. Object.defineProperty(window, 'location', {
  49. configurable: true,
  50. value: { replace: jest.fn() },
  51. });
  52. almIntegrationHandler = new AlmIntegrationsServiceMock();
  53. dopTranslationHandler = new DopTranslationServiceMock();
  54. newCodePeriodHandler = new NewCodeDefinitionServiceMock();
  55. });
  56. beforeEach(() => {
  57. jest.clearAllMocks();
  58. almIntegrationHandler.reset();
  59. dopTranslationHandler.reset();
  60. newCodePeriodHandler.reset();
  61. });
  62. afterAll(() => {
  63. Object.defineProperty(window, 'location', { configurable: true, value: original });
  64. });
  65. it('should ask for PAT when it is not set yet and show the import project feature afterwards', async () => {
  66. const user = userEvent.setup();
  67. renderCreateProject();
  68. expect(screen.getByText('onboarding.create_project.bitbucketcloud.title')).toBeInTheDocument();
  69. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  70. expect(
  71. screen.getByText('onboarding.create_project.bitbucket_cloud.enter_password'),
  72. ).toBeInTheDocument();
  73. expect(
  74. screen.getByText('onboarding.create_project.enter_password.instructions.bitbucket_cloud'),
  75. ).toBeInTheDocument();
  76. expect(
  77. screen.getByText(
  78. 'onboarding.create_project.pat.expired.info_message onboarding.create_project.pat.expired.info_message_contact',
  79. ),
  80. ).toBeInTheDocument();
  81. expect(screen.getByRole('button', { name: 'save' })).toBeDisabled();
  82. await user.click(
  83. screen.getByRole('textbox', {
  84. name: /onboarding.create_project.bitbucket_cloud.enter_username/,
  85. }),
  86. );
  87. await user.keyboard('username');
  88. await user.click(
  89. screen.getByRole('textbox', {
  90. name: /onboarding.create_project.bitbucket_cloud.enter_password/,
  91. }),
  92. );
  93. await user.keyboard('password');
  94. expect(screen.getByRole('button', { name: 'save' })).toBeEnabled();
  95. await user.click(screen.getByRole('button', { name: 'save' }));
  96. expect(screen.getByText('BitbucketCloud Repo 1')).toBeInTheDocument();
  97. expect(screen.getByText('BitbucketCloud Repo 2')).toBeInTheDocument();
  98. });
  99. it('should show import project feature when PAT is already set', async () => {
  100. const user = userEvent.setup();
  101. let projectItem;
  102. renderCreateProject();
  103. expect(screen.getByText('onboarding.create_project.bitbucketcloud.title')).toBeInTheDocument();
  104. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  105. await selectEvent.select(ui.instanceSelector.get(), [/conf-bitbucketcloud-2/]);
  106. expect(await screen.findByText('BitbucketCloud Repo 1')).toBeInTheDocument();
  107. expect(screen.getByText('BitbucketCloud Repo 2')).toBeInTheDocument();
  108. projectItem = screen.getByRole('listitem', { name: /BitbucketCloud Repo 1/ });
  109. expect(
  110. within(projectItem).getByText('onboarding.create_project.repository_imported'),
  111. ).toBeInTheDocument();
  112. expect(
  113. within(projectItem).getByRole('link', { name: /BitbucketCloud Repo 1/ }),
  114. ).toBeInTheDocument();
  115. expect(within(projectItem).getByRole('link', { name: /BitbucketCloud Repo 1/ })).toHaveAttribute(
  116. 'href',
  117. '/dashboard?id=key',
  118. );
  119. projectItem = screen.getByRole('listitem', { name: /BitbucketCloud Repo 2/ });
  120. const setupButton = within(projectItem).getByRole('button', {
  121. name: 'onboarding.create_project.import',
  122. });
  123. await user.click(setupButton);
  124. expect(
  125. screen.getByRole('heading', { name: 'onboarding.create_x_project.new_code_definition.title1' }),
  126. ).toBeInTheDocument();
  127. await user.click(screen.getByRole('radio', { name: 'new_code_definition.global_setting' }));
  128. await user.click(
  129. screen.getByRole('button', {
  130. name: 'onboarding.create_project.new_code_definition.create_x_projects1',
  131. }),
  132. );
  133. expect(await screen.findByText('/dashboard?id=key')).toBeInTheDocument();
  134. });
  135. it('should show search filter when PAT is already set', async () => {
  136. const user = userEvent.setup();
  137. renderCreateProject();
  138. expect(screen.getByText('onboarding.create_project.bitbucketcloud.title')).toBeInTheDocument();
  139. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  140. await selectEvent.select(ui.instanceSelector.get(), [/conf-bitbucketcloud-2/]);
  141. await waitFor(() =>
  142. expect(searchForBitbucketCloudRepositories).toHaveBeenLastCalledWith(
  143. 'conf-bitbucketcloud-2',
  144. '',
  145. BITBUCKET_CLOUD_PROJECTS_PAGESIZE,
  146. 1,
  147. ),
  148. );
  149. const inputSearch = screen.getByRole('searchbox', {
  150. name: 'onboarding.create_project.search_prompt',
  151. });
  152. await user.click(inputSearch);
  153. await user.keyboard('search');
  154. expect(searchForBitbucketCloudRepositories).toHaveBeenLastCalledWith(
  155. 'conf-bitbucketcloud-2',
  156. 'search',
  157. BITBUCKET_CLOUD_PROJECTS_PAGESIZE,
  158. 1,
  159. );
  160. });
  161. it('should show no result message when there are no projects', async () => {
  162. almIntegrationHandler.setBitbucketCloudRepositories([]);
  163. renderCreateProject();
  164. expect(screen.getByText('onboarding.create_project.bitbucketcloud.title')).toBeInTheDocument();
  165. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  166. await selectEvent.select(ui.instanceSelector.get(), [/conf-bitbucketcloud-2/]);
  167. expect(
  168. await screen.findByText('onboarding.create_project.bitbucketcloud.no_projects'),
  169. ).toBeInTheDocument();
  170. });
  171. it('should have load more', async () => {
  172. const user = userEvent.setup();
  173. almIntegrationHandler.createRandomBitbucketCloudProjectsWithLoadMore(
  174. BITBUCKET_CLOUD_PROJECTS_PAGESIZE,
  175. BITBUCKET_CLOUD_PROJECTS_PAGESIZE + 1,
  176. );
  177. renderCreateProject();
  178. expect(screen.getByText('onboarding.create_project.bitbucketcloud.title')).toBeInTheDocument();
  179. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  180. await selectEvent.select(ui.instanceSelector.get(), [/conf-bitbucketcloud-2/]);
  181. expect(await screen.findByRole('button', { name: 'show_more' })).toBeInTheDocument();
  182. /*
  183. * Next api call response will simulate reaching the last page so we can test the
  184. * loadmore button disapperance.
  185. */
  186. almIntegrationHandler.createRandomBitbucketCloudProjectsWithLoadMore(
  187. BITBUCKET_CLOUD_PROJECTS_PAGESIZE + 1,
  188. BITBUCKET_CLOUD_PROJECTS_PAGESIZE + 1,
  189. );
  190. await user.click(screen.getByRole('button', { name: 'show_more' }));
  191. expect(searchForBitbucketCloudRepositories).toHaveBeenLastCalledWith(
  192. 'conf-bitbucketcloud-2',
  193. '',
  194. BITBUCKET_CLOUD_PROJECTS_PAGESIZE,
  195. 2,
  196. );
  197. await waitFor(() => {
  198. expect(screen.queryByRole('button', { name: 'show_more' })).not.toBeInTheDocument();
  199. });
  200. });
  201. function renderCreateProject() {
  202. renderApp('project/create', <CreateProjectPage />, {
  203. navigateTo: 'project/create?mode=bitbucketcloud',
  204. });
  205. }