From 66759a33fa76177f08c9ed6344a2551767a71f8c Mon Sep 17 00:00:00 2001 From: Karsten Hassel Date: Sun, 6 Jun 2021 23:13:09 +0200 Subject: [PATCH 01/22] unit tests --- js/logger.js | 10 ++++--- .../weatherforecast/weatherforecast.js | 26 +++++++++++-------- package.json | 14 +++++++--- tests/unit/classes/class_spec.js | 6 ++--- tests/unit/classes/translator_spec.js | 16 ++++++++++-- tests/unit/functions/calendar_spec.js | 2 +- tests/unit/functions/cmp_versions_spec.js | 2 +- tests/unit/functions/currentweather_spec.js | 6 ++--- tests/unit/functions/newsfeed_spec.js | 4 ++- tests/unit/functions/weatherforecast_spec.js | 18 ++++++------- .../unit/global_vars/defaults_modules_spec.js | 10 +++---- tests/unit/global_vars/root_path_spec.js | 14 +++++----- 12 files changed, 78 insertions(+), 50 deletions(-) diff --git a/js/logger.js b/js/logger.js index 93a5bb53..1e74c850 100644 --- a/js/logger.js +++ b/js/logger.js @@ -22,7 +22,8 @@ root.Log = factory(root.config); } })(this, function (config) { - const logLevel = { + + let logLevel = { debug: Function.prototype.bind.call(console.debug, console), log: Function.prototype.bind.call(console.log, console), info: Function.prototype.bind.call(console.info, console), @@ -32,8 +33,11 @@ groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console), groupEnd: Function.prototype.bind.call(console.groupEnd, console), time: Function.prototype.bind.call(console.time, console), - timeEnd: Function.prototype.bind.call(console.timeEnd, console), - timeStamp: Function.prototype.bind.call(console.timeStamp, console) + timeEnd: Function.prototype.bind.call(console.timeEnd, console) + }; + + if (process.env.NODE_ENV.trim() !== "test") { + logLevel.push({timeStamp: Function.prototype.bind.call(console.timeStamp, console)}); }; logLevel.setLogLevel = function (newLevel) { diff --git a/modules/default/weatherforecast/weatherforecast.js b/modules/default/weatherforecast/weatherforecast.js index c180f5f5..39a7119b 100644 --- a/modules/default/weatherforecast/weatherforecast.js +++ b/modules/default/weatherforecast/weatherforecast.js @@ -339,7 +339,9 @@ Module.register("weatherforecast", { * * argument data object - Weather information received form openweather.org. */ - processWeather: function (data) { + processWeather: function (data, momenttz) { + let mom; + if (momenttz === null) {mom = moment} else {mom = momenttz}; // Forcast16 (paid) API endpoint provides this data. Onecall endpoint // does not. if (data.city) { @@ -357,8 +359,8 @@ Module.register("weatherforecast", { var dayEnds = 17; if (data.city && data.city.sunrise && data.city.sunset) { - dayStarts = new Date(moment.unix(data.city.sunrise).locale("en").format("YYYY/MM/DD HH:mm:ss")).getHours(); - dayEnds = new Date(moment.unix(data.city.sunset).locale("en").format("YYYY/MM/DD HH:mm:ss")).getHours(); + dayStarts = new Date(mom.unix(data.city.sunrise).locale("en").format("YYYY/MM/DD HH:mm:ss")).getHours(); + dayEnds = new Date(mom.unix(data.city.sunset).locale("en").format("YYYY/MM/DD HH:mm:ss")).getHours(); } // Handle different structs between forecast16 and onecall endpoints @@ -379,11 +381,11 @@ Module.register("weatherforecast", { var day; var hour; if (forecast.dt_txt) { - day = moment(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss").format("ddd"); - hour = new Date(moment(forecast.dt_txt).locale("en").format("YYYY-MM-DD HH:mm:ss")).getHours(); + day = mom(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss").format("ddd"); + hour = new Date(mom(forecast.dt_txt).locale("en").format("YYYY-MM-DD HH:mm:ss")).getHours(); } else { - day = moment(forecast.dt, "X").format("ddd"); - hour = new Date(moment(forecast.dt, "X")).getHours(); + day = mom(forecast.dt, "X").format("ddd"); + hour = new Date(mom(forecast.dt, "X")).getHours(); } if (day !== lastDay) { @@ -392,7 +394,7 @@ Module.register("weatherforecast", { icon: this.config.iconTable[forecast.weather[0].icon], maxTemp: this.roundValue(forecast.temp.max), minTemp: this.roundValue(forecast.temp.min), - rain: this.processRain(forecast, forecastList) + rain: this.processRain(forecast, forecastList, mom) }; this.forecast.push(forecastData); lastDay = day; @@ -482,16 +484,18 @@ Module.register("weatherforecast", { * That object has a property "3h" which contains the amount of rain since the previous forecast in the list. * This code finds all forecasts that is for the same day and sums the amount of rain and returns that. */ - processRain: function (forecast, allForecasts) { + processRain: function (forecast, allForecasts, momenttz) { + let mom; + if (momenttz === null) {mom = moment} else {mom = momenttz}; //If the amount of rain actually is a number, return it if (!isNaN(forecast.rain)) { return forecast.rain; } //Find all forecasts that is for the same day - var checkDateTime = forecast.dt_txt ? moment(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss") : moment(forecast.dt, "X"); + var checkDateTime = forecast.dt_txt ? mom(forecast.dt_txt, "YYYY-MM-DD hh:mm:ss") : mom(forecast.dt, "X"); var daysForecasts = allForecasts.filter(function (item) { - var itemDateTime = item.dt_txt ? moment(item.dt_txt, "YYYY-MM-DD hh:mm:ss") : moment(item.dt, "X"); + var itemDateTime = item.dt_txt ? mom(item.dt_txt, "YYYY-MM-DD hh:mm:ss") : mom(item.dt, "X"); return itemDateTime.isSame(checkDateTime, "day") && item.rain instanceof Object; }); diff --git a/package.json b/package.json index 098b6efb..880fb3ef 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "test": "NODE_ENV=test mocha tests --recursive", "test:coverage": "NODE_ENV=test nyc --reporter=lcov --reporter=text mocha tests --recursive --timeout=3000", "test:e2e": "NODE_ENV=test mocha tests/e2e --recursive", - "test:unit": "NODE_ENV=test mocha tests/unit --recursive", + "test:unit": "NODE_ENV=test jest --testMatch **/tests/unit/**/*", "test:prettier": "prettier . --check", "test:js": "eslint js/**/*.js modules/default/**/*.js clientonly/*.js serveronly/*.js translations/*.js vendor/*.js tests/**/*.js config/* --config .eslintrc.json --quiet", "test:css": "stylelint css/main.css modules/default/**/*.css --config .stylelintrc.json", @@ -52,11 +52,9 @@ "eslint-plugin-prettier": "^3.4.0", "express-basic-auth": "^1.2.0", "husky": "^6.0.0", + "jest": "27.0.4", "jsdom": "^16.6.0", "lodash": "^4.17.21", - "mocha": "^8.4.0", - "mocha-each": "^2.0.1", - "mocha-logger": "^1.0.7", "nyc": "^15.1.0", "prettier": "^2.3.0", "pretty-quick": "^3.1.0", @@ -93,5 +91,13 @@ }, "engines": { "node": ">=12" + }, + "jest": { + "moduleNameMapper": { + "node_helper": "/js/node_helper.js", + "logger": "/js/logger.js", + "moment-timezone": "/node_modules/moment-timezone/moment-timezone.js", + "moment": "/node_modules/moment/moment.js" + } } } diff --git a/tests/unit/classes/class_spec.js b/tests/unit/classes/class_spec.js index b1af5c67..f7f41143 100644 --- a/tests/unit/classes/class_spec.js +++ b/tests/unit/classes/class_spec.js @@ -7,7 +7,7 @@ describe("File js/class", function () { let clone; let dom; - before(function (done) { + beforeAll(function (done) { dom = new JSDOM( `\ \ \ \