mirror of
https://github.com/gethomepage/homepage.git
synced 2026-04-14 06:01:20 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
80
src/widgets/komodo/component.test.jsx
Normal file
80
src/widgets/komodo/component.test.jsx
Normal file
@@ -0,0 +1,80 @@
|
||||
// @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";
|
||||
|
||||
describe("widgets/komodo/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("defaults fields for stacks view and skips containers endpoint when showStacks=true and showSummary=false", () => {
|
||||
useWidgetAPI
|
||||
.mockReturnValueOnce({ data: undefined, error: undefined }) // containers (disabled)
|
||||
.mockReturnValueOnce({ data: undefined, error: undefined }) // stacks
|
||||
.mockReturnValueOnce({ data: undefined, error: undefined }); // servers (disabled)
|
||||
|
||||
const service = { widget: { type: "komodo", showStacks: true, showSummary: false } };
|
||||
const { container } = renderWithProviders(<Component service={service} />, { settings: { hideErrors: false } });
|
||||
|
||||
expect(service.widget.fields).toEqual(["total", "running", "down", "unhealthy"]);
|
||||
expect(useWidgetAPI.mock.calls[0][1]).toBe(""); // containersEndpoint
|
||||
expect(useWidgetAPI.mock.calls[1][1]).toBe("stacks");
|
||||
expect(useWidgetAPI.mock.calls[2][1]).toBe(""); // serversEndpoint
|
||||
|
||||
// Default fields filter out "unknown" which is rendered but not in widget.fields.
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
expect(screen.getByText("komodo.total")).toBeInTheDocument();
|
||||
expect(screen.getByText("komodo.running")).toBeInTheDocument();
|
||||
expect(screen.getByText("komodo.down")).toBeInTheDocument();
|
||||
expect(screen.getByText("komodo.unhealthy")).toBeInTheDocument();
|
||||
expect(screen.queryByText("komodo.unknown")).toBeNull();
|
||||
});
|
||||
|
||||
it("renders computed down=stopped+down for stacks view", () => {
|
||||
useWidgetAPI
|
||||
.mockReturnValueOnce({ data: undefined, error: undefined }) // containers (disabled)
|
||||
.mockReturnValueOnce({
|
||||
data: { total: 10, running: 7, stopped: 1, down: 2, unhealthy: 3, unknown: 4 },
|
||||
error: undefined,
|
||||
})
|
||||
.mockReturnValueOnce({ data: undefined, error: undefined }); // servers (disabled)
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<Component service={{ widget: { type: "komodo", showStacks: true, showSummary: false } }} />,
|
||||
{ settings: { hideErrors: false } },
|
||||
);
|
||||
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
expect(screen.getByText("10")).toBeInTheDocument();
|
||||
expect(screen.getByText("7")).toBeInTheDocument();
|
||||
const downBlock = findServiceBlockByLabel(container, "komodo.down");
|
||||
expect(downBlock).toBeTruthy();
|
||||
expect(downBlock.textContent).toContain("3"); // stopped(1) + down(2)
|
||||
});
|
||||
|
||||
it("renders summary view ratios when showSummary=true", () => {
|
||||
useWidgetAPI
|
||||
.mockReturnValueOnce({ data: { total: 5, running: 4 }, error: undefined }) // containers
|
||||
.mockReturnValueOnce({ data: { total: 2, running: 1 }, error: undefined }) // stacks
|
||||
.mockReturnValueOnce({ data: { total: 1, healthy: 1 }, error: undefined }); // servers
|
||||
|
||||
const { container } = renderWithProviders(
|
||||
<Component service={{ widget: { type: "komodo", showSummary: true } }} />,
|
||||
{ settings: { hideErrors: false } },
|
||||
);
|
||||
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(3);
|
||||
expect(screen.getByText("1 / 1")).toBeInTheDocument();
|
||||
expect(screen.getByText("1 / 2")).toBeInTheDocument();
|
||||
expect(screen.getByText("4 / 5")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
72
src/widgets/komodo/proxy.test.js
Normal file
72
src/widgets/komodo/proxy.test.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { httpProxy, getServiceWidget, validateWidgetData, logger } = vi.hoisted(() => ({
|
||||
httpProxy: vi.fn(),
|
||||
getServiceWidget: vi.fn(),
|
||||
validateWidgetData: vi.fn(() => true),
|
||||
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("utils/proxy/validate-widget-data", () => ({
|
||||
default: validateWidgetData,
|
||||
}));
|
||||
vi.mock("widgets/widgets", () => ({
|
||||
default: {
|
||||
komodo: {
|
||||
api: "{url}/{endpoint}",
|
||||
mappings: {
|
||||
stats: { body: { hello: "world" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import komodoProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/komodo/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
validateWidgetData.mockReturnValue(true);
|
||||
});
|
||||
|
||||
it("POSTs to the unified read endpoint with API key/secret", async () => {
|
||||
getServiceWidget.mockResolvedValue({ type: "komodo", url: "http://komodo", key: "k", secret: "s" });
|
||||
httpProxy.mockResolvedValueOnce([200, "application/json", Buffer.from("ok")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "stats", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await komodoProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy.mock.calls[0][0]).toBe("http://komodo/read");
|
||||
expect(httpProxy.mock.calls[0][1].headers["X-API-Key"]).toBe("k");
|
||||
expect(httpProxy.mock.calls[0][1].headers["X-API-Secret"]).toBe("s");
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toEqual(Buffer.from("ok"));
|
||||
});
|
||||
|
||||
it("returns 500 when data validation fails", async () => {
|
||||
validateWidgetData.mockReturnValue(false);
|
||||
getServiceWidget.mockResolvedValue({ type: "komodo", url: "http://komodo", key: "k", secret: "s" });
|
||||
httpProxy.mockResolvedValueOnce([200, "application/json", Buffer.from("bad")]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "stats", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await komodoProxyHandler(req, res);
|
||||
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.body.error.message).toBe("Invalid data");
|
||||
});
|
||||
});
|
||||
11
src/widgets/komodo/widget.test.js
Normal file
11
src/widgets/komodo/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("komodo widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user