* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { screen } from '@testing-library/react';
+import { screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
import React from 'react';
import { ComponentQualifier, Visibility } from '~sonar-aligned/types/component';
import { MetricKey } from '~sonar-aligned/types/metrics';
import { MeasuresServiceMock } from '../../../../../api/mocks/MeasuresServiceMock';
import SettingsServiceMock from '../../../../../api/mocks/SettingsServiceMock';
-import { mockCurrentUser, mockLoggedInUser } from '../../../../../helpers/testMocks';
+import { mockComponent } from '../../../../../helpers/mocks/component';
+import { mockCurrentUser, mockLoggedInUser, mockMeasure } from '../../../../../helpers/testMocks';
import { renderComponent } from '../../../../../helpers/testReactTestingUtils';
import { CurrentUser } from '../../../../../types/users';
import { Project } from '../../../types';
[MetricKey.reliability_rating]: '1.0',
[MetricKey.security_rating]: '1.0',
[MetricKey.sqale_rating]: '1.0',
+ [MetricKey.security_review_rating]: '1.0',
[MetricKey.new_bugs]: '12',
};
expect(screen.getAllByText('qualifier.APP')).toHaveLength(2);
});
-it('should not display awaiting analysis badge and do not display old measures', () => {
+it('should display 3 projects', () => {
renderProjectCard({
...PROJECT,
- measures: {
- ...MEASURES,
- [MetricKey.security_issues]: JSON.stringify({ LOW: 0, MEDIUM: 0, HIGH: 1, total: 1 }),
- [MetricKey.reliability_issues]: JSON.stringify({ LOW: 0, MEDIUM: 2, HIGH: 0, total: 2 }),
- [MetricKey.maintainability_issues]: JSON.stringify({ LOW: 3, MEDIUM: 0, HIGH: 0, total: 3 }),
- [MetricKey.code_smells]: '4',
- [MetricKey.bugs]: '5',
- [MetricKey.vulnerabilities]: '6',
- },
+ qualifier: ComponentQualifier.Application,
+ measures: { ...MEASURES, projects: '3' },
});
- expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
- expect(screen.getByText('1')).toBeInTheDocument();
- expect(screen.getByText('2')).toBeInTheDocument();
- expect(screen.getByText('3')).toBeInTheDocument();
- expect(screen.queryByText('4')).not.toBeInTheDocument();
- expect(screen.queryByText('5')).not.toBeInTheDocument();
- expect(screen.queryByText('6')).not.toBeInTheDocument();
+ expect(screen.getByText(/x_projects_.3/)).toBeInTheDocument();
});
-it('should display awaiting analysis badge and show the old measures', async () => {
- renderProjectCard({
- ...PROJECT,
- measures: {
- ...MEASURES,
- [MetricKey.code_smells]: '4',
- [MetricKey.bugs]: '5',
- [MetricKey.vulnerabilities]: '6',
- },
+describe('upgrade scenario (awaiting scan)', () => {
+ const oldRatings = {
+ [MetricKey.reliability_rating]: mockMeasure({
+ metric: MetricKey.reliability_rating,
+ value: '1',
+ }),
+ [MetricKey.sqale_rating]: mockMeasure({
+ metric: MetricKey.sqale_rating,
+ value: '1',
+ }),
+ [MetricKey.security_rating]: mockMeasure({
+ metric: MetricKey.security_rating,
+ value: '1',
+ }),
+ [MetricKey.security_review_rating]: mockMeasure({
+ metric: MetricKey.security_review_rating,
+ value: '1',
+ }),
+ };
+
+ const newRatings = {
+ [MetricKey.reliability_rating_new]: mockMeasure({
+ metric: MetricKey.reliability_rating_new,
+ value: '2',
+ }),
+ [MetricKey.sqale_rating_new]: mockMeasure({
+ metric: MetricKey.sqale_rating_new,
+ value: '2',
+ }),
+ [MetricKey.security_rating_new]: mockMeasure({
+ metric: MetricKey.security_rating_new,
+ value: '2',
+ }),
+ [MetricKey.security_review_rating_new]: mockMeasure({
+ metric: MetricKey.security_review_rating_new,
+ value: '2',
+ }),
+ };
+ beforeEach(() => {
+ measuresHandler.setComponents({
+ component: mockComponent({ key: PROJECT.key }),
+ ancestors: [],
+ children: [],
+ });
+ measuresHandler.registerComponentMeasures({
+ [PROJECT.key]: oldRatings,
+ });
+ });
+ it('should not display awaiting analysis badge and do not display old measures', async () => {
+ measuresHandler.registerComponentMeasures({
+ [PROJECT.key]: newRatings,
+ });
+ renderProjectCard({
+ ...PROJECT,
+ measures: {
+ ...MEASURES,
+ [MetricKey.security_issues]: JSON.stringify({ LOW: 0, MEDIUM: 0, HIGH: 1, total: 1 }),
+ [MetricKey.reliability_issues]: JSON.stringify({ LOW: 0, MEDIUM: 2, HIGH: 0, total: 2 }),
+ [MetricKey.maintainability_issues]: JSON.stringify({
+ LOW: 3,
+ MEDIUM: 0,
+ HIGH: 0,
+ total: 3,
+ }),
+ [MetricKey.sqale_rating_new]: '2',
+ [MetricKey.reliability_rating_new]: '2',
+ [MetricKey.security_rating_new]: '2',
+ [MetricKey.security_review_rating_new]: '2',
+ [MetricKey.code_smells]: '4',
+ [MetricKey.bugs]: '5',
+ [MetricKey.vulnerabilities]: '6',
+ },
+ });
+ expect(screen.getByText('1')).toBeInTheDocument();
+ expect(screen.getByText('2')).toBeInTheDocument();
+ expect(screen.getByText('3')).toBeInTheDocument();
+ await waitFor(() => expect(screen.getAllByText('B')).toHaveLength(4));
+ expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
+ expect(screen.queryByText('4')).not.toBeInTheDocument();
+ expect(screen.queryByText('5')).not.toBeInTheDocument();
+ expect(screen.queryByText('6')).not.toBeInTheDocument();
+ expect(screen.queryByText('A')).not.toBeInTheDocument();
});
- expect(screen.getByText('projects.awaiting_scan')).toBeInTheDocument();
- await expect(screen.getByText('projects.awaiting_scan')).toHaveATooltipWithContent(
- 'projects.awaiting_scan.description.TRK',
- );
- expect(screen.getByText('4')).toBeInTheDocument();
- expect(screen.getByText('5')).toBeInTheDocument();
- expect(screen.getByText('6')).toBeInTheDocument();
-});
-it('should display awaiting analysis badge and show the old measures for Application', async () => {
- renderProjectCard({
- ...PROJECT,
- qualifier: ComponentQualifier.Application,
- measures: {
- ...MEASURES,
- [MetricKey.code_smells]: '4',
- [MetricKey.bugs]: '5',
- [MetricKey.vulnerabilities]: '6',
- },
+ it('should display awaiting analysis badge and show the old measures', async () => {
+ const user = userEvent.setup();
+ renderProjectCard({
+ ...PROJECT,
+ measures: {
+ ...MEASURES,
+ [MetricKey.code_smells]: '4',
+ [MetricKey.bugs]: '5',
+ [MetricKey.vulnerabilities]: '6',
+ },
+ });
+ expect(await screen.findByText('projects.awaiting_scan')).toBeInTheDocument();
+ await user.click(screen.getByText('projects.awaiting_scan'));
+ await expect(screen.getByText('projects.awaiting_scan.description.TRK')).toBeInTheDocument();
+ expect(screen.getByText('4')).toBeInTheDocument();
+ expect(screen.getByText('5')).toBeInTheDocument();
+ expect(screen.getByText('6')).toBeInTheDocument();
+ expect(screen.getAllByText('A')).toHaveLength(4);
});
- expect(screen.getByText('projects.awaiting_scan')).toBeInTheDocument();
- await expect(screen.getByText('projects.awaiting_scan')).toHaveATooltipWithContent(
- 'projects.awaiting_scan.description.APP',
- );
- expect(screen.getByText('4')).toBeInTheDocument();
- expect(screen.getByText('5')).toBeInTheDocument();
- expect(screen.getByText('6')).toBeInTheDocument();
-});
-it('should not display awaiting analysis badge if project is not analyzed', () => {
- renderProjectCard({
- ...PROJECT,
- analysisDate: undefined,
+ it('should display awaiting analysis badge, show new software qualities, but old ratings', async () => {
+ renderProjectCard({
+ ...PROJECT,
+ measures: {
+ ...MEASURES,
+ [MetricKey.security_issues]: JSON.stringify({ LOW: 0, MEDIUM: 0, HIGH: 1, total: 1 }),
+ [MetricKey.reliability_issues]: JSON.stringify({ LOW: 0, MEDIUM: 2, HIGH: 0, total: 2 }),
+ [MetricKey.maintainability_issues]: JSON.stringify({
+ LOW: 3,
+ MEDIUM: 0,
+ HIGH: 0,
+ total: 3,
+ }),
+ [MetricKey.code_smells]: '4',
+ [MetricKey.bugs]: '5',
+ [MetricKey.vulnerabilities]: '6',
+ },
+ });
+ expect(await screen.findByText('projects.awaiting_scan')).toBeInTheDocument();
+ expect(screen.getByText('1')).toBeInTheDocument();
+ expect(screen.getByText('2')).toBeInTheDocument();
+ expect(screen.getByText('3')).toBeInTheDocument();
+ expect(screen.queryByText('4')).not.toBeInTheDocument();
+ expect(screen.queryByText('5')).not.toBeInTheDocument();
+ expect(screen.queryByText('6')).not.toBeInTheDocument();
+ await waitFor(() => expect(screen.getAllByText('A')).toHaveLength(4));
});
- expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
-});
-it('should not display awaiting analysis badge if project does not have lines of code', () => {
- renderProjectCard({
- ...PROJECT,
- measures: {
- ...(({ [MetricKey.ncloc]: _, ...rest }) => rest)(MEASURES),
- },
+ it('should display awaiting analysis badge and show the old measures for Application', async () => {
+ const user = userEvent.setup();
+ renderProjectCard({
+ ...PROJECT,
+ qualifier: ComponentQualifier.Application,
+ measures: {
+ ...MEASURES,
+ [MetricKey.code_smells]: '4',
+ [MetricKey.bugs]: '5',
+ [MetricKey.vulnerabilities]: '6',
+ },
+ });
+ expect(await screen.findByText('projects.awaiting_scan')).toBeInTheDocument();
+ await user.click(screen.getByText('projects.awaiting_scan'));
+ await expect(screen.getByText('projects.awaiting_scan.description.APP')).toBeInTheDocument();
+ expect(screen.getByText('4')).toBeInTheDocument();
+ expect(screen.getByText('5')).toBeInTheDocument();
+ expect(screen.getByText('6')).toBeInTheDocument();
});
- expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
-});
-it('should not display awaiting analysis badge if it is a new code filter', () => {
- renderProjectCard(PROJECT, undefined, 'leak');
- expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
-});
+ it('should not display awaiting analysis badge if project is not analyzed', () => {
+ renderProjectCard({
+ ...PROJECT,
+ analysisDate: undefined,
+ });
+ expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
+ });
-it('should display 3 aplication', () => {
- renderProjectCard({
- ...PROJECT,
- qualifier: ComponentQualifier.Application,
- measures: { ...MEASURES, projects: '3' },
+ it('should not display awaiting analysis badge if project does not have lines of code', () => {
+ renderProjectCard({
+ ...PROJECT,
+ measures: {
+ ...(({ [MetricKey.ncloc]: _, ...rest }) => rest)(MEASURES),
+ },
+ });
+ expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
+ });
+
+ it('should not display awaiting analysis badge if it is a new code filter', () => {
+ renderProjectCard(PROJECT, undefined, 'leak');
+ expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
+ });
+
+ it('should not display awaiting analysis badge if legacy mode is enabled', async () => {
+ settingsHandler.set('sonar.legacy.ratings.mode.enabled', 'true');
+ renderProjectCard({
+ ...PROJECT,
+ measures: {
+ ...MEASURES,
+ [MetricKey.code_smells]: '4',
+ [MetricKey.bugs]: '5',
+ [MetricKey.vulnerabilities]: '6',
+ },
+ });
+ expect(screen.getByText('4')).toBeInTheDocument();
+ expect(screen.getByText('5')).toBeInTheDocument();
+ expect(screen.getByText('6')).toBeInTheDocument();
+ await waitFor(() => expect(screen.getAllByText('A')).toHaveLength(4));
+ expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
+ });
+
+ it('should not display new values if legacy mode is enabled', async () => {
+ settingsHandler.set('sonar.legacy.ratings.mode.enabled', 'true');
+ measuresHandler.registerComponentMeasures({
+ [PROJECT.key]: {
+ ...newRatings,
+ ...oldRatings,
+ },
+ });
+ renderProjectCard({
+ ...PROJECT,
+ measures: {
+ ...MEASURES,
+ [MetricKey.security_issues]: JSON.stringify({ LOW: 0, MEDIUM: 0, HIGH: 1, total: 1 }),
+ [MetricKey.reliability_issues]: JSON.stringify({ LOW: 0, MEDIUM: 2, HIGH: 0, total: 2 }),
+ [MetricKey.maintainability_issues]: JSON.stringify({
+ LOW: 3,
+ MEDIUM: 0,
+ HIGH: 0,
+ total: 3,
+ }),
+ [MetricKey.sqale_rating_new]: '2',
+ [MetricKey.reliability_rating_new]: '2',
+ [MetricKey.security_rating_new]: '2',
+ [MetricKey.security_review_rating_new]: '2',
+ [MetricKey.code_smells]: '4',
+ [MetricKey.bugs]: '5',
+ [MetricKey.vulnerabilities]: '6',
+ },
+ });
+ expect(await screen.findByText('4')).toBeInTheDocument();
+ expect(screen.getByText('5')).toBeInTheDocument();
+ expect(screen.getByText('6')).toBeInTheDocument();
+ expect(screen.queryByText('1')).not.toBeInTheDocument();
+ expect(screen.queryByText('2')).not.toBeInTheDocument();
+ expect(screen.queryByText('3')).not.toBeInTheDocument();
+ await waitFor(() => expect(screen.getAllByText('A')).toHaveLength(4));
+ expect(screen.queryByText('B')).not.toBeInTheDocument();
+ expect(screen.queryByText('projects.awaiting_scan')).not.toBeInTheDocument();
});
- expect(screen.getByText(/x_projects_.3/)).toBeInTheDocument();
});
function renderProjectCard(project: Project, user: CurrentUser = USER_LOGGED_OUT, type?: string) {