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:
Veeck
2023-04-07 23:11:54 +02:00
committed by GitHub
parent 2c7beeaaaf
commit 32192d1698
8 changed files with 129 additions and 40 deletions

View File

@@ -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");
});
});
});