From 78592e3dc91a39bbf9431fd470598ddaa3de0beb Mon Sep 17 00:00:00 2001 From: phix Date: Fri, 15 Apr 2022 14:00:27 -0700 Subject: [PATCH 1/2] Fixing time module and tests. --- web/src/__tests__/reducers/jobs.test.ts | 42 +++++++++++++++++++++++++ web/src/helpers/time.ts | 10 +++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/web/src/__tests__/reducers/jobs.test.ts b/web/src/__tests__/reducers/jobs.test.ts index 0abb853706..7e81f19a66 100644 --- a/web/src/__tests__/reducers/jobs.test.ts +++ b/web/src/__tests__/reducers/jobs.test.ts @@ -2,6 +2,7 @@ import * as actionTypes from '../../store/actionCreators/actionTypes' import jobsReducer, { initialState } from '../../store/reducers/jobs' +import { stopWatchDuration } from "../../helpers/time"; const jobs = require('../../../docker/db/data/jobs.json') @@ -17,3 +18,44 @@ describe('jobs reducer', () => { expect(jobsReducer(initialState, action)).toStrictEqual({ isLoading: false, result: jobs, init: true }) }) }) + +describe('stopWatchDuration', () => { + const oneHour = 60 * 60 * 1000; + const oneDay = 24 * oneHour; + + it('more than one week', () => { + const value = stopWatchDuration(oneDay * 9) + expect("9d 0h 0m 0s").toBe(value); + }) + + it('more than one day', () => { + const value = stopWatchDuration(oneDay + oneHour) + expect("1d 1h 0m 0s").toBe(value); + }) + + it('less than one day', () => { + const value = stopWatchDuration(oneDay - 1000); + expect("23h 59m 59s").toBe(value); + }) + + it('less than one hour', () => { + const value = stopWatchDuration(oneHour - 1000); + expect("59m 59s").toBe(value); + }) + + it('less than one minute', () => { + const value = stopWatchDuration(oneHour - 1000); + expect("59m 59s").toBe(value); + }) + + it('less than one second', () => { + const value = stopWatchDuration(999); + expect("999 ms").toBe(value); + }) + + it('no time', () => { + const value = stopWatchDuration(0); + expect("0").toBe(value); + }) + +}) diff --git a/web/src/helpers/time.ts b/web/src/helpers/time.ts index 54f5192126..40d75a0c5f 100644 --- a/web/src/helpers/time.ts +++ b/web/src/helpers/time.ts @@ -14,13 +14,15 @@ export function stopWatchDuration(durationMs: number) { if (duration.asMilliseconds() === 0) { return '0' } - - if (duration.asHours() > 1) { - return `${duration.hours()}h ${addLeadingZero(duration.seconds())}s` + if (duration.asHours() > 24) { + return `${duration.days()}d ${duration.hours()}h ${duration.minutes()}m ${duration.seconds()}s` + } + if (duration.asMinutes() > 60) { + return `${duration.hours()}h ${duration.minutes()}m ${duration.seconds()}s` } if (duration.asSeconds() > 1) { return `${duration.minutes()}m ${addLeadingZero(duration.seconds())}s` } else { - return `${duration.milliseconds()} ms` + return `${duration.asMilliseconds()} ms` } } From 7f64157e246f9bffe189849124e9a57e52dcf2ab Mon Sep 17 00:00:00 2001 From: phix Date: Fri, 15 Apr 2022 14:07:05 -0700 Subject: [PATCH 2/2] Better tests and constants. --- web/src/__tests__/reducers/jobs.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/web/src/__tests__/reducers/jobs.test.ts b/web/src/__tests__/reducers/jobs.test.ts index 7e81f19a66..24590647d4 100644 --- a/web/src/__tests__/reducers/jobs.test.ts +++ b/web/src/__tests__/reducers/jobs.test.ts @@ -20,7 +20,8 @@ describe('jobs reducer', () => { }) describe('stopWatchDuration', () => { - const oneHour = 60 * 60 * 1000; + const oneMinute = 60 * 1000; + const oneHour = 60 * oneMinute; const oneDay = 24 * oneHour; it('more than one week', () => { @@ -44,8 +45,8 @@ describe('stopWatchDuration', () => { }) it('less than one minute', () => { - const value = stopWatchDuration(oneHour - 1000); - expect("59m 59s").toBe(value); + const value = stopWatchDuration(oneMinute - 1000); + expect("0m 59s").toBe(value); }) it('less than one second', () => {