83 lines
3.3 KiB
TypeScript
83 lines
3.3 KiB
TypeScript
// Guards the rebuilt "API-Aufrufe" table: rows render with the badge buckets the
|
|
// old SimpleBadge stack used, the empty state replaces the bare "no records"
|
|
// line, and the pagination footer only offers the steps that exist.
|
|
|
|
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import "@testing-library/jest-dom/vitest";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import type { ApiKeyLogDto } from "~/modules/api/dtos/ApiKeyLogDto";
|
|
import { ApiLogsPage, methodTone, speedLabelKey, statusTone } from "./ApiLogsPage";
|
|
|
|
vi.mock("react-i18next", () => ({
|
|
useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }),
|
|
}));
|
|
|
|
vi.mock("~/locale/format", () => ({
|
|
useLocaleFormatters: () => ({
|
|
number: (value: number) => String(value),
|
|
dateTime: (value: string | Date) => String(value),
|
|
}),
|
|
}));
|
|
|
|
const LOG = {
|
|
id: "log_1",
|
|
createdAt: "2026-08-02T10:00:00.000Z",
|
|
apiKey: { alias: "Website Lead Intake" },
|
|
ip: "192.0.2.10",
|
|
endpoint: "/api/leads",
|
|
method: "POST",
|
|
params: "{}",
|
|
status: 201,
|
|
duration: 42,
|
|
error: null,
|
|
} as unknown as ApiKeyLogDto;
|
|
|
|
function renderPage(items: ApiKeyLogDto[], pagination = { page: 1, pageSize: 10, totalItems: items.length, totalPages: 1 }) {
|
|
const actions = { onSearchChange: vi.fn(), onToggleProperty: vi.fn(), onReset: vi.fn(), onPage: vi.fn() };
|
|
render(<ApiLogsPage items={items} properties={[]} pagination={pagination} search="" actions={actions} />);
|
|
return actions;
|
|
}
|
|
|
|
describe("ApiLogsPage", () => {
|
|
it("renders a call row", () => {
|
|
renderPage([LOG]);
|
|
|
|
expect(screen.getByText("/api/leads")).toBeInTheDocument();
|
|
expect(screen.getByText("POST")).toBeInTheDocument();
|
|
expect(screen.getByText("201")).toBeInTheDocument();
|
|
expect(screen.getByText("metrics.speed.fast")).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the empty state instead of a bare table", () => {
|
|
renderPage([]);
|
|
|
|
expect(screen.getByText("models.apiKeyLog.empty.title")).toBeInTheDocument();
|
|
expect(document.querySelector("[data-ui-id='ui.settings.apiLogs.table']")).toBeNull();
|
|
});
|
|
|
|
it("offers both page steps in the middle of the result set", () => {
|
|
const actions = renderPage([LOG], { page: 2, pageSize: 10, totalItems: 30, totalPages: 3 });
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.settings.apiLogs.prev']")!);
|
|
expect(actions.onPage).toHaveBeenCalledWith(1);
|
|
fireEvent.click(document.querySelector("[data-ui-id='ui.settings.apiLogs.next']")!);
|
|
expect(actions.onPage).toHaveBeenCalledWith(3);
|
|
});
|
|
|
|
it("offers no page step on a single-page result set", () => {
|
|
renderPage([LOG], { page: 1, pageSize: 10, totalItems: 5, totalPages: 1 });
|
|
|
|
expect(document.querySelector("[data-ui-id='ui.settings.apiLogs.prev']")).toBeNull();
|
|
expect(document.querySelector("[data-ui-id='ui.settings.apiLogs.next']")).toBeNull();
|
|
});
|
|
|
|
it("keeps the badge buckets of the replaced SimpleBadge stack", () => {
|
|
expect(methodTone("DELETE")).toBe("subtle-negative");
|
|
expect(methodTone("HEAD")).toBe("subtle-neutral");
|
|
expect(statusTone(204)).toBe("subtle-positive");
|
|
expect(statusTone(500)).toBe("subtle-negative");
|
|
expect(statusTone(null)).toBe("subtle-warning");
|
|
expect(speedLabelKey(5)).toBe("metrics.speed.lightningFast");
|
|
expect(speedLabelKey(2500)).toBe("metrics.speed.verySlow");
|
|
});
|
|
});
|