mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
Try out a mockdate class
This commit is contained in:
parent
b08f882324
commit
e2427fe299
@ -1,6 +1,6 @@
|
||||
const helpers = require("../global-setup");
|
||||
const expect = require("chai").expect;
|
||||
const moment = require("moment");
|
||||
const MockDate = require("./mocks/date.js");
|
||||
|
||||
const describe = global.describe;
|
||||
const it = global.it;
|
||||
@ -93,36 +93,20 @@ describe("Compliments module", function() {
|
||||
|
||||
describe("Feature date in compliments module", function() {
|
||||
describe("Set date and empty compliments for anytime, morning, evening and afternoon", function() {
|
||||
let RealDate;
|
||||
|
||||
before(function() {
|
||||
// Set config sample for use in test
|
||||
process.env.MM_CONFIG_FILE = "tests/configs/modules/compliments/compliments_date.js";
|
||||
|
||||
RealDate = Date;
|
||||
let customTimeMs = moment("2015-10-12T06:00:00.000Z").valueOf();
|
||||
|
||||
function MockDate() {
|
||||
return new RealDate(customTimeMs);
|
||||
}
|
||||
|
||||
MockDate.now = function () {
|
||||
return new MockDate().valueOf();
|
||||
};
|
||||
|
||||
MockDate.prototype = RealDate.prototype;
|
||||
|
||||
Date = MockDate;
|
||||
MockDate.set("2000-12-10");
|
||||
});
|
||||
|
||||
it("Show anytime because if configure empty parts of day compliments and set anytime compliments", function() {
|
||||
it("Show happy birthday compliment on special date", function() {
|
||||
return app.client.waitUntilWindowLoaded().getText(".compliments").then(function(text) {
|
||||
expect(text).to.be.oneOf(["Happy birthday, Ada Lovelace!"]);
|
||||
});
|
||||
});
|
||||
|
||||
after(function() {
|
||||
Date = RealDate;
|
||||
MockDate.reset();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
86
tests/e2e/modules/mocks/date.js
Normal file
86
tests/e2e/modules/mocks/date.js
Normal file
@ -0,0 +1,86 @@
|
||||
(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
|
||||
};
|
||||
|
||||
}));
|
Loading…
x
Reference in New Issue
Block a user