Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ProjectCard-test.tsx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 React from 'react';
  22. import { mockCurrentUser, mockLoggedInUser } from '../../../../../helpers/testMocks';
  23. import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
  24. import { ComponentQualifier, Visibility } from '../../../../../types/component';
  25. import { MetricKey } from '../../../../../types/metrics';
  26. import { CurrentUser } from '../../../../../types/users';
  27. import { Project } from '../../../types';
  28. import ProjectCard from '../ProjectCard';
  29. const MEASURES = {
  30. [MetricKey.ncloc]: '1000',
  31. [MetricKey.alert_status]: 'OK',
  32. [MetricKey.reliability_rating]: '1.0',
  33. [MetricKey.security_rating]: '1.0',
  34. [MetricKey.sqale_rating]: '1.0',
  35. [MetricKey.new_bugs]: '12',
  36. };
  37. const PROJECT: Project = {
  38. analysisDate: '2017-01-01',
  39. key: 'foo',
  40. measures: MEASURES,
  41. name: 'Foo',
  42. qualifier: ComponentQualifier.Project,
  43. tags: [],
  44. visibility: Visibility.Public,
  45. isScannable: false,
  46. };
  47. const USER_LOGGED_OUT = mockCurrentUser();
  48. const USER_LOGGED_IN = mockLoggedInUser();
  49. it('should not display the quality gate', () => {
  50. const project = { ...PROJECT, analysisDate: undefined };
  51. renderProjectCard(project);
  52. expect(screen.getByText('projects.not_analyzed.TRK')).toBeInTheDocument();
  53. });
  54. it('should display tags', async () => {
  55. const project = { ...PROJECT, tags: ['foo', 'bar'] };
  56. renderProjectCard(project);
  57. await expect(screen.getByText('foo')).toHaveATooltipWithContent('foo, bar');
  58. });
  59. it('should display private badge', () => {
  60. const project: Project = { ...PROJECT, visibility: Visibility.Private };
  61. renderProjectCard(project);
  62. expect(screen.getByLabelText('visibility.private')).toBeInTheDocument();
  63. });
  64. it('should display configure analysis button for logged in user and scan rights', () => {
  65. const user = mockLoggedInUser();
  66. renderProjectCard({ ...PROJECT, isScannable: true, analysisDate: undefined }, user);
  67. expect(screen.getByText('projects.configure_analysis')).toBeInTheDocument();
  68. });
  69. it('should not display configure analysis button for logged in user and without scan rights', () => {
  70. renderProjectCard({ ...PROJECT, analysisDate: undefined }, USER_LOGGED_IN);
  71. expect(screen.queryByText('projects.configure_analysis')).not.toBeInTheDocument();
  72. });
  73. it('should display applications', () => {
  74. renderProjectCard({ ...PROJECT, qualifier: ComponentQualifier.Application });
  75. expect(screen.getByLabelText('qualifier.APP')).toBeInTheDocument();
  76. });
  77. it('should not display awaiting analysis badge and do not display old measures', () => {
  78. renderProjectCard({
  79. ...PROJECT,
  80. measures: {
  81. ...MEASURES,
  82. [MetricKey.security_issues]: JSON.stringify({ LOW: 0, MEDIUM: 0, HIGH: 1, total: 1 }),
  83. [MetricKey.reliability_issues]: JSON.stringify({ LOW: 0, MEDIUM: 2, HIGH: 0, total: 2 }),
  84. [MetricKey.maintainability_issues]: JSON.stringify({ LOW: 3, MEDIUM: 0, HIGH: 0, total: 3 }),
  85. [MetricKey.code_smells]: '4',
  86. [MetricKey.bugs]: '5',
  87. [MetricKey.vulnerabilities]: '6',
  88. },
  89. });
  90. expect(screen.queryByRole('status', { name: 'projects.awaiting_scan' })).not.toBeInTheDocument();
  91. expect(screen.getByText('1')).toBeInTheDocument();
  92. expect(screen.getByText('2')).toBeInTheDocument();
  93. expect(screen.getByText('3')).toBeInTheDocument();
  94. expect(screen.queryByText('4')).not.toBeInTheDocument();
  95. expect(screen.queryByText('5')).not.toBeInTheDocument();
  96. expect(screen.queryByText('6')).not.toBeInTheDocument();
  97. });
  98. it('should display awaiting analysis badge and show the old measures', async () => {
  99. renderProjectCard({
  100. ...PROJECT,
  101. measures: {
  102. ...MEASURES,
  103. [MetricKey.code_smells]: '4',
  104. [MetricKey.bugs]: '5',
  105. [MetricKey.vulnerabilities]: '6',
  106. },
  107. });
  108. expect(screen.getByRole('status', { name: 'projects.awaiting_scan' })).toBeInTheDocument();
  109. await expect(
  110. screen.getByRole('status', { name: 'projects.awaiting_scan' }),
  111. ).toHaveATooltipWithContent('projects.awaiting_scan.description.TRK');
  112. expect(screen.getByText('4')).toBeInTheDocument();
  113. expect(screen.getByText('5')).toBeInTheDocument();
  114. expect(screen.getByText('6')).toBeInTheDocument();
  115. });
  116. it('should display awaiting analysis badge and show the old measures for Application', async () => {
  117. renderProjectCard({
  118. ...PROJECT,
  119. qualifier: ComponentQualifier.Application,
  120. measures: {
  121. ...MEASURES,
  122. [MetricKey.code_smells]: '4',
  123. [MetricKey.bugs]: '5',
  124. [MetricKey.vulnerabilities]: '6',
  125. },
  126. });
  127. expect(screen.getByRole('status', { name: 'projects.awaiting_scan' })).toBeInTheDocument();
  128. await expect(
  129. screen.getByRole('status', { name: 'projects.awaiting_scan' }),
  130. ).toHaveATooltipWithContent('projects.awaiting_scan.description.APP');
  131. expect(screen.getByText('4')).toBeInTheDocument();
  132. expect(screen.getByText('5')).toBeInTheDocument();
  133. expect(screen.getByText('6')).toBeInTheDocument();
  134. });
  135. it('should not display awaiting analysis badge if project is not analyzed', () => {
  136. renderProjectCard({
  137. ...PROJECT,
  138. analysisDate: undefined,
  139. });
  140. expect(screen.queryByRole('status', { name: 'projects.awaiting_scan' })).not.toBeInTheDocument();
  141. });
  142. it('should not display awaiting analysis badge if project does not have lines of code', () => {
  143. renderProjectCard({
  144. ...PROJECT,
  145. measures: {
  146. ...(({ [MetricKey.ncloc]: _, ...rest }) => rest)(MEASURES),
  147. },
  148. });
  149. expect(screen.queryByRole('status', { name: 'projects.awaiting_scan' })).not.toBeInTheDocument();
  150. });
  151. it('should not display awaiting analysis badge if it is a new code filter', () => {
  152. renderProjectCard(PROJECT, undefined, 'leak');
  153. expect(screen.queryByRole('status', { name: 'projects.awaiting_scan' })).not.toBeInTheDocument();
  154. });
  155. it('should display 3 aplication', () => {
  156. renderProjectCard({
  157. ...PROJECT,
  158. qualifier: ComponentQualifier.Application,
  159. measures: { ...MEASURES, projects: '3' },
  160. });
  161. expect(screen.getByText(/x_projects_.3/)).toBeInTheDocument();
  162. });
  163. function renderProjectCard(project: Project, user: CurrentUser = USER_LOGGED_OUT, type?: string) {
  164. renderComponent(
  165. <ProjectCard currentUser={user} handleFavorite={jest.fn()} project={project} type={type} />,
  166. );
  167. }