diff --git a/CHANGELOG.md b/CHANGELOG.md index f701f165..a80d8883 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Run tests on long term support and latest stable version of nodejs - Added the ability to configure a list of modules that shouldn't be update checked. - Run linters on git commits +- Added date functionality to compliments: display birthday wishes or celebrate an anniversary ### Fixed - Force declaration of public ip address in config file (ISSUE #1852) diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index 606da1df..bf7bca9f 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -28,6 +28,9 @@ Module.register("compliments", { "Wow, you look hot!", "You look nice!", "Hi, sexy!" + ], + "....-01-01": [ + "Happy new year!" ] }, updateInterval: 30000, @@ -37,7 +40,8 @@ Module.register("compliments", { morningEndTime: 12, afternoonStartTime: 12, afternoonEndTime: 17, - random: true + random: true, + mockDate: null }, lastIndexUsed:-1, // Set currentweather from module @@ -102,6 +106,7 @@ Module.register("compliments", { */ complimentArray: function() { var hour = moment().hour(); + var date = this.config.mockDate ? this.config.mockDate : moment().format("YYYY-MM-DD"); var compliments; if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) { @@ -122,6 +127,12 @@ Module.register("compliments", { compliments.push.apply(compliments, this.config.compliments.anytime); + for (entry in this.config.compliments) { + if (new RegExp(entry).test(date)) { + compliments.push.apply(compliments, this.config.compliments[entry]); + } + } + return compliments; }, @@ -151,19 +162,19 @@ Module.register("compliments", { // get the current time of day compliments list var compliments = this.complimentArray(); // variable for index to next message to display - let index=0; + let index = 0; // are we randomizing if(this.config.random){ // yes index = this.randomIndex(compliments); } else{ - // no, sequetial - // if doing sequential, don't fall off the end + // no, sequential + // if doing sequential, don't fall off the end index = (this.lastIndexUsed >= (compliments.length-1))?0: ++this.lastIndexUsed; } - return compliments[index]; + return compliments[index] || ""; }, // Override dom generator. @@ -173,9 +184,9 @@ Module.register("compliments", { // get the compliment text var complimentText = this.randomCompliment(); // split it into parts on newline text - var parts= complimentText.split("\n"); + var parts = complimentText.split("\n"); // create a span to hold it all - var compliment=document.createElement("span"); + var compliment = document.createElement("span"); // process all the parts of the compliment text for (part of parts){ // create a text element for each part diff --git a/tests/configs/modules/compliments/compliments_date.js b/tests/configs/modules/compliments/compliments_date.js new file mode 100644 index 00000000..7ea984b1 --- /dev/null +++ b/tests/configs/modules/compliments/compliments_date.js @@ -0,0 +1,41 @@ +/* Magic Mirror Test config compliments with date type + * + * By Rejas + * + * MIT Licensed. + */ + +let config = { + port: 8080, + ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], + + language: "en", + timeFormat: 12, + units: "metric", + electronOptions: { + webPreferences: { + nodeIntegration: true, + }, + }, + + modules: [ + { + module: "compliments", + position: "middle_center", + config: { + mockDate: "2020-01-01", + compliments: { + morning: [], + afternoon: [], + evening: [], + "....-01-01": [ + "Happy new year!" + ] + } + } + } + ] +}; + +/*************** DO NOT EDIT THE LINE BELOW ***************/ +if (typeof module !== "undefined") {module.exports = config;} diff --git a/tests/e2e/modules/compliments_spec.js b/tests/e2e/modules/compliments_spec.js index 35529b19..97fd0ed9 100644 --- a/tests/e2e/modules/compliments_spec.js +++ b/tests/e2e/modules/compliments_spec.js @@ -89,4 +89,19 @@ describe("Compliments module", function() { }); }); }); + + describe("Feature date in compliments module", function() { + describe("Set date and empty compliments for anytime, morning, evening and afternoon", function() { + before(function() { + // Set config sample for use in test + process.env.MM_CONFIG_FILE = "tests/configs/modules/compliments/compliments_date.js"; + }); + + it("Show happy new year compliment on new years day", function() { + return app.client.waitUntilWindowLoaded().getText(".compliments").then(function(text) { + expect(text).to.be.oneOf(["Happy new year!"]); + }); + }); + }); + }); });