test: cover middleware + core utils (logger, hooks, proxy)

This commit is contained in:
shamoon
2026-02-04 09:23:36 -08:00
parent bcdd4166a3
commit dfb25a2d61
6 changed files with 371 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const { useSWR } = vi.hoisted(() => ({ useSWR: vi.fn() }));
vi.mock("swr", () => ({
default: useSWR,
}));
import useWidgetAPI from "./use-widget-api";
describe("utils/proxy/use-widget-api", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("formats the proxy url and passes refreshInterval when provided in options", () => {
useSWR.mockReturnValue({ data: { ok: true }, error: undefined, mutate: "m" });
const widget = { service_group: "g", service_name: "s", index: 0 };
const result = useWidgetAPI(widget, "status", { refreshInterval: 123, foo: "bar" });
expect(useSWR).toHaveBeenCalledWith(
expect.stringContaining("/api/services/proxy?"),
expect.objectContaining({ refreshInterval: 123 }),
);
expect(result.data).toEqual({ ok: true });
expect(result.error).toBeUndefined();
expect(result.mutate).toBe("m");
});
it("returns data.error as the top-level error", () => {
const dataError = { message: "nope" };
useSWR.mockReturnValue({ data: { error: dataError }, error: undefined, mutate: vi.fn() });
const widget = { service_group: "g", service_name: "s", index: 0 };
const result = useWidgetAPI(widget, "status", {});
expect(result.error).toBe(dataError);
});
it("disables the request when endpoint is an empty string", () => {
useSWR.mockReturnValue({ data: undefined, error: undefined, mutate: vi.fn() });
const widget = { service_group: "g", service_name: "s", index: 0 };
useWidgetAPI(widget, "");
expect(useSWR).toHaveBeenCalledWith(null, {});
});
});