Skip to content

Commit

Permalink
minor provider code modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgrdich committed Dec 22, 2022
1 parent 1f4c430 commit db9948f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 5 additions & 4 deletions kafka-ui-react-app/src/lib/dateTimeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ export const formatTimestamp = (
return '';
}

// empty array gets the default one from the browser
const date = new Date(timestamp);

try {
// empty array gets the default one from the browser
return date.toLocaleString([], format);
} catch (e) {
// invalid date
if (Number.isNaN(date.getTime())) {
return '';
}

return date.toLocaleString([], format);
};

export const formatMilliseconds = (input = 0) => {
Expand Down
23 changes: 23 additions & 0 deletions kafka-ui-react-app/src/lib/hooks/__tests__/dateTimeHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { formatTimestamp } from 'lib/dateTimeHelpers';

describe('dateTimeHelpers', () => {
describe('formatTimestamp', () => {
it('should check the empty case', () => {
expect(formatTimestamp('')).toBe('');
});

it('should check the invalid case', () => {
expect(formatTimestamp('invalid')).toBe('');
});

it('should output the correct date', () => {
const date = new Date();
expect(formatTimestamp(date)).toBe(
date.toLocaleString([], { hour12: false })
);
expect(formatTimestamp(date.getTime())).toBe(
date.toLocaleString([], { hour12: false })
);
});
});
});

0 comments on commit db9948f

Please sign in to comment.