--- /dev/null
+// 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",
+]
+`;
--- /dev/null
+/*
+ * 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');
+ });
+});
}
}
-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];
});
}
- 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,
qualifier: string,
branchLike?: T.BranchLike
): Promise<Children> {
- const metrics = getCodeMetrics(qualifier, branchLike);
+ const metrics = getCodeMetrics(qualifier, branchLike, { includeQGStatus: true });
return getChildren(componentKey, metrics, {
ps: PAGE_SIZE,