mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-22 13:09:26 +00:00
Refactor formatTime into util class (#3073)
While looking at https://github.com/MichMich/MagicMirror/pull/3070 I noticed that the weather and clock module do some formatTime stuff, so why not use a common function for that? --------- Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
const { performWebRequest } = require("../../../../modules/default/utils");
|
||||
global.moment = require("moment-timezone");
|
||||
const { performWebRequest, formatTime } = require("../../../../modules/default/utils");
|
||||
|
||||
const nodeVersion = process.version.match(/^v(\d+)\.*/)[1];
|
||||
|
||||
describe("Utils tests", () => {
|
||||
describe("Default modules utils tests", () => {
|
||||
describe("The performWebRequest-method", () => {
|
||||
if (nodeVersion > 18) {
|
||||
const locationHost = "localhost:8080";
|
||||
@@ -109,4 +110,78 @@ describe("Utils tests", () => {
|
||||
test("Always ok, need one test", () => {});
|
||||
}
|
||||
});
|
||||
|
||||
describe("formatTime", () => {
|
||||
const time = new Date();
|
||||
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers();
|
||||
moment.tz.setDefault("Europe/Berlin");
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.setSystemTime(new Date("2023-01-01 13:13"));
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
jest.setSystemTime(new Date());
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it("should convert correctly according to the config", () => {
|
||||
time.setHours(13, 13);
|
||||
expect(
|
||||
formatTime(
|
||||
{
|
||||
timeFormat: 24
|
||||
},
|
||||
time
|
||||
)
|
||||
).toBe("13:13");
|
||||
expect(
|
||||
formatTime(
|
||||
{
|
||||
showPeriod: true,
|
||||
showPeriodUpper: true,
|
||||
timeFormat: 12
|
||||
},
|
||||
time
|
||||
)
|
||||
).toBe("1:13 PM");
|
||||
expect(
|
||||
formatTime(
|
||||
{
|
||||
showPeriod: true,
|
||||
showPeriodUpper: false,
|
||||
timeFormat: 12
|
||||
},
|
||||
time
|
||||
)
|
||||
).toBe("1:13 pm");
|
||||
expect(
|
||||
formatTime(
|
||||
{
|
||||
showPeriod: false,
|
||||
timeFormat: 12
|
||||
},
|
||||
time
|
||||
)
|
||||
).toBe("1:13");
|
||||
});
|
||||
|
||||
it("should convert correctly into another timezone", () => {
|
||||
expect(
|
||||
formatTime(
|
||||
{
|
||||
timeFormat: 24,
|
||||
timezone: "America/Toronto"
|
||||
},
|
||||
time
|
||||
)
|
||||
).toBe("07:13");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -7,7 +7,7 @@ describe("Weather utils tests", () => {
|
||||
const units = ["mm", "cm"];
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
var result = weather.convertPrecipitationUnit(values[i], units[i], undefined);
|
||||
const result = weather.convertPrecipitationUnit(values[i], units[i], undefined);
|
||||
expect(result).toBe(`${values[i].toFixed(2)} ${units[i]}`);
|
||||
}
|
||||
});
|
||||
@@ -17,7 +17,7 @@ describe("Weather utils tests", () => {
|
||||
const units = ["mm", "cm"];
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
var result = weather.convertPrecipitationUnit(values[i], units[i], "metric");
|
||||
const result = weather.convertPrecipitationUnit(values[i], units[i], "metric");
|
||||
expect(result).toBe(`${values[i].toFixed(2)} ${units[i]}`);
|
||||
}
|
||||
});
|
||||
@@ -26,7 +26,7 @@ describe("Weather utils tests", () => {
|
||||
const values = [1, 2];
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
var result = weather.convertPrecipitationUnit(values[i], undefined, "metric");
|
||||
const result = weather.convertPrecipitationUnit(values[i], undefined, "metric");
|
||||
expect(result).toBe(`${values[i].toFixed(2)} mm`);
|
||||
}
|
||||
});
|
||||
@@ -37,7 +37,7 @@ describe("Weather utils tests", () => {
|
||||
const expectedValues = [0.04, 0.79];
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
var result = weather.convertPrecipitationUnit(values[i], units[i], "imperial");
|
||||
const result = weather.convertPrecipitationUnit(values[i], units[i], "imperial");
|
||||
expect(result).toBe(`${expectedValues[i]} in`);
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user