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..ca275d27 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, @@ -102,6 +105,7 @@ Module.register("compliments", { */ complimentArray: function() { var hour = moment().hour(); + var date = moment().format("YYYY-MM-DD"); var compliments; if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) { @@ -122,6 +126,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 +161,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 +183,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..649d789b --- /dev/null +++ b/tests/configs/modules/compliments/compliments_date.js @@ -0,0 +1,40 @@ +/* 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: { + 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..d7828268 100644 --- a/tests/e2e/modules/compliments_spec.js +++ b/tests/e2e/modules/compliments_spec.js @@ -1,5 +1,6 @@ const helpers = require("../global-setup"); const expect = require("chai").expect; +const MockDate = require("./mocks/date.js"); const describe = global.describe; const it = global.it; @@ -89,4 +90,24 @@ 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"; + MockDate.set("2000-01-01"); + }); + + 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!"]); + }); + }); + + after(function() { + MockDate.reset(); + }); + }); + }); }); diff --git a/tests/e2e/modules/mocks/date.js b/tests/e2e/modules/mocks/date.js new file mode 100644 index 00000000..35113566 --- /dev/null +++ b/tests/e2e/modules/mocks/date.js @@ -0,0 +1,93 @@ +/** + * MockDate + * + * By Bob Lauer (https://github.com/boblauer/MockDate + * MIT Licensed. +*/ + +(function(name, definition) { + if (typeof module !== "undefined") {module.exports = definition();} + else if (typeof define === "function" && typeof define.amd === "object") {define(definition);} + else {this[name] = definition();} +}("MockDate", function() { + "use strict"; + + var _Date = Date + , _getTimezoneOffset = Date.prototype.getTimezoneOffset + , now = null + ; + + function MockDate(y, m, d, h, M, s, ms) { + var date; + + switch (arguments.length) { + + case 0: + if (now !== null) { + date = new _Date(now); + } else { + date = new _Date(); + } + break; + + case 1: + date = new _Date(y); + break; + + default: + d = typeof d === "undefined" ? 1 : d; + h = h || 0; + M = M || 0; + s = s || 0; + ms = ms || 0; + date = new _Date(y, m, d, h, M, s, ms); + break; + } + + return date; + } + + MockDate.UTC = _Date.UTC; + + MockDate.now = function() { + return new MockDate().valueOf(); + }; + + MockDate.parse = function(dateString) { + return _Date.parse(dateString); + }; + + MockDate.toString = function() { + return _Date.toString(); + }; + + MockDate.prototype = _Date.prototype; + + function set(date, timezoneOffset) { + var dateObj = new Date(date); + if (isNaN(dateObj.getTime())) { + throw new TypeError("mockdate: The time set is an invalid date: " + date); + } + + if (typeof timezoneOffset === "number") { + MockDate.prototype.getTimezoneOffset = function() { + return timezoneOffset; + }; + } + + Date = MockDate; + + now = dateObj.valueOf(); + } + + function reset() { + Date = _Date; + Date.prototype.getTimezoneOffset = _getTimezoneOffset; + } + + return { + set: set, + reset: reset + }; + +}));