77 lines
3.0 KiB
TypeScript
77 lines
3.0 KiB
TypeScript
import "@testing-library/jest-dom";
|
|
import { render, screen } from "@testing-library/react";
|
|
import { MemoryRouter } from "react-router";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import TenantActivityLogToolbar from "~/components/activity/TenantActivityLogToolbar";
|
|
|
|
vi.mock("react-i18next", () => ({
|
|
useTranslation: () => ({ t: (key: string) => key }),
|
|
}));
|
|
|
|
vi.mock("~/components/ui/input/InputFilters", () => ({
|
|
default: ({ filters }: { filters: { name: string; title: string }[] }) => (
|
|
<div data-testid="input-filters">
|
|
{filters.map((filter) => (
|
|
<span key={filter.name}>{filter.title}</span>
|
|
))}
|
|
</div>
|
|
),
|
|
}));
|
|
|
|
const filters = [
|
|
{ name: "action", title: "models.log.action" },
|
|
{ name: "url", title: "models.log.url" },
|
|
{ name: "actor", title: "models.log.actor" },
|
|
];
|
|
|
|
function renderToolbar(initialEntry: string) {
|
|
return render(
|
|
<MemoryRouter initialEntries={[initialEntry]}>
|
|
<TenantActivityLogToolbar filters={filters} />
|
|
</MemoryRouter>
|
|
);
|
|
}
|
|
|
|
describe("TenantActivityLogToolbar", () => {
|
|
it("renders the provided filters", () => {
|
|
renderToolbar("/acme/settings/logs?action=create");
|
|
|
|
expect(screen.getByTestId("input-filters")).toBeInTheDocument();
|
|
expect(screen.getByText("models.log.action")).toBeInTheDocument();
|
|
expect(screen.getByText("models.log.url")).toBeInTheDocument();
|
|
expect(screen.getByText("models.log.actor")).toBeInTheDocument();
|
|
});
|
|
|
|
it("preserves active filters while replacing stale pagination with CSV export", () => {
|
|
renderToolbar("/acme/settings/logs?q=login&action=auth&url=%2Flogin&actor=Grace&page=3&pageSize=25");
|
|
|
|
const exportUrl = new URL(screen.getByRole("link", { name: "Export" }).getAttribute("href") ?? "", "https://example.test");
|
|
|
|
expect(exportUrl.pathname).toBe("/acme/settings/logs");
|
|
expect(exportUrl.searchParams.get("q")).toBe("login");
|
|
expect(exportUrl.searchParams.get("action")).toBe("auth");
|
|
expect(exportUrl.searchParams.get("url")).toBe("/login");
|
|
expect(exportUrl.searchParams.get("actor")).toBe("Grace");
|
|
expect(exportUrl.searchParams.get("pageSize")).toBe("25");
|
|
expect(exportUrl.searchParams.get("tenantId")).toBeNull();
|
|
expect(exportUrl.searchParams.get("page")).toBeNull();
|
|
expect(exportUrl.searchParams.get("export")).toBe("csv");
|
|
});
|
|
|
|
it("replaces an existing export value with csv", () => {
|
|
renderToolbar("/acme/settings/logs?export=json&actor=user-1");
|
|
|
|
const exportUrl = new URL(screen.getByRole("link", { name: "Export" }).getAttribute("href") ?? "", "https://example.test");
|
|
|
|
expect(exportUrl.pathname).toBe("/acme/settings/logs");
|
|
expect(exportUrl.searchParams.get("actor")).toBe("user-1");
|
|
expect(exportUrl.searchParams.get("export")).toBe("csv");
|
|
});
|
|
|
|
it("creates a valid tenant-relative CSV export href without existing search params", () => {
|
|
renderToolbar("/acme/settings/logs");
|
|
|
|
expect(screen.getByRole("link", { name: "Export" })).toHaveAttribute("href", "/acme/settings/logs?export=csv");
|
|
});
|
|
});
|