From cb3209eefa3f943524effb7b0d4f03c4bf9a45f6 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Wed, 6 Nov 2019 16:19:42 +0900 Subject: [PATCH] SONAR-12585 Fix missing releasability badges in portfolio project list --- .../__snapshots__/utils-test.tsx.snap | 61 +++++++++++++ .../js/apps/code/__tests__/utils-test.tsx | 88 +++++++++++++++++++ .../sonar-web/src/main/js/apps/code/utils.ts | 16 ++-- 3 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 server/sonar-web/src/main/js/apps/code/__tests__/__snapshots__/utils-test.tsx.snap create mode 100644 server/sonar-web/src/main/js/apps/code/__tests__/utils-test.tsx diff --git a/server/sonar-web/src/main/js/apps/code/__tests__/__snapshots__/utils-test.tsx.snap b/server/sonar-web/src/main/js/apps/code/__tests__/__snapshots__/utils-test.tsx.snap new file mode 100644 index 00000000000..208dae9fd4a --- /dev/null +++ b/server/sonar-web/src/main/js/apps/code/__tests__/__snapshots__/utils-test.tsx.snap @@ -0,0 +1,61 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`getCodeMetrics should return the right metrics for apps 1`] = ` +Array [ + "alert_status", + "ncloc", + "bugs", + "vulnerabilities", + "code_smells", + "security_hotspots", + "coverage", + "duplicated_lines_density", +] +`; + +exports[`getCodeMetrics should return the right metrics for portfolios 1`] = ` +Array [ + "releasability_rating", + "reliability_rating", + "security_rating", + "security_review_rating", + "sqale_rating", + "ncloc", +] +`; + +exports[`getCodeMetrics should return the right metrics for portfolios 2`] = ` +Array [ + "releasability_rating", + "reliability_rating", + "security_rating", + "security_review_rating", + "sqale_rating", + "ncloc", + "alert_status", +] +`; + +exports[`getCodeMetrics should return the right metrics for projects 1`] = ` +Array [ + "ncloc", + "bugs", + "vulnerabilities", + "code_smells", + "security_hotspots", + "coverage", + "duplicated_lines_density", +] +`; + +exports[`getCodeMetrics should return the right metrics for projects 2`] = ` +Array [ + "new_lines", + "bugs", + "vulnerabilities", + "code_smells", + "security_hotspots", + "new_coverage", + "new_duplicated_lines_density", +] +`; diff --git a/server/sonar-web/src/main/js/apps/code/__tests__/utils-test.tsx b/server/sonar-web/src/main/js/apps/code/__tests__/utils-test.tsx new file mode 100644 index 00000000000..84a22b4f01b --- /dev/null +++ b/server/sonar-web/src/main/js/apps/code/__tests__/utils-test.tsx @@ -0,0 +1,88 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { getChildren } from '../../../api/components'; +import { mockMainBranch, mockPullRequest } from '../../../helpers/testMocks'; +import { addComponent, addComponentChildren, getComponentBreadcrumbs } from '../bucket'; +import { getCodeMetrics, loadMoreChildren, retrieveComponentChildren } from '../utils'; + +jest.mock('../../../api/components', () => ({ + getChildren: jest.fn().mockRejectedValue({}) +})); + +jest.mock('../bucket', () => ({ + addComponent: jest.fn(), + addComponentBreadcrumbs: jest.fn(), + addComponentChildren: jest.fn(), + getComponent: jest.fn(), + getComponentBreadcrumbs: jest.fn(), + getComponentChildren: jest.fn() +})); + +beforeEach(() => { + jest.clearAllMocks(); +}); + +describe('getCodeMetrics', () => { + it('should return the right metrics for portfolios', () => { + expect(getCodeMetrics('VW')).toMatchSnapshot(); + expect(getCodeMetrics('VW', undefined, { includeQGStatus: true })).toMatchSnapshot(); + }); + + it('should return the right metrics for apps', () => { + expect(getCodeMetrics('APP')).toMatchSnapshot(); + }); + + it('should return the right metrics for projects', () => { + expect(getCodeMetrics('TRK', mockMainBranch())).toMatchSnapshot(); + expect(getCodeMetrics('TRK', mockPullRequest())).toMatchSnapshot(); + }); +}); + +describe('retrieveComponentChildren', () => { + it('should retrieve children correctly', async () => { + const components = [{}, {}]; + (getChildren as jest.Mock).mockResolvedValueOnce({ + components, + paging: { total: 2, pageIndex: 0 } + }); + + await retrieveComponentChildren('key', 'TRK', mockMainBranch()); + + expect(addComponentChildren).toHaveBeenCalledWith('key', components, 2, 0); + expect(addComponent).toHaveBeenCalledTimes(2); + expect(getComponentBreadcrumbs).toHaveBeenCalledWith('key'); + }); +}); + +describe('loadMoreChildren', () => { + it('should load more children', async () => { + const components = [{}, {}, {}]; + (getChildren as jest.Mock).mockResolvedValueOnce({ + components, + paging: { total: 6, pageIndex: 1 } + }); + + await loadMoreChildren('key', 1, 'TRK', mockMainBranch()); + + expect(addComponentChildren).toHaveBeenCalledWith('key', components, 6, 1); + expect(addComponent).toHaveBeenCalledTimes(3); + expect(getComponentBreadcrumbs).toHaveBeenCalledWith('key'); + }); +}); diff --git a/server/sonar-web/src/main/js/apps/code/utils.ts b/server/sonar-web/src/main/js/apps/code/utils.ts index fe238eae14a..2e05df99a95 100644 --- a/server/sonar-web/src/main/js/apps/code/utils.ts +++ b/server/sonar-web/src/main/js/apps/code/utils.ts @@ -99,9 +99,14 @@ function storeChildrenBreadcrumbs(parentComponentKey: string, children: T.Breadc } } -export function getCodeMetrics(qualifier: string, branchLike?: T.BranchLike) { +export function getCodeMetrics( + qualifier: string, + branchLike?: T.BranchLike, + options: { includeQGStatus?: boolean } = {} +) { if (['VW', 'SVW'].includes(qualifier)) { - return [...PORTFOLIO_METRICS]; + const metrics = [...PORTFOLIO_METRICS]; + return options.includeQGStatus ? metrics.concat('alert_status') : metrics; } if (qualifier === 'APP') { return [...APPLICATION_METRICS]; @@ -144,10 +149,7 @@ export function retrieveComponentChildren( }); } - const metrics = getCodeMetrics(qualifier, branchLike); - if (['VW', 'SVW'].includes(qualifier)) { - metrics.push('alert_status'); - } + const metrics = getCodeMetrics(qualifier, branchLike, { includeQGStatus: true }); return getChildren(componentKey, metrics, { ps: PAGE_SIZE, @@ -212,7 +214,7 @@ export function loadMoreChildren( qualifier: string, branchLike?: T.BranchLike ): Promise { - const metrics = getCodeMetrics(qualifier, branchLike); + const metrics = getCodeMetrics(qualifier, branchLike, { includeQGStatus: true }); return getChildren(componentKey, metrics, { ps: PAGE_SIZE, -- 2.39.5