mirror of
https://github.com/gethomepage/homepage.git
synced 2026-04-09 11:41:20 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
95
src/widgets/urbackup/component.test.jsx
Normal file
95
src/widgets/urbackup/component.test.jsx
Normal file
@@ -0,0 +1,95 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { screen } from "@testing-library/react";
|
||||
import { afterEach, 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/urbackup/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2020-01-01T00:00:00Z"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("renders placeholders while loading (optionally includes totalUsed)", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<Component service={{ widget: { type: "urbackup", fields: ["ok", "errored", "noRecent", "totalUsed"] } }} />,
|
||||
{
|
||||
settings: { hideErrors: false },
|
||||
},
|
||||
);
|
||||
|
||||
// Container filters children by widget.fields.
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
expect(screen.getByText("urbackup.ok")).toBeInTheDocument();
|
||||
expect(screen.getByText("urbackup.errored")).toBeInTheDocument();
|
||||
expect(screen.getByText("urbackup.noRecent")).toBeInTheDocument();
|
||||
expect(screen.getByText("urbackup.totalUsed")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders ok/errored/noRecent and totalUsed when loaded", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: {
|
||||
maxDays: 3,
|
||||
clientStatuses: [
|
||||
// ok
|
||||
{
|
||||
lastbackup: 1577836800,
|
||||
lastbackup_image: 1577836800,
|
||||
file_ok: true,
|
||||
image_ok: true,
|
||||
image_not_supported: false,
|
||||
image_disabled: false,
|
||||
},
|
||||
// errored
|
||||
{
|
||||
lastbackup: 1577836800,
|
||||
lastbackup_image: 1577836800,
|
||||
file_ok: false,
|
||||
image_ok: true,
|
||||
image_not_supported: false,
|
||||
image_disabled: false,
|
||||
},
|
||||
// no recent
|
||||
{
|
||||
lastbackup: 0,
|
||||
lastbackup_image: 0,
|
||||
file_ok: true,
|
||||
image_ok: true,
|
||||
image_not_supported: false,
|
||||
image_disabled: false,
|
||||
},
|
||||
],
|
||||
diskUsage: [{ used: 1 }, { used: 2 }],
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const service = { widget: { type: "urbackup", fields: ["ok", "errored", "noRecent", "totalUsed"] } };
|
||||
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||
|
||||
expectBlockValue(container, "urbackup.ok", 1);
|
||||
expectBlockValue(container, "urbackup.errored", 1);
|
||||
expectBlockValue(container, "urbackup.noRecent", 1);
|
||||
expectBlockValue(container, "urbackup.totalUsed", 3);
|
||||
});
|
||||
});
|
||||
120
src/widgets/urbackup/proxy.test.js
Normal file
120
src/widgets/urbackup/proxy.test.js
Normal file
@@ -0,0 +1,120 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { UrbackupServer, state, getServiceWidget } = vi.hoisted(() => {
|
||||
const state = { instances: [] };
|
||||
|
||||
const UrbackupServer = vi.fn((opts) => {
|
||||
const instance = {
|
||||
opts,
|
||||
getStatus: vi.fn(),
|
||||
getUsage: vi.fn(),
|
||||
};
|
||||
state.instances.push(instance);
|
||||
return instance;
|
||||
});
|
||||
|
||||
return {
|
||||
UrbackupServer,
|
||||
state,
|
||||
getServiceWidget: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("urbackup-server-api", () => ({
|
||||
UrbackupServer,
|
||||
}));
|
||||
|
||||
vi.mock("utils/config/service-helpers", () => ({
|
||||
default: getServiceWidget,
|
||||
}));
|
||||
|
||||
import urbackupProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/urbackup/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
state.instances.length = 0;
|
||||
});
|
||||
|
||||
it("returns client statuses and maxDays without disk usage by default", async () => {
|
||||
getServiceWidget.mockResolvedValue({
|
||||
url: "http://ur",
|
||||
username: "u",
|
||||
password: "p",
|
||||
maxDays: 5,
|
||||
});
|
||||
|
||||
UrbackupServer.mockImplementationOnce((opts) => {
|
||||
const instance = {
|
||||
opts,
|
||||
getStatus: vi.fn().mockResolvedValue([{ id: 1 }]),
|
||||
getUsage: vi.fn(),
|
||||
};
|
||||
state.instances.push(instance);
|
||||
return instance;
|
||||
});
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await urbackupProxyHandler(req, res);
|
||||
|
||||
expect(UrbackupServer).toHaveBeenCalledWith({ url: "http://ur", username: "u", password: "p" });
|
||||
expect(state.instances[0].getUsage).not.toHaveBeenCalled();
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toEqual({ clientStatuses: [{ id: 1 }], diskUsage: false, maxDays: 5 });
|
||||
});
|
||||
|
||||
it("fetches disk usage when requested via fields", async () => {
|
||||
getServiceWidget.mockResolvedValue({
|
||||
url: "http://ur",
|
||||
username: "u",
|
||||
password: "p",
|
||||
maxDays: 1,
|
||||
fields: ["totalUsed"],
|
||||
});
|
||||
|
||||
UrbackupServer.mockImplementationOnce((opts) => {
|
||||
const instance = {
|
||||
opts,
|
||||
getStatus: vi.fn().mockResolvedValue([{ id: 1 }]),
|
||||
getUsage: vi.fn().mockResolvedValue({ totalUsed: 123 }),
|
||||
};
|
||||
state.instances.push(instance);
|
||||
return instance;
|
||||
});
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await urbackupProxyHandler(req, res);
|
||||
|
||||
expect(state.instances[0].getUsage).toHaveBeenCalled();
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body.diskUsage).toEqual({ totalUsed: 123 });
|
||||
});
|
||||
|
||||
it("returns 500 on server errors", async () => {
|
||||
getServiceWidget.mockResolvedValue({ url: "http://ur", username: "u", password: "p" });
|
||||
|
||||
UrbackupServer.mockImplementationOnce((opts) => {
|
||||
const instance = {
|
||||
opts,
|
||||
getStatus: vi.fn().mockRejectedValue(new Error("nope")),
|
||||
getUsage: vi.fn(),
|
||||
};
|
||||
state.instances.push(instance);
|
||||
return instance;
|
||||
});
|
||||
|
||||
const req = { query: { group: "g", service: "svc", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await urbackupProxyHandler(req, res);
|
||||
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.body).toEqual({ error: "Error communicating with UrBackup server" });
|
||||
});
|
||||
});
|
||||
11
src/widgets/urbackup/widget.test.js
Normal file
11
src/widgets/urbackup/widget.test.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
|
||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||
|
||||
import widget from "./widget";
|
||||
|
||||
describe("urbackup widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user