Fix date test.

This commit is contained in:
Michael Teeuw
2020-04-01 10:57:50 +02:00
parent 66642a19c5
commit 2464d01891
4 changed files with 4 additions and 101 deletions

View File

@@ -40,7 +40,8 @@ Module.register("compliments", {
morningEndTime: 12,
afternoonStartTime: 12,
afternoonEndTime: 17,
random: true
random: true,
mockDate: null
},
lastIndexUsed:-1,
// Set currentweather from module
@@ -105,7 +106,7 @@ Module.register("compliments", {
*/
complimentArray: function() {
var hour = moment().hour();
var date = moment().format("YYYY-MM-DD");
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")) {

View File

@@ -23,6 +23,7 @@ let config = {
module: "compliments",
position: "middle_center",
config: {
mockDate: "2020-01-01",
compliments: {
morning: [],
afternoon: [],

View File

@@ -1,6 +1,5 @@
const helpers = require("../global-setup");
const expect = require("chai").expect;
const MockDate = require("./mocks/date.js");
const describe = global.describe;
const it = global.it;
@@ -96,7 +95,6 @@ describe("Compliments module", 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() {
@@ -104,10 +102,6 @@ describe("Compliments module", function() {
expect(text).to.be.oneOf(["Happy new year!"]);
});
});
after(function() {
MockDate.reset();
});
});
});
});

View File

@@ -1,93 +0,0 @@
/**
* 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
};
}));