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
@@ -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";
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 blocks = Array.from(container.querySelectorAll(".service-block"));
const block = blocks.find((b) => b.textContent?.includes(label));
expect(block, `missing block for ${label}`).toBeTruthy();
expect(block.textContent).toContain(String(value));
}
describe("widgets/audiobookshelf/component", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("renders placeholders while loading", () => {
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
const { container } = renderWithProviders(<Component service={{ widget: { type: "audiobookshelf" } }} />, {
settings: { hideErrors: false },
});
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
expect(screen.getByText("audiobookshelf.podcasts")).toBeInTheDocument();
expect(screen.getByText("audiobookshelf.podcastsDuration")).toBeInTheDocument();
expect(screen.getByText("audiobookshelf.books")).toBeInTheDocument();
expect(screen.getByText("audiobookshelf.booksDuration")).toBeInTheDocument();
});
it("aggregates totals across libraries by mediaType", () => {
useWidgetAPI.mockReturnValue({
data: [
{ mediaType: "podcast", stats: { totalItems: "2", totalDuration: "100" } },
{ mediaType: "podcast", stats: { totalItems: "1", totalDuration: "200" } },
{ mediaType: "book", stats: { totalItems: "4", totalDuration: "300" } },
],
error: undefined,
});
const { container } = renderWithProviders(<Component service={{ widget: { type: "audiobookshelf" } }} />, {
settings: { hideErrors: false },
});
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
expectBlockValue(container, "audiobookshelf.podcasts", 3);
expectBlockValue(container, "audiobookshelf.podcastsDuration", 300);
expectBlockValue(container, "audiobookshelf.books", 4);
expectBlockValue(container, "audiobookshelf.booksDuration", 300);
});
});
+67
View File
@@ -0,0 +1,67 @@
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: {
audiobookshelf: {
api: "{url}/api/{endpoint}",
},
},
}));
import audiobookshelfProxyHandler from "./proxy";
describe("widgets/audiobookshelf/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("retrieves libraries and per-library stats", async () => {
getServiceWidget.mockResolvedValue({ type: "audiobookshelf", url: "http://abs", key: "k" });
httpProxy
.mockResolvedValueOnce([
200,
"application/json",
Buffer.from(
JSON.stringify({
libraries: [
{ id: "l1", name: "A" },
{ id: "l2", name: "B" },
],
}),
),
])
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ total: 1 }))])
.mockResolvedValueOnce([200, "application/json", Buffer.from(JSON.stringify({ total: 2 }))]);
const req = { query: { group: "g", service: "svc", endpoint: "libraries", index: "0" } };
const res = createMockRes();
await audiobookshelfProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(3);
expect(httpProxy.mock.calls[0][1].headers.Authorization).toBe("Bearer k");
expect(res.statusCode).toBe(200);
expect(res.body).toEqual([
{ id: "l1", name: "A", stats: { total: 1 } },
{ id: "l2", name: "B", stats: { total: 2 } },
]);
});
});
+12
View File
@@ -0,0 +1,12 @@
import { describe, expect, it } from "vitest";
import { expectWidgetConfigShape } from "test-utils/widget-config";
import widget from "./widget";
describe("audiobookshelf widget config", () => {
it("exports a valid widget config", () => {
expectWidgetConfigShape(widget);
expect(widget.mappings?.libraries?.endpoint).toBe("libraries");
});
});