From d1ab3eb7a6d9d7e04f43c669ff5ffeb9f9f09754 Mon Sep 17 00:00:00 2001 From: Reinder Kraaij <kraaij@astron.nl> Date: Mon, 6 Nov 2023 08:10:23 +0000 Subject: [PATCH] When something is null , it's nothing and thus 0 --- .../tmss_webapp/src/utils/unit.converter.js | 5 ++++- .../src/utils/unit.converter.test.js | 17 ++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.js b/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.js index 525f1bbd238..7b062a66fd9 100644 --- a/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.js +++ b/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.js @@ -104,8 +104,11 @@ export const UnitConverter = { return seconds; }, getSecsToHrsWithFractionDigits: function (seconds, fractionDigits = 2) { + if (seconds===null ){ + return 0; + } if (typeof seconds !== "number" || isNaN(seconds)) { - throw TypeError("Cannot convert a wrong value. Must be a number") + return "0?"; } return (seconds / 3600).toFixed(fractionDigits) }, diff --git a/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.test.js b/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.test.js index c48d61acda2..daaffdeb8e2 100644 --- a/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.test.js +++ b/SAS/TMSS/frontend/tmss_webapp/src/utils/unit.converter.test.js @@ -60,9 +60,7 @@ describe('getSecsToHrsWithFractionDigits', () => { it('should handle zero seconds', () => { const seconds = 0; - const result = UnitConverter.getSecsToHrsWithFractionDigits(seconds); - expect(result).toBe('0.00'); }); @@ -74,10 +72,15 @@ describe('getSecsToHrsWithFractionDigits', () => { 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); - }).toThrow(TypeError) - }) + + it('should handle null with 0', () => { + const wrongSeconds = null; + const result = UnitConverter.getSecsToHrsWithFractionDigits(wrongSeconds); + expect(result).toBe(0); + }); }); \ No newline at end of file -- GitLab