From 7468647b37542d29df4bdbf083d7d4f098d1a1ed Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Wed, 3 Nov 2021 09:28:34 -0500 Subject: [PATCH] Extracted duration function and related tests Signed-off-by: Andre Wanlin --- .../components/BuildTable/BuildTable.test.ts | 20 ------- .../src/components/BuildTable/BuildTable.tsx | 46 +-------------- .../src/utils/getDurationFromDates.test.ts | 35 +++++++++++ .../src/utils/getDurationFromDates.ts | 58 +++++++++++++++++++ 4 files changed, 96 insertions(+), 63 deletions(-) create mode 100644 plugins/azure-devops/src/utils/getDurationFromDates.test.ts create mode 100644 plugins/azure-devops/src/utils/getDurationFromDates.ts diff --git a/plugins/azure-devops/src/components/BuildTable/BuildTable.test.ts b/plugins/azure-devops/src/components/BuildTable/BuildTable.test.ts index 3797cb65d6..22476b334e 100644 --- a/plugins/azure-devops/src/components/BuildTable/BuildTable.test.ts +++ b/plugins/azure-devops/src/components/BuildTable/BuildTable.test.ts @@ -242,23 +242,3 @@ describe('getBuildStateComponent', () => { }); }); }); - -describe('getBuildDuration', () => { - describe('getBuildDuration with undefined startTime and valid finishTime', () => { - it('should return empty result', () => { - const finishTime = new Date('2021-10-15T11:00:00.0000000Z'); - - const result = getBuildDuration(undefined, finishTime); - - expect(result).toEqual(''); - }); - }); - - describe('getBuildDuration with undefined startTime and undefined finishTime', () => { - it('should return empty result', () => { - const result = getBuildDuration(undefined, undefined); - - expect(result).toEqual(''); - }); - }); -}); diff --git a/plugins/azure-devops/src/components/BuildTable/BuildTable.tsx b/plugins/azure-devops/src/components/BuildTable/BuildTable.tsx index 94d02d15cd..cca4b4a92e 100644 --- a/plugins/azure-devops/src/components/BuildTable/BuildTable.tsx +++ b/plugins/azure-devops/src/components/BuildTable/BuildTable.tsx @@ -20,7 +20,6 @@ import { BuildStatus, RepoBuild, } from '@backstage/plugin-azure-devops-common'; -import { DateTime, Interval } from 'luxon' import { Link, ResponseErrorPanel, @@ -35,8 +34,9 @@ import { } from '@backstage/core-components'; import { AzurePipelinesIcon } from '../AzurePipelinesIcon'; +import { DateTime } from 'luxon'; import React from 'react'; -import humanizeDuration from 'humanize-duration'; +import { getDurationFromDates } from '../../utils/getDurationFromDates'; export const getBuildResultComponent = (result: number | undefined) => { switch (result) { @@ -115,46 +115,6 @@ export const getBuildStateComponent = ( } }; -export const getBuildDuration = ( - startTime?: Date, - finishTime?: Date, -): string => { - if (!startTime || (!startTime && !finishTime)) { - return ''; - } - - const start = DateTime.fromISO(startTime.toString()); - const finish = finishTime - ? DateTime.fromISO(finishTime.toString()) - : DateTime.now(); - - const formatted = Interval.fromDateTimes(start, finish) - .toDuration() - .valueOf(); - - const shortEnglishHumanizer = humanizeDuration.humanizer({ - language: 'shortEn', - languages: { - shortEn: { - y: () => 'y', - mo: () => 'mo', - w: () => 'w', - d: () => 'd', - h: () => 'h', - m: () => 'm', - s: () => 's', - ms: () => 'ms', - }, - }, - }); - - return shortEnglishHumanizer(formatted, { - largest: 2, - round: true, - spacer: '', - }); -}; - const columns: TableColumn[] = [ { title: 'ID', @@ -193,7 +153,7 @@ const columns: TableColumn[] = [ render: (row: Partial) => ( - {getBuildDuration(row.startTime, row.finishTime)} + {getDurationFromDates(row.startTime, row.finishTime)} ), diff --git a/plugins/azure-devops/src/utils/getDurationFromDates.test.ts b/plugins/azure-devops/src/utils/getDurationFromDates.test.ts new file mode 100644 index 0000000000..36763b7cba --- /dev/null +++ b/plugins/azure-devops/src/utils/getDurationFromDates.test.ts @@ -0,0 +1,35 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { getDurationFromDates } from './getDurationFromDates'; + +describe('getDurationFromDates with undefined startTime and valid finishTime', () => { + it('should return empty result', () => { + const finishTime = new Date('2021-10-15T11:00:00.0000000Z'); + + const result = getDurationFromDates(undefined, finishTime); + + expect(result).toEqual(''); + }); +}); + +describe('getDurationFromDates with undefined startTime and undefined finishTime', () => { + it('should return empty result', () => { + const result = getDurationFromDates(undefined, undefined); + + expect(result).toEqual(''); + }); +}); diff --git a/plugins/azure-devops/src/utils/getDurationFromDates.ts b/plugins/azure-devops/src/utils/getDurationFromDates.ts new file mode 100644 index 0000000000..5b1fb60219 --- /dev/null +++ b/plugins/azure-devops/src/utils/getDurationFromDates.ts @@ -0,0 +1,58 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { DateTime, Interval } from 'luxon'; +import humanizeDuration from 'humanize-duration'; + +export const getDurationFromDates = ( + startTime?: Date, + finishTime?: Date, +): string => { + if (!startTime || (!startTime && !finishTime)) { + return ''; + } + + const start = DateTime.fromISO(startTime.toString()); + const finish = finishTime + ? DateTime.fromISO(finishTime.toString()) + : DateTime.now(); + + const formatted = Interval.fromDateTimes(start, finish) + .toDuration() + .valueOf(); + + const shortEnglishHumanizer = humanizeDuration.humanizer({ + language: 'shortEn', + languages: { + shortEn: { + y: () => 'y', + mo: () => 'mo', + w: () => 'w', + d: () => 'd', + h: () => 'h', + m: () => 'm', + s: () => 's', + ms: () => 'ms', + }, + }, + }); + + return shortEnglishHumanizer(formatted, { + largest: 2, + round: true, + spacer: '', + }); +};