From: Jérôme Benoit Date: Wed, 8 Nov 2023 18:52:05 +0000 (+0100) Subject: fix(simulator): format properly duration close to zero X-Git-Tag: v1.2.24~30 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d7ceb0f08aac1129438b46fe854678aa7aff27ce;p=e-mobility-charging-stations-simulator.git fix(simulator): format properly duration close to zero Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 6d8c2228..ca4d2fa9 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -51,15 +51,15 @@ export const formatDurationMilliSeconds = (duration: number): string => { hoursToSeconds(hours) - minutesToSeconds(minutes), ); - return formatDuration( - { - days, - hours, - minutes, - seconds, - }, - { zero: true }, - ); + if (days === 0 && hours === 0 && minutes === 0 && seconds === 0) { + return formatDuration({ seconds }, { zero: true }); + } + return formatDuration({ + days, + hours, + minutes, + seconds, + }); }; export const formatDurationSeconds = (duration: number): string => { diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 60170cf9..82964803 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -58,13 +58,15 @@ await describe('Utils test suite', async () => { }); await it('Verify formatDurationMilliSeconds()', () => { - expect(formatDurationMilliSeconds(0)).toBe(''); + expect(formatDurationMilliSeconds(0)).toBe('0 seconds'); + expect(formatDurationMilliSeconds(900)).toBe('0 seconds'); expect(formatDurationMilliSeconds(1000)).toBe('1 second'); expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours'); }); await it('Verify formatDurationSeconds()', () => { - expect(formatDurationSeconds(0)).toBe(''); + expect(formatDurationSeconds(0)).toBe('0 seconds'); + expect(formatDurationSeconds(0.9)).toBe('0 seconds'); expect(formatDurationSeconds(1)).toBe('1 second'); expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours'); });