aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStas Vilchik <stas.vilchik@sonarsource.com>2018-08-23 10:53:03 +0200
committerSonarTech <sonartech@sonarsource.com>2018-08-23 20:21:35 +0200
commit9a69333f1cad5a6b8a5e64744dcd25352f25737c (patch)
tree1d7407530546ceb515df9a7922bfc424f94110d5
parent53dd61f4cf58190b0a0182def92c7ef5c692d633 (diff)
downloadsonarqube-9a69333f1cad5a6b8a5e64744dcd25352f25737c.tar.gz
sonarqube-9a69333f1cad5a6b8a5e64744dcd25352f25737c.zip
change ProjectCardLeak-test to not depend on current time
-rw-r--r--server/sonar-web/src/main/js/apps/projects/components/ProjectCardLeak.tsx8
-rw-r--r--server/sonar-web/src/main/js/apps/projects/components/__tests__/ProjectCardLeak-test.tsx6
-rw-r--r--server/sonar-web/src/main/js/apps/projects/components/__tests__/__snapshots__/ProjectCardLeak-test.tsx.snap2
-rw-r--r--server/sonar-web/src/main/js/apps/projects/utils.ts22
4 files changed, 22 insertions, 16 deletions
diff --git a/server/sonar-web/src/main/js/apps/projects/components/ProjectCardLeak.tsx b/server/sonar-web/src/main/js/apps/projects/components/ProjectCardLeak.tsx
index bfade459220..fe1452872f4 100644
--- a/server/sonar-web/src/main/js/apps/projects/components/ProjectCardLeak.tsx
+++ b/server/sonar-web/src/main/js/apps/projects/components/ProjectCardLeak.tsx
@@ -19,6 +19,7 @@
*/
import * as React from 'react';
import { Link } from 'react-router';
+import * as difference from 'date-fns/difference_in_milliseconds';
import ProjectCardQualityGate from './ProjectCardQualityGate';
import ProjectCardLeakMeasures from './ProjectCardLeakMeasures';
import ProjectCardOrganizationContainer from './ProjectCardOrganizationContainer';
@@ -41,9 +42,8 @@ interface Props {
export default function ProjectCardLeak({ height, organization, project }: Props) {
const { measures } = project;
const hasTags = project.tags.length > 0;
- const period = project.leakPeriodDate
- ? new Date().getTime() - new Date(project.leakPeriodDate).getTime()
- : 0;
+ const periodMs = project.leakPeriodDate ? difference(Date.now(), project.leakPeriodDate) : 0;
+
return (
<div className="boxed-group project-card" data-key={project.key} style={{ height }}>
<div className="boxed-group-header clearfix">
@@ -79,7 +79,7 @@ export default function ProjectCardLeak({ height, organization, project }: Props
project.leakPeriodDate && (
<div className="project-card-dates note text-right pull-right">
<span className="project-card-leak-date pull-right">
- {translateWithParameters('projects.new_code_period_x', formatDuration(period))}
+ {translateWithParameters('projects.new_code_period_x', formatDuration(periodMs))}
</span>
<DateTimeFormatter date={project.analysisDate}>
{formattedDate => (
diff --git a/server/sonar-web/src/main/js/apps/projects/components/__tests__/ProjectCardLeak-test.tsx b/server/sonar-web/src/main/js/apps/projects/components/__tests__/ProjectCardLeak-test.tsx
index b83840aedde..67d4b26c17a 100644
--- a/server/sonar-web/src/main/js/apps/projects/components/__tests__/ProjectCardLeak-test.tsx
+++ b/server/sonar-web/src/main/js/apps/projects/components/__tests__/ProjectCardLeak-test.tsx
@@ -22,6 +22,12 @@ import { shallow } from 'enzyme';
import ProjectCardLeak from '../ProjectCardLeak';
import { Visibility } from '../../../../app/types';
+jest.mock(
+ 'date-fns/difference_in_milliseconds',
+ () => () => 1000 * 60 * 60 * 24 * 30 * 8 // ~ 8 months
+);
+
+/* eslint-disable camelcase */
const MEASURES = {
alert_status: 'OK',
reliability_rating: '1.0',
diff --git a/server/sonar-web/src/main/js/apps/projects/components/__tests__/__snapshots__/ProjectCardLeak-test.tsx.snap b/server/sonar-web/src/main/js/apps/projects/components/__tests__/__snapshots__/ProjectCardLeak-test.tsx.snap
index d3204d97486..bcf67883a01 100644
--- a/server/sonar-web/src/main/js/apps/projects/components/__tests__/__snapshots__/ProjectCardLeak-test.tsx.snap
+++ b/server/sonar-web/src/main/js/apps/projects/components/__tests__/__snapshots__/ProjectCardLeak-test.tsx.snap
@@ -168,7 +168,7 @@ exports[`should display the leak measures and quality gate 1`] = `
<span
className="project-card-leak-date pull-right"
>
- projects.new_code_period_x.duration.years.1 duration.months.8
+ projects.new_code_period_x.duration.months.8
</span>
<DateTimeFormatter
date="2017-01-01"
diff --git a/server/sonar-web/src/main/js/apps/projects/utils.ts b/server/sonar-web/src/main/js/apps/projects/utils.ts
index 61ea473403f..6c515d4a1df 100644
--- a/server/sonar-web/src/main/js/apps/projects/utils.ts
+++ b/server/sonar-web/src/main/js/apps/projects/utils.ts
@@ -369,19 +369,19 @@ function format(periods: Array<{ value: number; label: string }>) {
return result;
}
-export function formatDuration(value: number) {
- if (value < ONE_MINUTE) {
+export function formatDuration(ms: number) {
+ if (ms < ONE_MINUTE) {
return translate('duration.seconds');
}
- const years = Math.floor(value / ONE_YEAR);
- value -= years * ONE_YEAR;
- const months = Math.floor(value / ONE_MONTH);
- value -= months * ONE_MONTH;
- const days = Math.floor(value / ONE_DAY);
- value -= days * ONE_DAY;
- const hours = Math.floor(value / ONE_HOUR);
- value -= hours * ONE_HOUR;
- const minutes = Math.floor(value / ONE_MINUTE);
+ const years = Math.floor(ms / ONE_YEAR);
+ ms -= years * ONE_YEAR;
+ const months = Math.floor(ms / ONE_MONTH);
+ ms -= months * ONE_MONTH;
+ const days = Math.floor(ms / ONE_DAY);
+ ms -= days * ONE_DAY;
+ const hours = Math.floor(ms / ONE_HOUR);
+ ms -= hours * ONE_HOUR;
+ const minutes = Math.floor(ms / ONE_MINUTE);
return format([
{ value: years, label: 'duration.years' },
{ value: months, label: 'duration.months' },