]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7143 display different types of coverage
authorStas Vilchik <vilchiks@gmail.com>
Wed, 16 Dec 2015 14:23:40 +0000 (15:23 +0100)
committerStas Vilchik <vilchiks@gmail.com>
Wed, 16 Dec 2015 14:23:40 +0000 (15:23 +0100)
server/sonar-web/src/main/js/apps/code/actions/index.js
server/sonar-web/src/main/js/apps/code/components/Code.js
server/sonar-web/src/main/js/apps/code/components/Component.js
server/sonar-web/src/main/js/apps/code/components/Components.js
server/sonar-web/src/main/js/apps/code/reducers/index.js
server/sonar-web/tests/apps/code/components-test.js
server/sonar-web/tests/apps/code/store-test.js

index 9e9df67cfea83fdb275fb3699ad390d55d1a17ba..135c77758ab48517df13e890daf31eed1d0b16d5 100644 (file)
@@ -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);
     }
   };
 }
index d894ab8c95286dae8c41762d92af687660013e96..4c5b278503354bf2d57c0a10286362fa25edd027 100644 (file)
@@ -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 {
                   <Components
                       baseComponent={baseComponent}
                       components={components}
+                      coverageMetric={coverageMetric}
                       onBrowse={this.handleBrowse.bind(this)}/>
                 </div>
             )}
index e319d9ef826540ddfc994056110952dade83feb6..00a752cb591a41ba7ec324ba74fd474e5d0d0372 100644 (file)
@@ -5,7 +5,7 @@ import ComponentMeasure from './ComponentMeasure';
 import ComponentDetach from './ComponentDetach';
 
 
-const Component = ({ component, onBrowse }) => (
+const Component = ({ component, coverageMetric, onBrowse }) => (
     <tr>
       <td className="thin nowrap">
         <span className="spacer-right">
@@ -45,7 +45,7 @@ const Component = ({ component, onBrowse }) => (
         <div className="code-components-cell">
           <ComponentMeasure
               component={component}
-              metricKey="coverage"
+              metricKey={coverageMetric}
               metricType="PERCENT"/>
         </div>
       </td>
index 1167e319296eb001ec599296a470446728a142af..79a83c3e51acd03250bc894ce1adfabc0ac9b512 100644 (file)
@@ -4,7 +4,7 @@ import Component from './Component';
 import ComponentsEmpty from './ComponentsEmpty';
 
 
-const Components = ({ baseComponent, components, onBrowse }) => (
+const Components = ({ baseComponent, components, coverageMetric, onBrowse }) => (
     <table className="data zebra">
       <thead>
         <tr>
@@ -18,7 +18,9 @@ const Components = ({ baseComponent, components, onBrowse }) => (
         </tr>
       </thead>
       <tbody>
-        <Component component={baseComponent}/>
+        <Component
+            component={baseComponent}
+            coverageMetric={coverageMetric}/>
         <tr className="blank">
           <td colSpan="7">&nbsp;</td>
         </tr>
@@ -29,6 +31,7 @@ const Components = ({ baseComponent, components, onBrowse }) => (
                 <Component
                     key={component.key}
                     component={component}
+                    coverageMetric={coverageMetric}
                     onBrowse={onBrowse}/>
             ))
         ) : (
index d75ca95040925d420f89cbddd1d6d7af1ae3c55b..3a86e706e8dd8b1c3d094b16e02c7a7030f27a3d 100644 (file)
@@ -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
 });
 
 
index 92ed3427f75aea994edde3ef755639035269fb4b..2469bf268702353d3cd1937670b42c06af8e95bf 100644 (file)
@@ -93,6 +93,7 @@ describe('Code :: Components', () => {
       output = shallow(
           <Component
               component={exampleComponent}
+              coverageMetric="coverage"
               onBrowse={exampleOnBrowse}/>);
     });
 
index 039e199a19be1b4fa3fc20dc43fac726d5a77f5a..cf8532fe75526f88af49f8e9a9df54a809b29705 100644 (file)
@@ -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');
+        });
+      });
     });
   });
 });