diff --git a/modules/default/weather/weatherobject.js b/modules/default/weather/weatherobject.js index 7495844b..2d08f028 100755 --- a/modules/default/weather/weatherobject.js +++ b/modules/default/weather/weatherobject.js @@ -12,6 +12,13 @@ * As soon as we start implementing the forecast, mode properties will be added. */ class WeatherObject { + /** + * + * @param units + * @param tempUnits + * @param windUnits + * @param useKmh + */ constructor(units, tempUnits, windUnits, useKmh) { this.units = units; this.tempUnits = tempUnits; @@ -132,8 +139,14 @@ class WeatherObject { * @param {number} lon longitude */ updateSunTime(lat, lon) { - let times = SunCalc.getTimes(new Date(), lat, lon); + let now = !this.date ? new Date() : this.date.toDate(); + let times = SunCalc.getTimes(now, lat, lon); this.sunrise = moment(times.sunrise, "X"); this.sunset = moment(times.sunset, "X"); } } + +/*************** DO NOT EDIT THE LINE BELOW ***************/ +if (typeof module !== "undefined") { + module.exports = WeatherObject; +} diff --git a/package-lock.json b/package-lock.json index 68a80731..ae6ab35e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -44,7 +44,8 @@ "stylelint": "^13.13.1", "stylelint-config-prettier": "^8.0.2", "stylelint-config-standard": "^22.0.0", - "stylelint-prettier": "^1.2.0" + "stylelint-prettier": "^1.2.0", + "suncalc": "^1.8.0" }, "engines": { "node": ">=12" @@ -9373,6 +9374,12 @@ "node": ">= 8.0" } }, + "node_modules/suncalc": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/suncalc/-/suncalc-1.8.0.tgz", + "integrity": "sha1-HZiYEJVjB4dQ9JlKlZ5lTYdqy/U=", + "dev": true + }, "node_modules/supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", @@ -17712,6 +17719,12 @@ "debug": "^4.1.0" } }, + "suncalc": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/suncalc/-/suncalc-1.8.0.tgz", + "integrity": "sha1-HZiYEJVjB4dQ9JlKlZ5lTYdqy/U=", + "dev": true + }, "supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", diff --git a/package.json b/package.json index dd96c7af..6bce27aa 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,8 @@ "stylelint": "^13.13.1", "stylelint-config-prettier": "^8.0.2", "stylelint-config-standard": "^22.0.0", - "stylelint-prettier": "^1.2.0" + "stylelint-prettier": "^1.2.0", + "suncalc": "^1.8.0" }, "optionalDependencies": { "electron": "^13.2.3" diff --git a/tests/unit/functions/weather_object_spec.js b/tests/unit/functions/weather_object_spec.js new file mode 100644 index 00000000..8878f377 --- /dev/null +++ b/tests/unit/functions/weather_object_spec.js @@ -0,0 +1,24 @@ +const WeatherObject = require("../../../modules/default/weather/weatherobject.js"); + +global.moment = require("moment"); +global.SunCalc = require("suncalc"); + +describe("WeatherObject", function () { + let weatherobject; + + beforeAll(function () { + weatherobject = new WeatherObject("metric", "metric", "metric", true); + }); + + it("should return true for daytime at noon", function () { + weatherobject.date = moment(12, "HH"); + weatherobject.updateSunTime(-6.774877582342688, 37.63345667023327); + expect(weatherobject.isDayTime()).toBe(true); + }); + + it("should return false for daytime at midnight", function () { + weatherobject.date = moment(0, "HH"); + weatherobject.updateSunTime(-6.774877582342688, 37.63345667023327); + expect(weatherobject.isDayTime()).toBe(false); + }); +});