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.

GitHub-it.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 } 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 { getGithubRepositories } 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 { mockGitHubRepository } from '../../../../helpers/mocks/alm-integrations';
  29. import { renderApp } from '../../../../helpers/testReactTestingUtils';
  30. import { byLabelText, byRole, byText } from '../../../../helpers/testSelector';
  31. import CreateProjectPage from '../CreateProjectPage';
  32. jest.mock('../../../../api/alm-integrations');
  33. jest.mock('../../../../api/alm-settings');
  34. const original = window.location;
  35. let almIntegrationHandler: AlmIntegrationsServiceMock;
  36. let dopTranslationHandler: DopTranslationServiceMock;
  37. let newCodePeriodHandler: NewCodeDefinitionServiceMock;
  38. const ui = {
  39. githubCreateProjectButton: byText('onboarding.create_project.select_method.github'),
  40. instanceSelector: byLabelText(/alm.configuration.selector.label/),
  41. organizationSelector: byLabelText('onboarding.create_project.github.choose_organization'),
  42. project1: byRole('listitem', { name: 'Github repo 1' }),
  43. project1Checkbox: byRole('listitem', { name: 'Github repo 1' }).byRole('checkbox'),
  44. project2: byRole('listitem', { name: 'Github repo 2' }),
  45. project2Checkbox: byRole('listitem', { name: 'Github repo 2' }).byRole('checkbox'),
  46. project3: byRole('listitem', { name: 'Github repo 3' }),
  47. project3Checkbox: byRole('listitem', { name: 'Github repo 3' }).byRole('checkbox'),
  48. checkAll: byRole('checkbox', { name: 'onboarding.create_project.select_all_repositories' }),
  49. importButton: byRole('button', { name: 'onboarding.create_project.import' }),
  50. backButton: byRole('button', { name: 'back' }),
  51. newCodeTitle: byRole('heading', {
  52. name: 'onboarding.create_x_project.new_code_definition.title1',
  53. }),
  54. newCodeMultipleProjectTitle: byRole('heading', {
  55. name: 'onboarding.create_x_project.new_code_definition.title2',
  56. }),
  57. changePeriodLaterInfo: byText('onboarding.create_projects.new_code_definition.change_info'),
  58. createProjectButton: byRole('button', {
  59. name: 'onboarding.create_project.new_code_definition.create_x_projects1',
  60. }),
  61. createProjectsButton: byRole('button', {
  62. name: 'onboarding.create_project.new_code_definition.create_x_projects2',
  63. }),
  64. globalSettingRadio: byRole('radio', { name: 'new_code_definition.global_setting' }),
  65. createErrorMessage: byText('onboarding.create_project.github.warning.message'),
  66. };
  67. beforeAll(() => {
  68. Object.defineProperty(window, 'location', {
  69. configurable: true,
  70. value: { replace: jest.fn() },
  71. });
  72. almIntegrationHandler = new AlmIntegrationsServiceMock();
  73. dopTranslationHandler = new DopTranslationServiceMock();
  74. newCodePeriodHandler = new NewCodeDefinitionServiceMock();
  75. });
  76. beforeEach(() => {
  77. jest.clearAllMocks();
  78. almIntegrationHandler.reset();
  79. dopTranslationHandler.reset();
  80. newCodePeriodHandler.reset();
  81. });
  82. afterAll(() => {
  83. Object.defineProperty(window, 'location', { configurable: true, value: original });
  84. });
  85. it('should redirect to github authorization page when not already authorized', async () => {
  86. renderCreateProject('project/create?mode=github');
  87. expect(await screen.findByText('onboarding.create_project.github.title')).toBeInTheDocument();
  88. expect(screen.getByText('alm.configuration.selector.placeholder')).toBeInTheDocument();
  89. expect(ui.instanceSelector.get()).toBeInTheDocument();
  90. await selectEvent.select(await ui.instanceSelector.find(), [/conf-github-1/]);
  91. expect(window.location.replace).toHaveBeenCalled();
  92. });
  93. it('should not redirect to github when url is malformated', async () => {
  94. renderCreateProject('project/create?mode=github');
  95. expect(await screen.findByText('onboarding.create_project.github.title')).toBeInTheDocument();
  96. expect(screen.getByText('alm.configuration.selector.placeholder')).toBeInTheDocument();
  97. expect(ui.instanceSelector.get()).toBeInTheDocument();
  98. await waitFor(() => selectEvent.select(ui.instanceSelector.get(), [/conf-github-3/]));
  99. expect(await ui.createErrorMessage.find()).toBeInTheDocument();
  100. expect(window.location.replace).not.toHaveBeenCalled();
  101. });
  102. it('should show import project feature when the authentication is successfull', async () => {
  103. const user = userEvent.setup();
  104. renderCreateProject('project/create?mode=github&dopSetting=conf-github-2&code=213321213');
  105. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  106. await waitFor(() => selectEvent.select(ui.organizationSelector.get(), [/org-1/]));
  107. expect(await ui.project1.find()).toBeInTheDocument();
  108. expect(ui.project2.get()).toBeInTheDocument();
  109. expect(ui.checkAll.get()).not.toBeChecked();
  110. expect(ui.project1Checkbox.get()).toBeChecked();
  111. expect(ui.project1Checkbox.get()).toBeDisabled();
  112. expect(
  113. ui.project1.byText('onboarding.create_project.repository_imported').get(),
  114. ).toBeInTheDocument();
  115. expect(ui.project1.byRole('link', { name: /Github repo 1/ }).get()).toBeInTheDocument();
  116. expect(ui.project1.byRole('link', { name: /Github repo 1/ }).get()).toHaveAttribute(
  117. 'href',
  118. '/dashboard?id=key123',
  119. );
  120. expect(ui.project2Checkbox.get()).not.toBeChecked();
  121. expect(ui.project2Checkbox.get()).toBeEnabled();
  122. expect(ui.importButton.query()).not.toBeInTheDocument();
  123. await user.click(ui.project2Checkbox.get());
  124. await waitFor(() => expect(ui.checkAll.get()).toBeChecked());
  125. expect(ui.importButton.get()).toBeInTheDocument();
  126. await user.click(ui.importButton.get());
  127. expect(await ui.newCodeTitle.find()).toBeInTheDocument();
  128. await user.click(ui.globalSettingRadio.get());
  129. expect(ui.createProjectButton.get()).toBeEnabled();
  130. await user.click(ui.createProjectButton.get());
  131. expect(await screen.findByText('/dashboard?id=key')).toBeInTheDocument();
  132. });
  133. it('should import several projects', async () => {
  134. const user = userEvent.setup();
  135. almIntegrationHandler.setGithubRepositories([
  136. mockGitHubRepository({ name: 'Github repo 1', key: 'key1' }),
  137. mockGitHubRepository({ name: 'Github repo 2', key: 'key2' }),
  138. mockGitHubRepository({ name: 'Github repo 3', key: 'key3' }),
  139. ]);
  140. renderCreateProject('project/create?mode=github&dopSetting=conf-github-2&code=213321213');
  141. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  142. await waitFor(() => selectEvent.select(ui.organizationSelector.get(), [/org-1/]));
  143. expect(await ui.project1.find()).toBeInTheDocument();
  144. expect(ui.project1Checkbox.get()).not.toBeChecked();
  145. expect(ui.project2Checkbox.get()).not.toBeChecked();
  146. expect(ui.project3Checkbox.get()).not.toBeChecked();
  147. expect(ui.checkAll.get()).not.toBeChecked();
  148. expect(ui.importButton.query()).not.toBeInTheDocument();
  149. await user.click(ui.project1Checkbox.get());
  150. expect(ui.project1Checkbox.get()).toBeChecked();
  151. expect(ui.project2Checkbox.get()).not.toBeChecked();
  152. expect(ui.project3Checkbox.get()).not.toBeChecked();
  153. expect(ui.checkAll.get()).not.toBeChecked();
  154. expect(ui.importButton.get()).toBeInTheDocument();
  155. await user.click(ui.checkAll.get());
  156. expect(ui.project1Checkbox.get()).toBeChecked();
  157. expect(ui.project2Checkbox.get()).toBeChecked();
  158. expect(ui.project3Checkbox.get()).toBeChecked();
  159. expect(ui.checkAll.get()).toBeChecked();
  160. expect(ui.importButton.get()).toBeInTheDocument();
  161. await user.click(ui.checkAll.get());
  162. expect(ui.project1Checkbox.get()).not.toBeChecked();
  163. expect(ui.project2Checkbox.get()).not.toBeChecked();
  164. expect(ui.project3Checkbox.get()).not.toBeChecked();
  165. expect(ui.checkAll.get()).not.toBeChecked();
  166. expect(ui.importButton.query()).not.toBeInTheDocument();
  167. await user.click(ui.project1Checkbox.get());
  168. await user.click(ui.project2Checkbox.get());
  169. expect(ui.importButton.get()).toBeInTheDocument();
  170. await user.click(ui.importButton.get());
  171. expect(await ui.newCodeMultipleProjectTitle.find()).toBeInTheDocument();
  172. expect(ui.changePeriodLaterInfo.get()).toBeInTheDocument();
  173. expect(ui.createProjectsButton.get()).toBeDisabled();
  174. await user.click(ui.backButton.get());
  175. expect(ui.project1Checkbox.get()).toBeChecked();
  176. expect(ui.project2Checkbox.get()).toBeChecked();
  177. expect(ui.project3Checkbox.get()).not.toBeChecked();
  178. expect(ui.importButton.get()).toBeInTheDocument();
  179. await user.click(ui.importButton.get());
  180. expect(await ui.newCodeMultipleProjectTitle.find()).toBeInTheDocument();
  181. await user.click(ui.globalSettingRadio.get());
  182. expect(ui.createProjectsButton.get()).toBeEnabled();
  183. await user.click(ui.createProjectsButton.get());
  184. expect(await screen.findByText('/projects?sort=-creation_date')).toBeInTheDocument();
  185. });
  186. it('should show search filter when the authentication is successful', async () => {
  187. const user = userEvent.setup();
  188. renderCreateProject('project/create?mode=github&dopSetting=conf-github-2&code=213321213');
  189. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  190. await waitFor(() => selectEvent.select(ui.organizationSelector.get(), [/org-1/]));
  191. const inputSearch = screen.getByRole('searchbox');
  192. await user.click(inputSearch);
  193. await user.keyboard('search');
  194. await waitFor(() => {
  195. expect(getGithubRepositories).toHaveBeenLastCalledWith({
  196. almSetting: 'conf-github-2',
  197. organization: 'org-1',
  198. page: 1,
  199. pageSize: 50,
  200. query: 'search',
  201. });
  202. });
  203. });
  204. it('should have load more', async () => {
  205. const user = userEvent.setup();
  206. almIntegrationHandler.createRandomGithubRepositoriessWithLoadMore(10, 20);
  207. renderCreateProject('project/create?mode=github&dopSetting=conf-github-2&code=213321213');
  208. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  209. await waitFor(() => selectEvent.select(ui.organizationSelector.get(), [/org-1/]));
  210. const loadMore = await screen.findByRole('button', { name: 'show_more' });
  211. expect(loadMore).toBeInTheDocument();
  212. /*
  213. * Next api call response will simulate reaching the last page so we can test the
  214. * loadmore button disapperance.
  215. */
  216. almIntegrationHandler.createRandomGithubRepositoriessWithLoadMore(20, 20);
  217. await user.click(loadMore);
  218. expect(getGithubRepositories).toHaveBeenLastCalledWith({
  219. almSetting: 'conf-github-2',
  220. organization: 'org-1',
  221. page: 2,
  222. pageSize: 50,
  223. query: '',
  224. });
  225. expect(loadMore).not.toBeInTheDocument();
  226. });
  227. it('should show no result message when there are no projects', async () => {
  228. almIntegrationHandler.setGithubRepositories([]);
  229. renderCreateProject('project/create?mode=github&dopSetting=conf-github-2&code=213321213');
  230. expect(await ui.instanceSelector.find()).toBeInTheDocument();
  231. await waitFor(() => selectEvent.select(ui.organizationSelector.get(), [/org-1/]));
  232. expect(screen.getByText('no_results')).toBeInTheDocument();
  233. });
  234. function renderCreateProject(navigateTo?: string) {
  235. renderApp('project/create', <CreateProjectPage />, {
  236. navigateTo,
  237. });
  238. }