Skip to content
Snippets Groups Projects
Commit d1ab3eb7 authored by Reinder Kraaij's avatar Reinder Kraaij :eye:
Browse files

When something is null , it's nothing and thus 0

parent 3bc51444
No related branches found
No related tags found
1 merge request!1207When something is null , it's nothing and thus 0
...@@ -104,8 +104,11 @@ export const UnitConverter = { ...@@ -104,8 +104,11 @@ export const UnitConverter = {
return seconds; return seconds;
}, },
getSecsToHrsWithFractionDigits: function (seconds, fractionDigits = 2) { getSecsToHrsWithFractionDigits: function (seconds, fractionDigits = 2) {
if (seconds===null ){
return 0;
}
if (typeof seconds !== "number" || isNaN(seconds)) { if (typeof seconds !== "number" || isNaN(seconds)) {
throw TypeError("Cannot convert a wrong value. Must be a number") return "0?";
} }
return (seconds / 3600).toFixed(fractionDigits) return (seconds / 3600).toFixed(fractionDigits)
}, },
......
...@@ -60,9 +60,7 @@ describe('getSecsToHrsWithFractionDigits', () => { ...@@ -60,9 +60,7 @@ describe('getSecsToHrsWithFractionDigits', () => {
it('should handle zero seconds', () => { it('should handle zero seconds', () => {
const seconds = 0; const seconds = 0;
const result = UnitConverter.getSecsToHrsWithFractionDigits(seconds); const result = UnitConverter.getSecsToHrsWithFractionDigits(seconds);
expect(result).toBe('0.00'); expect(result).toBe('0.00');
}); });
...@@ -74,10 +72,15 @@ describe('getSecsToHrsWithFractionDigits', () => { ...@@ -74,10 +72,15 @@ describe('getSecsToHrsWithFractionDigits', () => {
expect(result).toBe('-0.37'); expect(result).toBe('-0.37');
}); });
test.each(["12", null, undefined, []])('Should log an error and return "Invalid" for wrong input: %s', (wrongSeconds) => { test.each(["12", undefined, "aa"])('Should handle invalid data with 0? for wrong input: %s', (wrongSeconds) => {
const result = UnitConverter.getSecsToHrsWithFractionDigits(wrongSeconds);
expect(result).toBe('0?');
});
expect(() => {
UnitConverter.getSecsToHrsWithFractionDigits(wrongSeconds); it('should handle null with 0', () => {
}).toThrow(TypeError) const wrongSeconds = null;
}) const result = UnitConverter.getSecsToHrsWithFractionDigits(wrongSeconds);
expect(result).toBe(0);
});
}); });
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment