diff --git a/defaultmodules/compliments/compliments.js b/defaultmodules/compliments/compliments.js index 2ded7c191b..b591780630 100644 --- a/defaultmodules/compliments/compliments.js +++ b/defaultmodules/compliments/compliments.js @@ -147,7 +147,7 @@ Module.register("compliments", { timeOfDay = "evening"; } - if (timeOfDay && this.config.compliments.hasOwnProperty(timeOfDay)) { + if (this.config.compliments.hasOwnProperty(timeOfDay)) { compliments = [...this.config.compliments[timeOfDay]]; } diff --git a/defaultmodules/weather/weatherutils.js b/defaultmodules/weather/weatherutils.js index 7d1436c394..4f1abc9a4f 100644 --- a/defaultmodules/weather/weatherutils.js +++ b/defaultmodules/weather/weatherutils.js @@ -187,14 +187,12 @@ const WeatherUtils = { let imperialWeatherObject = { ...weatherObject }; - if (imperialWeatherObject) { - if (imperialWeatherObject.feelsLikeTemp) imperialWeatherObject.feelsLikeTemp = this.convertTemp(imperialWeatherObject.feelsLikeTemp, "imperial"); - if (imperialWeatherObject.maxTemperature) imperialWeatherObject.maxTemperature = this.convertTemp(imperialWeatherObject.maxTemperature, "imperial"); - if (imperialWeatherObject.minTemperature) imperialWeatherObject.minTemperature = this.convertTemp(imperialWeatherObject.minTemperature, "imperial"); - if (imperialWeatherObject.precipitationAmount) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, imperialWeatherObject.precipitationUnits); - if (imperialWeatherObject.temperature) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); - if (imperialWeatherObject.windSpeed) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); - } + if ("feelsLikeTemp" in imperialWeatherObject) imperialWeatherObject.feelsLikeTemp = this.convertTemp(imperialWeatherObject.feelsLikeTemp, "imperial"); + if ("maxTemperature" in imperialWeatherObject) imperialWeatherObject.maxTemperature = this.convertTemp(imperialWeatherObject.maxTemperature, "imperial"); + if ("minTemperature" in imperialWeatherObject) imperialWeatherObject.minTemperature = this.convertTemp(imperialWeatherObject.minTemperature, "imperial"); + if ("precipitationAmount" in imperialWeatherObject) imperialWeatherObject.precipitationAmount = this.convertPrecipitationToInch(imperialWeatherObject.precipitationAmount, imperialWeatherObject.precipitationUnits); + if ("temperature" in imperialWeatherObject) imperialWeatherObject.temperature = this.convertTemp(imperialWeatherObject.temperature, "imperial"); + if ("windSpeed" in imperialWeatherObject) imperialWeatherObject.windSpeed = this.convertWind(imperialWeatherObject.windSpeed, "imperial"); return imperialWeatherObject; } diff --git a/tests/unit/modules/default/weather/weather_utils_spec.js b/tests/unit/modules/default/weather/weather_utils_spec.js index 5f21121994..98c64b74b0 100644 --- a/tests/unit/modules/default/weather/weather_utils_spec.js +++ b/tests/unit/modules/default/weather/weather_utils_spec.js @@ -113,4 +113,51 @@ describe("Weather utils tests", () => { } }); }); + + describe("convertWeatherObjectToImperial", () => { + it("should return null for null or empty input", () => { + expect(WeatherUtils.convertWeatherObjectToImperial(null)).toBeNull(); + expect(WeatherUtils.convertWeatherObjectToImperial({})).toBeNull(); + }); + + it("should convert 0°C correctly to 32°F", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ temperature: 0 }); + expect(result.temperature).toBe(32); + }); + + it("should convert all temperature fields", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ + temperature: 0, + feelsLikeTemp: 0, + minTemperature: -10, + maxTemperature: 10 + }); + expect(result.temperature).toBe(32); + expect(result.feelsLikeTemp).toBe(32); + expect(result.minTemperature).toBe(14); + expect(result.maxTemperature).toBe(50); + }); + + it("should convert windSpeed correctly", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ windSpeed: 10 }); + expect(result.windSpeed).toBeCloseTo(22.369, 2); + }); + + it("should convert precipitationAmount correctly", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ precipitationAmount: 25.4, precipitationUnits: "mm" }); + expect(result.precipitationAmount).toBeCloseTo(1, 5); + }); + + it("should not modify properties that are not present", () => { + const result = WeatherUtils.convertWeatherObjectToImperial({ temperature: 20 }); + expect("windSpeed" in result).toBe(false); + expect("feelsLikeTemp" in result).toBe(false); + }); + + it("should not mutate the original object", () => { + const input = { temperature: 20 }; + WeatherUtils.convertWeatherObjectToImperial(input); + expect(input.temperature).toBe(20); + }); + }); });