3 * Copyright (C) 2009-2024 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.
20 import { screen } from '@testing-library/react';
21 import * as React from 'react';
23 import { ComponentQualifier } from '~sonar-aligned/types/component';
24 import { MetricKey } from '~sonar-aligned/types/metrics';
25 import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
26 import { Dict } from '../../../../../types/types';
27 import ProjectCardMeasures, { ProjectCardMeasuresProps } from '../ProjectCardMeasures';
29 jest.mock('date-fns', () => ({
30 ...jest.requireActual('date-fns'),
31 differenceInMilliseconds: () => 1000 * 60 * 60 * 24 * 30 * 8, // ~ 8 months
34 describe('Overall measures', () => {
35 it('should be rendered properly', () => {
36 renderProjectCardMeasures();
37 expect(screen.getByTitle('metric.security_issues.short_name')).toBeInTheDocument();
40 it("should be not be rendered if there's no line of code", () => {
41 renderProjectCardMeasures({ [MetricKey.ncloc]: undefined });
42 expect(screen.getByText('overview.project.main_branch_empty')).toBeInTheDocument();
45 it("should be not be rendered if there's no line of code and application", () => {
46 renderProjectCardMeasures(
47 { [MetricKey.ncloc]: undefined },
48 { componentQualifier: ComponentQualifier.Application },
50 expect(screen.getByText('portfolio.app.empty')).toBeInTheDocument();
54 describe('New code measures', () => {
55 it('should be rendered properly', () => {
56 renderProjectCardMeasures({}, { isNewCode: true });
57 expect(screen.getByLabelText(MetricKey.new_security_hotspots_reviewed)).toBeInTheDocument();
58 expect(screen.getByTitle('metric.new_violations.description')).toBeInTheDocument();
62 function renderProjectCardMeasures(
63 measuresOverride: Dict<string | undefined> = {},
64 props: Partial<ProjectCardMeasuresProps> = {},
67 [MetricKey.alert_status]: 'ERROR',
68 [MetricKey.bugs]: '17',
69 [MetricKey.code_smells]: '132',
70 [MetricKey.coverage]: '88.3',
71 [MetricKey.duplicated_lines_density]: '9.8',
72 [MetricKey.maintainability_issues]: JSON.stringify({ total: 10 }),
73 [MetricKey.reliability_issues]: JSON.stringify({ total: 10 }),
74 [MetricKey.security_issues]: JSON.stringify({ total: 10 }),
75 [MetricKey.ncloc]: '2053',
76 [MetricKey.reliability_rating]: '1.0',
77 [MetricKey.security_rating]: '1.0',
78 [MetricKey.sqale_rating]: '1.0',
79 [MetricKey.vulnerabilities]: '0',
80 [MetricKey.new_reliability_rating]: '1.0',
81 [MetricKey.new_bugs]: '8',
82 [MetricKey.new_security_rating]: '2.0',
83 [MetricKey.new_vulnerabilities]: '2',
84 [MetricKey.new_maintainability_rating]: '1.0',
85 [MetricKey.new_code_smells]: '0',
86 [MetricKey.new_coverage]: '26.55',
87 [MetricKey.new_duplicated_lines_density]: '0.55',
88 [MetricKey.new_violations]: '10',
89 [MetricKey.new_lines]: '87',
96 componentQualifier={ComponentQualifier.Project}