From f7c1c7a08a7fecdfb339493830ecd122bfd481ab Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Wed, 16 Dec 2015 15:23:40 +0100 Subject: [PATCH] SONAR-7143 display different types of coverage --- .../src/main/js/apps/code/actions/index.js | 24 ++++--- .../src/main/js/apps/code/components/Code.js | 3 +- .../main/js/apps/code/components/Component.js | 4 +- .../js/apps/code/components/Components.js | 7 +- .../src/main/js/apps/code/reducers/index.js | 29 +++++++- .../tests/apps/code/components-test.js | 1 + .../sonar-web/tests/apps/code/store-test.js | 70 ++++++++++++++++++- 7 files changed, 123 insertions(+), 15 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/code/actions/index.js b/server/sonar-web/src/main/js/apps/code/actions/index.js index 9e9df67cfea..135c77758ab 100644 --- a/server/sonar-web/src/main/js/apps/code/actions/index.js +++ b/server/sonar-web/src/main/js/apps/code/actions/index.js @@ -7,10 +7,14 @@ const METRICS = [ 'ncloc', 'sqale_index', 'violations', - // TODO handle other types of coverage - 'coverage', 'duplicated_lines_density' ]; +const METRICS_WITH_COVERAGE = [ + ...METRICS, + 'coverage', + 'it_coverage', + 'overall_coverage' +]; export const INIT = 'INIT'; @@ -44,18 +48,22 @@ export function showSource (component) { } -function fetchChildren (dispatch, baseComponent) { +function fetchChildren (dispatch, getState, baseComponent) { dispatch(requestComponents(baseComponent)); - return getChildren(baseComponent.key, METRICS) + + const { coverageMetric } = getState(); + const metrics = [...METRICS, coverageMetric]; + + return getChildren(baseComponent.key, metrics) .then(components => _.sortBy(components, 'name')) .then(components => dispatch(receiveComponents(baseComponent, components))); } export function initComponent (baseComponent) { - return dispatch => { - return getComponent(baseComponent.key, METRICS) - .then(component => fetchChildren(dispatch, component)); + return (dispatch, getState) => { + return getComponent(baseComponent.key, METRICS_WITH_COVERAGE) + .then(component => fetchChildren(dispatch, getState, component)); }; } @@ -64,7 +72,7 @@ export function fetchComponents (baseComponent) { return (dispatch, getState) => { const { fetching } = getState(); if (!fetching) { - return fetchChildren(dispatch, baseComponent); + return fetchChildren(dispatch, getState, baseComponent); } }; } diff --git a/server/sonar-web/src/main/js/apps/code/components/Code.js b/server/sonar-web/src/main/js/apps/code/components/Code.js index d894ab8c952..4c5b2785033 100644 --- a/server/sonar-web/src/main/js/apps/code/components/Code.js +++ b/server/sonar-web/src/main/js/apps/code/components/Code.js @@ -36,7 +36,7 @@ class Code extends Component { } render () { - const { fetching, baseComponent, components, breadcrumbs, sourceViewer } = this.props; + const { fetching, baseComponent, components, breadcrumbs, sourceViewer, coverageMetric } = this.props; const shouldShowBreadcrumbs = Array.isArray(breadcrumbs) && breadcrumbs.length > 1; const shouldShowComponents = !sourceViewer && components; const shouldShowSourceViewer = sourceViewer; @@ -65,6 +65,7 @@ class Code extends Component { )} diff --git a/server/sonar-web/src/main/js/apps/code/components/Component.js b/server/sonar-web/src/main/js/apps/code/components/Component.js index e319d9ef826..00a752cb591 100644 --- a/server/sonar-web/src/main/js/apps/code/components/Component.js +++ b/server/sonar-web/src/main/js/apps/code/components/Component.js @@ -5,7 +5,7 @@ import ComponentMeasure from './ComponentMeasure'; import ComponentDetach from './ComponentDetach'; -const Component = ({ component, onBrowse }) => ( +const Component = ({ component, coverageMetric, onBrowse }) => ( @@ -45,7 +45,7 @@ const Component = ({ component, onBrowse }) => (
diff --git a/server/sonar-web/src/main/js/apps/code/components/Components.js b/server/sonar-web/src/main/js/apps/code/components/Components.js index 1167e319296..79a83c3e51a 100644 --- a/server/sonar-web/src/main/js/apps/code/components/Components.js +++ b/server/sonar-web/src/main/js/apps/code/components/Components.js @@ -4,7 +4,7 @@ import Component from './Component'; import ComponentsEmpty from './ComponentsEmpty'; -const Components = ({ baseComponent, components, onBrowse }) => ( +const Components = ({ baseComponent, components, coverageMetric, onBrowse }) => ( @@ -18,7 +18,9 @@ const Components = ({ baseComponent, components, onBrowse }) => ( - + @@ -29,6 +31,7 @@ const Components = ({ baseComponent, components, onBrowse }) => ( )) ) : ( diff --git a/server/sonar-web/src/main/js/apps/code/reducers/index.js b/server/sonar-web/src/main/js/apps/code/reducers/index.js index d75ca950409..3a86e706e8d 100644 --- a/server/sonar-web/src/main/js/apps/code/reducers/index.js +++ b/server/sonar-web/src/main/js/apps/code/reducers/index.js @@ -1,3 +1,4 @@ +import _ from 'underscore'; import { combineReducers } from 'redux'; import { BROWSE, RECEIVE_COMPONENTS, SHOW_SOURCE } from '../actions'; @@ -70,12 +71,38 @@ export function sourceViewer (state = null, action) { } +function selectCoverageMetric (component) { + const coverage = _.findWhere(component.msr, { key: 'coverage' }); + const itCoverage = _.findWhere(component.msr, { key: 'it_coverage' }); + const overallCoverage = _.findWhere(component.msr, { key: 'overall_coverage' }); + + if (coverage != null && itCoverage != null && overallCoverage != null) { + return 'overall_coverage'; + } else if (coverage != null) { + return 'coverage'; + } else { + return 'it_coverage'; + } +} + + +export function coverageMetric (state = null, action) { + switch (action.type) { + case BROWSE: + return state !== null ? state : selectCoverageMetric(action.baseComponent); + default: + return state; + } +} + + const rootReducer = combineReducers({ fetching, baseComponent, components, breadcrumbs, - sourceViewer + sourceViewer, + coverageMetric }); diff --git a/server/sonar-web/tests/apps/code/components-test.js b/server/sonar-web/tests/apps/code/components-test.js index 92ed3427f75..2469bf26870 100644 --- a/server/sonar-web/tests/apps/code/components-test.js +++ b/server/sonar-web/tests/apps/code/components-test.js @@ -93,6 +93,7 @@ describe('Code :: Components', () => { output = shallow( ); }); diff --git a/server/sonar-web/tests/apps/code/store-test.js b/server/sonar-web/tests/apps/code/store-test.js index 039e199a19b..cf8532fe755 100644 --- a/server/sonar-web/tests/apps/code/store-test.js +++ b/server/sonar-web/tests/apps/code/store-test.js @@ -5,7 +5,8 @@ import { baseComponent, components, breadcrumbs, - sourceViewer + sourceViewer, + coverageMetric } from '../../../src/main/js/apps/code/reducers'; import { requestComponents, @@ -93,6 +94,73 @@ describe('Code :: Store', () => { expect(sourceViewer(exampleComponent, requestComponents({}))) .to.equal(null); }); + + describe('coverageMetric', () => { + it('should be initially null', () => { + expect(coverageMetric(undefined, {})) + .to.equal(null); + }); + + it('should be set to "coverage"', () => { + const componentWithCoverage = { + ...exampleComponent, + msr: [ + { key: 'coverage', val: 13 } + ] + }; + + expect(coverageMetric(null, requestComponents(componentWithCoverage))) + .to.equal('coverage'); + }); + + it('should be set to "it_coverage"', () => { + const componentWithCoverage = { + ...exampleComponent, + msr: [ + { key: 'it_coverage', val: 13 } + ] + }; + + expect(coverageMetric(null, requestComponents(componentWithCoverage))) + .to.equal('it_coverage'); + }); + + it('should be set to "overall_coverage"', () => { + const componentWithCoverage = { + ...exampleComponent, + msr: [ + { key: 'coverage', val: 11 }, + { key: 'it_coverage', val: 12 }, + { key: 'overall_coverage', val: 13 } + ] + }; + + expect(coverageMetric(null, requestComponents(componentWithCoverage))) + .to.equal('overall_coverage'); + }); + + it('should fallback to "it_coverage"', () => { + const componentWithCoverage = { + ...exampleComponent, + msr: [] + }; + + expect(coverageMetric(null, requestComponents(componentWithCoverage))) + .to.equal('it_coverage'); + }); + + it('should not be reset after set once', () => { + const componentWithCoverage = { + ...exampleComponent, + msr: [ + { key: 'coverage', val: 13 } + ] + }; + + expect(coverageMetric('overall_coverage', requestComponents(componentWithCoverage))) + .to.equal('overall_coverage'); + }); + }); }); }); }); -- 2.39.5