Files
MagicMirror/tests/unit/modules/default/utils_spec.js
T

155 lines
4.0 KiB
JavaScript
Raw Normal View History

2023-07-01 21:17:31 +02:00
global.moment = require("moment-timezone");
const { performWebRequest, formatTime } = require("../../../../modules/default/utils");
2023-04-04 20:44:32 +02:00
2023-07-01 21:17:31 +02:00
describe("Default modules utils tests", () => {
describe("performWebRequest", () => {
2023-10-01 20:13:41 +02:00
const locationHost = "localhost:8080";
const locationProtocol = "http";
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
let fetchResponse;
let fetchMock;
let urlToCall;
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
beforeEach(() => {
fetchResponse = new Response();
global.fetch = jest.fn(() => Promise.resolve(fetchResponse));
fetchMock = global.fetch;
});
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
describe("When using cors proxy", () => {
Object.defineProperty(global, "location", {
value: {
host: locationHost,
protocol: locationProtocol
}
});
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
it("Calls correct URL once", async () => {
2023-10-01 20:13:41 +02:00
urlToCall = "http://www.test.com/path?param1=value1";
await performWebRequest(urlToCall, "json", true);
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-10-01 20:13:41 +02:00
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?url=${urlToCall}`);
});
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
it("Sends correct headers", async () => {
2023-10-01 20:13:41 +02:00
urlToCall = "http://www.test.com/path?param1=value1";
2023-07-01 21:17:31 +02:00
2023-10-01 20:13:41 +02:00
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
await performWebRequest(urlToCall, "json", true, headers);
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-10-01 20:13:41 +02:00
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?sendheaders=header1:value1,header2:value2&url=${urlToCall}`);
2023-01-01 18:09:08 +01:00
});
2023-10-01 20:13:41 +02:00
});
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
describe("When not using cors proxy", () => {
2024-01-01 15:38:08 +01:00
it("Calls correct URL once", async () => {
2023-10-01 20:13:41 +02:00
urlToCall = "http://www.test.com/path?param1=value1";
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
await performWebRequest(urlToCall);
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-10-01 20:13:41 +02:00
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
});
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
it("Sends correct headers", async () => {
2023-10-01 20:13:41 +02:00
urlToCall = "http://www.test.com/path?param1=value1";
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
await performWebRequest(urlToCall, "json", false, headers);
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
const expectedHeaders = { headers: { header1: "value1", header2: "value2" } };
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-10-01 20:13:41 +02:00
expect(fetchMock.mock.calls[0][1]).toStrictEqual(expectedHeaders);
2023-01-01 18:09:08 +01:00
});
2023-10-01 20:13:41 +02:00
});
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
describe("When receiving json format", () => {
2024-01-01 15:38:08 +01:00
it("Returns undefined when no data is received", async () => {
2023-10-01 20:13:41 +02:00
urlToCall = "www.test.com";
2023-07-01 21:17:31 +02:00
2023-10-01 20:13:41 +02:00
const response = await performWebRequest(urlToCall);
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
expect(response).toBeUndefined();
2023-10-01 20:13:41 +02:00
});
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
it("Returns object when data is received", async () => {
2023-10-01 20:13:41 +02:00
urlToCall = "www.test.com";
2024-01-01 15:38:08 +01:00
fetchResponse = new Response("{\"body\": \"some content\"}");
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
const response = await performWebRequest(urlToCall);
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
expect(response.body).toBe("some content");
});
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
it("Returns expected headers when data is received", async () => {
2023-10-01 20:13:41 +02:00
urlToCall = "www.test.com";
2024-01-01 15:38:08 +01:00
fetchResponse = new Response("{\"body\": \"some content\"}", { headers: { header1: "value1", header2: "value2" } });
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
const response = await performWebRequest(urlToCall, "json", false, undefined, ["header1"]);
2023-01-01 18:09:08 +01:00
2024-01-01 15:38:08 +01:00
expect(response.headers).toHaveLength(1);
2023-10-01 20:13:41 +02:00
expect(response.headers[0].name).toBe("header1");
expect(response.headers[0].value).toBe("value1");
2023-01-01 18:09:08 +01:00
});
2023-10-01 20:13:41 +02:00
});
2023-01-01 18:09:08 +01:00
});
2023-07-01 21:17:31 +02:00
describe("formatTime", () => {
const time = new Date();
beforeEach(async () => {
time.setHours(13, 13);
});
it("should convert correctly according to the config", () => {
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");
});
});
2023-01-01 18:09:08 +01:00
});