2021-08-31 23:26:06 +02:00
|
|
|
const WeatherObject = require("../../../modules/default/weather/weatherobject.js");
|
2022-10-28 19:56:55 +02:00
|
|
|
const WeatherUtils = require("../../../modules/default/weather/weatherutils.js");
|
2021-08-31 23:26:06 +02:00
|
|
|
|
2021-09-06 21:15:23 +02:00
|
|
|
global.moment = require("moment-timezone");
|
2021-08-31 23:26:06 +02:00
|
|
|
global.SunCalc = require("suncalc");
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
describe("WeatherObject", () => {
|
2021-09-06 21:15:23 +02:00
|
|
|
let originalTimeZone;
|
2021-08-31 23:26:06 +02:00
|
|
|
let weatherobject;
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
beforeAll(() => {
|
2021-09-06 21:15:23 +02:00
|
|
|
originalTimeZone = moment.tz.guess();
|
|
|
|
moment.tz.setDefault("Africa/Dar_es_Salaam");
|
2022-10-24 19:41:34 +02:00
|
|
|
weatherobject = new WeatherObject();
|
2021-08-31 23:26:06 +02:00
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
it("should return true for daytime at noon", () => {
|
2023-01-26 19:43:34 +01:00
|
|
|
weatherobject.date = moment("12:00", "HH:mm");
|
2021-08-31 23:26:06 +02:00
|
|
|
weatherobject.updateSunTime(-6.774877582342688, 37.63345667023327);
|
|
|
|
expect(weatherobject.isDayTime()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-10-04 10:15:24 +02:00
|
|
|
it("should return false for daytime at midnight", () => {
|
2023-01-26 19:43:34 +01:00
|
|
|
weatherobject.date = moment("00:00", "HH:mm");
|
2021-08-31 23:26:06 +02:00
|
|
|
weatherobject.updateSunTime(-6.774877582342688, 37.63345667023327);
|
|
|
|
expect(weatherobject.isDayTime()).toBe(false);
|
|
|
|
});
|
2021-09-06 21:15:23 +02:00
|
|
|
|
2023-01-26 19:43:34 +01:00
|
|
|
it("should return sunrise as the next sunaction", () => {
|
|
|
|
weatherobject.updateSunTime(-6.774877582342688, 37.63345667023327);
|
|
|
|
let midnight = moment("00:00", "HH:mm");
|
|
|
|
expect(weatherobject.nextSunAction(midnight)).toBe("sunrise");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return sunset as the next sunaction", () => {
|
|
|
|
weatherobject.updateSunTime(-6.774877582342688, 37.63345667023327);
|
|
|
|
let noon = moment(weatherobject.sunrise).hour(14);
|
|
|
|
expect(weatherobject.nextSunAction(noon)).toBe("sunset");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return an already defined feelsLike info", () => {
|
|
|
|
weatherobject.feelsLikeTemp = "feelsLikeTempValue";
|
|
|
|
expect(weatherobject.feelsLike()).toBe("feelsLikeTempValue");
|
|
|
|
});
|
|
|
|
|
2022-10-28 19:56:55 +02:00
|
|
|
afterAll(() => {
|
|
|
|
moment.tz.setDefault(originalTimeZone);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-26 19:43:34 +01:00
|
|
|
describe("WeatherUtils", () => {
|
2023-02-07 00:03:08 +01:00
|
|
|
it("should convert windspeed correctly from mps to beaufort", () => {
|
|
|
|
expect(Math.round(WeatherUtils.convertWind(5, "beaufort"))).toBe(3);
|
|
|
|
expect(Math.round(WeatherUtils.convertWind(42, "beaufort"))).toBe(12);
|
|
|
|
});
|
|
|
|
|
2022-10-24 19:41:34 +02:00
|
|
|
it("should convert windspeed correctly from mph to mps", () => {
|
2022-10-28 19:56:55 +02:00
|
|
|
expect(Math.round(WeatherUtils.convertWindToMetric(93.951324266285))).toBe(42);
|
2022-10-24 19:41:34 +02:00
|
|
|
});
|
|
|
|
|
2022-10-28 19:56:55 +02:00
|
|
|
it("should convert windspeed correctly from kmh to mps", () => {
|
|
|
|
expect(Math.round(WeatherUtils.convertWindToMs(151.2))).toBe(42);
|
2022-10-24 19:41:34 +02:00
|
|
|
});
|
|
|
|
|
2022-10-28 19:56:55 +02:00
|
|
|
it("should convert wind direction correctly from cardinal to value", () => {
|
|
|
|
expect(WeatherUtils.convertWindDirection("SSE")).toBe(157);
|
2021-09-06 21:15:23 +02:00
|
|
|
});
|
2023-01-26 19:43:34 +01:00
|
|
|
|
|
|
|
it("should return a calculated feelsLike info", () => {
|
|
|
|
expect(WeatherUtils.calculateFeelsLike(0, 20, 40)).toBe(-9.444444444444445);
|
|
|
|
});
|
2023-02-07 00:03:08 +01:00
|
|
|
|
|
|
|
it("should return a calculated feelsLike info", () => {
|
|
|
|
expect(WeatherUtils.calculateFeelsLike(30, 0, 60)).toBe(32.8320322777777);
|
|
|
|
});
|
2021-08-31 23:26:06 +02:00
|
|
|
});
|