From 48fe020c05788e6feb02b720a68f3fc0361649b7 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Fri, 12 Nov 2021 13:52:29 -0600 Subject: [PATCH] Updated to handle dates nicely with tests Signed-off-by: Andre Wanlin --- .../PullRequestTable/PullRequestTable.tsx | 5 +- .../src/utils/getDurationFromDates.test.ts | 58 ++++++++++++++----- .../src/utils/getDurationFromDates.ts | 10 ++-- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/plugins/azure-devops/src/components/PullRequestTable/PullRequestTable.tsx b/plugins/azure-devops/src/components/PullRequestTable/PullRequestTable.tsx index a898766394..90e5f55b04 100644 --- a/plugins/azure-devops/src/components/PullRequestTable/PullRequestTable.tsx +++ b/plugins/azure-devops/src/components/PullRequestTable/PullRequestTable.tsx @@ -80,8 +80,9 @@ const columns: TableColumn[] = [ field: 'creationDate', width: 'auto', render: (row: Partial) => - DateTime.fromISO( - (row.creationDate ?? new Date()).toString(), + (row.creationDate + ? DateTime.fromISO(row.creationDate) + : DateTime.now() ).toRelative(), }, ]; diff --git a/plugins/azure-devops/src/utils/getDurationFromDates.test.ts b/plugins/azure-devops/src/utils/getDurationFromDates.test.ts index 36763b7cba..fac0e917d0 100644 --- a/plugins/azure-devops/src/utils/getDurationFromDates.test.ts +++ b/plugins/azure-devops/src/utils/getDurationFromDates.test.ts @@ -16,20 +16,52 @@ 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'); +describe('getDurationFromDates', () => { + beforeAll(() => { + jest.useFakeTimers('modern'); + jest.setSystemTime(new Date('2021-10-15T09:45:25.0000000Z')); + }); - const result = getDurationFromDates(undefined, finishTime); + afterAll(() => { + jest.useRealTimers(); + }); - expect(result).toEqual(''); - }); -}); - -describe('getDurationFromDates with undefined startTime and undefined finishTime', () => { - it('should return empty result', () => { - const result = getDurationFromDates(undefined, undefined); - - expect(result).toEqual(''); + describe('getDurationFromDates with valid startTime and valid finishTime', () => { + it('should return empty result', () => { + const startTime = '2021-10-15T09:30:00.0000000Z'; + const finishTime = '2021-10-15T11:00:00.0000000Z'; + + const result = getDurationFromDates(startTime, finishTime); + + expect(result).toEqual('1h, 30m'); + }); + }); + + describe('getDurationFromDates with valid startTime and undefined finishTime', () => { + it('should return empty result', () => { + const startTime = '2021-10-15T09:30:00.0000000Z'; + + const result = getDurationFromDates(startTime, undefined); + + expect(result).toEqual('15m, 25s'); + }); + }); + + describe('getDurationFromDates with undefined startTime and valid finishTime', () => { + it('should return empty result', () => { + const finishTime = '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 index 5b1fb60219..789849f975 100644 --- a/plugins/azure-devops/src/utils/getDurationFromDates.ts +++ b/plugins/azure-devops/src/utils/getDurationFromDates.ts @@ -18,17 +18,15 @@ import { DateTime, Interval } from 'luxon'; import humanizeDuration from 'humanize-duration'; export const getDurationFromDates = ( - startTime?: Date, - finishTime?: Date, + startTime?: string, + finishTime?: string, ): string => { if (!startTime || (!startTime && !finishTime)) { return ''; } - const start = DateTime.fromISO(startTime.toString()); - const finish = finishTime - ? DateTime.fromISO(finishTime.toString()) - : DateTime.now(); + const start = DateTime.fromISO(startTime); + const finish = finishTime ? DateTime.fromISO(finishTime) : DateTime.now(); const formatted = Interval.fromDateTimes(start, finish) .toDuration()