mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-29 07:21:16 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { screen } from "@testing-library/react";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { renderWithProviders } from "test-utils/render-with-providers";
|
||||
import { findServiceBlockByLabel } from "test-utils/widget-assertions";
|
||||
|
||||
const { useWidgetAPI } = vi.hoisted(() => ({ useWidgetAPI: vi.fn() }));
|
||||
vi.mock("utils/proxy/use-widget-api", () => ({ default: useWidgetAPI }));
|
||||
|
||||
import Component from "./component";
|
||||
|
||||
function expectBlockValue(container, label, value) {
|
||||
const block = findServiceBlockByLabel(container, label);
|
||||
expect(block, `missing block for ${label}`).toBeTruthy();
|
||||
expect(block.textContent).toContain(String(value));
|
||||
}
|
||||
|
||||
describe("widgets/qnap/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders placeholders while loading", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "qnap" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
expect(screen.getByText("qnap.cpuUsage")).toBeInTheDocument();
|
||||
expect(screen.getByText("qnap.memUsage")).toBeInTheDocument();
|
||||
expect(screen.getByText("qnap.systemTempC")).toBeInTheDocument();
|
||||
expect(screen.getByText("qnap.poolUsage")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders computed mem and pool usage for multi-volume payload", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: {
|
||||
system: {
|
||||
cpu_usage: { _cdata: "50 %" },
|
||||
total_memory: { _cdata: 100 },
|
||||
free_memory: { _cdata: 25 },
|
||||
sys_tempc: { _text: 40 },
|
||||
},
|
||||
volume: {
|
||||
volumeUseList: { volumeUse: [{ total_size: { _cdata: "100" }, free_size: { _cdata: "50" } }] },
|
||||
volumeList: { volume: [{ volumeLabel: { _cdata: "DataVol1" } }] },
|
||||
},
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "qnap" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expectBlockValue(container, "qnap.cpuUsage", "50");
|
||||
// mem% = ((100-25)/100)*100 = 75
|
||||
expectBlockValue(container, "qnap.memUsage", "75");
|
||||
// pool% = ((100-50)/100)*100 = 50
|
||||
expectBlockValue(container, "qnap.poolUsage", "50");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { httpProxy, getServiceWidget, cache, xml2json, logger } = vi.hoisted(() => {
|
||||
const store = new Map();
|
||||
return {
|
||||
httpProxy: vi.fn(),
|
||||
getServiceWidget: vi.fn(),
|
||||
cache: {
|
||||
get: vi.fn((k) => store.get(k)),
|
||||
put: vi.fn((k, v) => store.set(k, v)),
|
||||
del: vi.fn((k) => store.delete(k)),
|
||||
_reset: () => store.clear(),
|
||||
},
|
||||
xml2json: vi.fn((xml) => {
|
||||
if (xml === "login") {
|
||||
return JSON.stringify({ QDocRoot: { authSid: { _cdata: "sid1" } } });
|
||||
}
|
||||
if (xml === "system") {
|
||||
return JSON.stringify({
|
||||
QDocRoot: {
|
||||
authPassed: { _cdata: "1" },
|
||||
func: { ownContent: { root: { cpu: 1 } } },
|
||||
},
|
||||
});
|
||||
}
|
||||
if (xml === "volume") {
|
||||
return JSON.stringify({ QDocRoot: { authPassed: { _cdata: "1" }, volume: { ok: true } } });
|
||||
}
|
||||
return JSON.stringify({ QDocRoot: { authPassed: { _cdata: "1" } } });
|
||||
}),
|
||||
logger: { debug: vi.fn(), error: vi.fn() },
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("memory-cache", () => ({
|
||||
default: cache,
|
||||
...cache,
|
||||
}));
|
||||
vi.mock("xml-js", () => ({
|
||||
xml2json,
|
||||
}));
|
||||
vi.mock("utils/logger", () => ({
|
||||
default: () => logger,
|
||||
}));
|
||||
vi.mock("utils/config/service-helpers", () => ({
|
||||
default: getServiceWidget,
|
||||
}));
|
||||
vi.mock("utils/proxy/http", () => ({
|
||||
httpProxy,
|
||||
}));
|
||||
|
||||
import qnapProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/qnap/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
cache._reset();
|
||||
});
|
||||
|
||||
it("logs in and returns system + volume data", async () => {
|
||||
getServiceWidget.mockResolvedValue({ url: "http://qnap", username: "u", password: "p" });
|
||||
|
||||
httpProxy
|
||||
// login
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("login")])
|
||||
// system
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("system")])
|
||||
// volume
|
||||
.mockResolvedValueOnce([200, "application/xml", Buffer.from("volume")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await qnapProxyHandler(req, res);
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body.system).toEqual({ cpu: 1 });
|
||||
expect(res.body.volume).toEqual(expect.objectContaining({ authPassed: { _cdata: "1" } }));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
|
||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||
|
||||
import widget from "./widget";
|
||||
|
||||
describe("qnap widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user