Updated to handle dates nicely with tests
Signed-off-by: Andre Wanlin <awanlin@rapidrtc.com>
This commit is contained in:
@@ -80,8 +80,9 @@ const columns: TableColumn[] = [
|
||||
field: 'creationDate',
|
||||
width: 'auto',
|
||||
render: (row: Partial<PullRequest>) =>
|
||||
DateTime.fromISO(
|
||||
(row.creationDate ?? new Date()).toString(),
|
||||
(row.creationDate
|
||||
? DateTime.fromISO(row.creationDate)
|
||||
: DateTime.now()
|
||||
).toRelative(),
|
||||
},
|
||||
];
|
||||
|
||||
@@ -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('');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user