Chore: homepage tests (#6278)

This commit is contained in:
shamoon
2026-02-04 19:58:39 -08:00
committed by GitHub
parent 7d019185a3
commit 872a3600aa
558 changed files with 32606 additions and 84 deletions

View File

@@ -0,0 +1,67 @@
// @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";
const { useWidgetAPI } = vi.hoisted(() => ({
useWidgetAPI: vi.fn(),
}));
vi.mock("utils/proxy/use-widget-api", () => ({
default: useWidgetAPI,
}));
import Component from "./component";
describe("widgets/unraid/component", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("defaults widget.fields and filters down to 4 visible blocks while loading", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
const service = { widget: { type: "unraid" } };
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
// Component sets default fields
expect(service.widget.fields).toEqual(["status", "cpu", "memoryPercent", "notifications"]);
// Container filters the many placeholder Blocks down to the selected fields.
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
expect(screen.getByText("unraid.status")).toBeInTheDocument();
expect(screen.getByText("unraid.cpu")).toBeInTheDocument();
expect(screen.getByText("unraid.notifications")).toBeInTheDocument();
expect(screen.getByText("unraid.memoryUsed")).toBeInTheDocument();
expect(screen.queryByText("unraid.memoryAvailable")).toBeNull();
});
it("renders values for the default fields", () => {
useWidgetAPI.mockReturnValue({
data: {
arrayState: "started",
cpuPercent: 12,
memoryAvailable: 100,
memoryUsed: 50,
memoryUsedPercent: 33,
unreadNotifications: 7,
arrayUsed: 1,
arrayFree: 2,
arrayUsedPercent: 3,
caches: {},
},
error: undefined,
});
const service = { widget: { type: "unraid" } };
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
expect(screen.getByText("unraid.started")).toBeInTheDocument();
expect(screen.getByText("12")).toBeInTheDocument();
expect(screen.getByText("33")).toBeInTheDocument();
expect(screen.getByText("7")).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,85 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import createMockRes from "test-utils/create-mock-res";
const { httpProxy, getServiceWidget, logger } = vi.hoisted(() => ({
httpProxy: vi.fn(),
getServiceWidget: vi.fn(),
logger: {
debug: vi.fn(),
error: vi.fn(),
},
}));
vi.mock("utils/logger", () => ({
default: () => logger,
}));
vi.mock("utils/config/service-helpers", () => ({
default: getServiceWidget,
}));
vi.mock("utils/proxy/http", () => ({
httpProxy,
}));
import unraidProxyHandler from "./proxy";
describe("widgets/unraid/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("calls the Unraid GraphQL endpoint and returns a flattened response", async () => {
getServiceWidget.mockResolvedValue({ url: "http://unraid", key: "k" });
httpProxy.mockResolvedValueOnce([
200,
"application/json",
Buffer.from(
JSON.stringify({
data: {
metrics: { memory: { active: 10, available: 90, percentTotal: 10 }, cpu: { percentTotal: 5 } },
notifications: { overview: { unread: { total: 2 } } },
array: {
state: "STARTED",
capacity: { kilobytes: { free: 10, used: 20, total: 40 } },
caches: [{ name: "cache", fsType: "btrfs", fsSize: 100, fsFree: 25, fsUsed: 75 }],
},
},
}),
),
]);
const req = { query: { group: "g", service: "svc", index: "0" } };
const res = createMockRes();
await unraidProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(1);
expect(httpProxy.mock.calls[0][0].toString()).toBe("http://unraid/graphql");
expect(res.statusCode).toBe(200);
expect(res.body).toEqual(
expect.objectContaining({
memoryUsedPercent: 10,
cpuPercent: 5,
unreadNotifications: 2,
arrayState: "STARTED",
}),
);
expect(res.body.caches.cache.fsUsedPercent).toBe(75);
});
it("returns 500 when the response cannot be processed", async () => {
getServiceWidget.mockResolvedValue({ url: "http://unraid", key: "k" });
httpProxy.mockResolvedValueOnce([200, "application/json", Buffer.from("not-json")]);
const req = { query: { group: "g", service: "svc", index: "0" } };
const res = createMockRes();
await unraidProxyHandler(req, res);
expect(res.statusCode).toBe(500);
expect(res.body).toEqual(expect.objectContaining({ error: expect.any(String) }));
});
});

View File

@@ -0,0 +1,12 @@
import { describe, expect, it } from "vitest";
import { expectWidgetConfigShape } from "test-utils/widget-config";
import widget from "./widget";
describe("unraid widget config", () => {
it("exports a valid widget config", () => {
expectWidgetConfigShape(widget);
expect(widget.api).toBeUndefined();
});
});