From 9a69333f1cad5a6b8a5e64744dcd25352f25737c Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Thu, 23 Aug 2018 10:53:03 +0200 Subject: [PATCH] change ProjectCardLeak-test to not depend on current time --- .../projects/components/ProjectCardLeak.tsx | 8 +++---- .../__tests__/ProjectCardLeak-test.tsx | 6 +++++ .../ProjectCardLeak-test.tsx.snap | 2 +- .../src/main/js/apps/projects/utils.ts | 22 +++++++++---------- 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 (
@@ -79,7 +79,7 @@ export default function ProjectCardLeak({ height, organization, project }: Props project.leakPeriodDate && (
- {translateWithParameters('projects.new_code_period_x', formatDuration(period))} + {translateWithParameters('projects.new_code_period_x', formatDuration(periodMs))} {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`] = ` - projects.new_code_period_x.duration.years.1 duration.months.8 + projects.new_code_period_x.duration.months.8 ) { 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' }, -- 2.39.5