Skip to content

Commit

Permalink
fix: floor seconds to int in the edge case moment returns it as float
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Nad <[email protected]>
  • Loading branch information
olga-union committed Sep 8, 2022
1 parent afd85bf commit ef5285c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
40 changes: 40 additions & 0 deletions packages/zapp/console/src/common/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { millisecondsToHMS } from 'common/formatters';
import { unknownValueString, zeroSecondsString } from './constants';

describe('millisecondsToHMS', () => {
it('should display unknown if the value is negative', () => {
expect(millisecondsToHMS(-1)).toEqual(unknownValueString);
});

it('should display 0s if the value is 0', () => {
expect(millisecondsToHMS(0)).toEqual(zeroSecondsString);
});

it('should display in `ms` format if the value is < 1000', () => {
expect(millisecondsToHMS(999)).toEqual('999ms');
});

it('should display in `h` format if the value is >= 86400000 (24h)', () => {
expect(millisecondsToHMS(86400001)).toEqual('24h');
});

it('should display in `h m` format if the value is < 86400000 (24h)', () => {
expect(millisecondsToHMS(86340000)).toEqual('23h 59m');
});

it('should display in `h m s` format if the value is < 86400000 (24h)', () => {
expect(millisecondsToHMS(86399999)).toEqual('23h 59m 59s');
});

it('should display in `m s` format if the value is < 3600000 (1h)', () => {
expect(millisecondsToHMS(3599999)).toEqual('59m 59s');
});

it('should display in `m` format if the value is < 3600000 (1h) and seconds < 1', () => {
expect(millisecondsToHMS(3540000)).toEqual('59m');
});

it('should display in `s` format if the value is < 60000 (1m)', () => {
expect(millisecondsToHMS(59999)).toEqual('59s');
});
});
3 changes: 2 additions & 1 deletion packages/zapp/console/src/common/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export function millisecondsToHMS(valueMS: number): string {
}

if (duration.seconds() >= 1) {
parts.push(`${duration.seconds()}s`);
// there may be a bug in Momemt.js that shows float number of seconds, so rounding it down to make sure it will be int
parts.push(`${Math.floor(duration.seconds())}s`);
}

return parts.length ? parts.join(' ') : unknownValueString;
Expand Down

0 comments on commit ef5285c

Please sign in to comment.