mirror of
https://github.com/gethomepage/homepage.git
synced 2026-04-05 09:41:21 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
62
src/widgets/filebrowser/component.test.jsx
Normal file
62
src/widgets/filebrowser/component.test.jsx
Normal file
@@ -0,0 +1,62 @@
|
||||
// @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/filebrowser/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders placeholders while loading", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<Component service={{ widget: { type: "filebrowser", url: "http://x" } }} />,
|
||||
{ settings: { hideErrors: false } },
|
||||
);
|
||||
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(3);
|
||||
expect(screen.getByText("filebrowser.available")).toBeInTheDocument();
|
||||
expect(screen.getByText("filebrowser.used")).toBeInTheDocument();
|
||||
expect(screen.getByText("filebrowser.total")).toBeInTheDocument();
|
||||
expect(screen.getAllByText("-")).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("renders error UI when widget API errors", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: { message: "nope" } });
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "filebrowser", url: "http://x" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("renders computed available/used/total bytes", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: { total: 100, used: 40 }, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<Component service={{ widget: { type: "filebrowser", url: "http://x" } }} />,
|
||||
{ settings: { hideErrors: false } },
|
||||
);
|
||||
|
||||
expectBlockValue(container, "filebrowser.available", 60);
|
||||
expectBlockValue(container, "filebrowser.used", 40);
|
||||
expectBlockValue(container, "filebrowser.total", 100);
|
||||
});
|
||||
});
|
||||
79
src/widgets/filebrowser/proxy.test.js
Normal file
79
src/widgets/filebrowser/proxy.test.js
Normal file
@@ -0,0 +1,79 @@
|
||||
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,
|
||||
}));
|
||||
|
||||
vi.mock("widgets/widgets", () => ({
|
||||
default: {
|
||||
filebrowser: {
|
||||
api: "{url}/{endpoint}",
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import filebrowserProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/filebrowser/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("logs in and uses X-AUTH token for subsequent requests", async () => {
|
||||
getServiceWidget.mockResolvedValue({
|
||||
type: "filebrowser",
|
||||
url: "http://fb",
|
||||
username: "u",
|
||||
password: "p",
|
||||
authHeader: "X-User",
|
||||
});
|
||||
|
||||
httpProxy
|
||||
.mockResolvedValueOnce([200, "text/plain", "token123"])
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from("data")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "api/raw", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await filebrowserProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy).toHaveBeenCalledTimes(2);
|
||||
expect(httpProxy.mock.calls[0][0]).toBe("http://fb/login");
|
||||
expect(httpProxy.mock.calls[0][1].headers).toEqual({ "X-User": "u" });
|
||||
expect(httpProxy.mock.calls[1][1].headers["X-AUTH"]).toBe("token123");
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toEqual(Buffer.from("data"));
|
||||
});
|
||||
|
||||
it("returns 500 when login fails", async () => {
|
||||
getServiceWidget.mockResolvedValue({ type: "filebrowser", url: "http://fb", username: "u", password: "p" });
|
||||
httpProxy.mockResolvedValueOnce([401, "text/plain", "nope"]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "api/raw", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await filebrowserProxyHandler(req, res);
|
||||
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.body).toEqual({ error: "Failed to authenticate with Filebrowser" });
|
||||
});
|
||||
});
|
||||
11
src/widgets/filebrowser/widget.test.js
Normal file
11
src/widgets/filebrowser/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("filebrowser widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user