Add tests for new methods

This commit is contained in:
rejas 2021-08-31 23:26:06 +02:00
parent 43eb760bce
commit f2bc10c5c0
4 changed files with 54 additions and 3 deletions

View File

@ -12,6 +12,13 @@
* As soon as we start implementing the forecast, mode properties will be added. * As soon as we start implementing the forecast, mode properties will be added.
*/ */
class WeatherObject { class WeatherObject {
/**
*
* @param units
* @param tempUnits
* @param windUnits
* @param useKmh
*/
constructor(units, tempUnits, windUnits, useKmh) { constructor(units, tempUnits, windUnits, useKmh) {
this.units = units; this.units = units;
this.tempUnits = tempUnits; this.tempUnits = tempUnits;
@ -132,8 +139,14 @@ class WeatherObject {
* @param {number} lon longitude * @param {number} lon longitude
*/ */
updateSunTime(lat, lon) { 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.sunrise = moment(times.sunrise, "X");
this.sunset = moment(times.sunset, "X"); this.sunset = moment(times.sunset, "X");
} }
} }
/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = WeatherObject;
}

15
package-lock.json generated
View File

@ -44,7 +44,8 @@
"stylelint": "^13.13.1", "stylelint": "^13.13.1",
"stylelint-config-prettier": "^8.0.2", "stylelint-config-prettier": "^8.0.2",
"stylelint-config-standard": "^22.0.0", "stylelint-config-standard": "^22.0.0",
"stylelint-prettier": "^1.2.0" "stylelint-prettier": "^1.2.0",
"suncalc": "^1.8.0"
}, },
"engines": { "engines": {
"node": ">=12" "node": ">=12"
@ -9373,6 +9374,12 @@
"node": ">= 8.0" "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": { "node_modules/supports-color": {
"version": "7.2.0", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
@ -17712,6 +17719,12 @@
"debug": "^4.1.0" "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": { "supports-color": {
"version": "7.2.0", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",

View File

@ -62,7 +62,8 @@
"stylelint": "^13.13.1", "stylelint": "^13.13.1",
"stylelint-config-prettier": "^8.0.2", "stylelint-config-prettier": "^8.0.2",
"stylelint-config-standard": "^22.0.0", "stylelint-config-standard": "^22.0.0",
"stylelint-prettier": "^1.2.0" "stylelint-prettier": "^1.2.0",
"suncalc": "^1.8.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"electron": "^13.2.3" "electron": "^13.2.3"

View File

@ -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);
});
});