Spaces:
Running

MiniSearch / client /components /Logs /LogsModal.test.tsx
system's picture
system HF Staff
Sync from felladrin/MiniSearch@74613ff
6a3fdef verified
Raw History Blame Contribute Delete
5.56 kB
import { MantineProvider } from "@mantine/core";
import { act, render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { logEntriesPubSub } from "@/modules/logEntries";
import LogsModal from "./LogsModal";
const [setLogEntries] = logEntriesPubSub;
function logEntry(index: number, message = `entry ${index}`) {
return {
id: `id-${index}`,
timestamp: new Date(2024, 5, 15, 12, 0, index).toISOString(),
message,
};
}
function renderLogsModal(opened = true, onClose = vi.fn()) {
return render(
<MantineProvider>
<LogsModal opened={opened} onClose={onClose} />
</MantineProvider>,
);
}
/** The messages shown on the current page, in table order. */
function shownMessages() {
const body = screen.getAllByRole("rowgroup")[1];
return within(body)
.queryAllByRole("row")
.map((row) => within(row).getAllByRole("cell")[1].textContent);
}
describe("LogsModal", () => {
// jsdom implements neither, so the download test assigns them and every
// test puts back whatever was there.
const { createObjectURL, revokeObjectURL } = URL;
beforeEach(() => {
setLogEntries([]);
});
afterEach(() => {
vi.restoreAllMocks();
URL.createObjectURL = createObjectURL;
URL.revokeObjectURL = revokeObjectURL;
});
it("renders nothing while closed", () => {
setLogEntries([logEntry(1)]);
renderLogsModal(false);
expect(screen.queryByText("Logs")).not.toBeInTheDocument();
expect(screen.queryByText("entry 1")).not.toBeInTheDocument();
});
it("shows five entries per page and pages through the rest", async () => {
setLogEntries(Array.from({ length: 7 }, (_, index) => logEntry(index)));
const user = userEvent.setup();
renderLogsModal();
expect(shownMessages()).toEqual([
"entry 0",
"entry 1",
"entry 2",
"entry 3",
"entry 4",
]);
await user.click(screen.getByRole("button", { name: "2" }));
expect(shownMessages()).toEqual(["entry 5", "entry 6"]);
});
it("shows entries logged while it is open", () => {
renderLogsModal();
expect(shownMessages()).toEqual([]);
act(() => setLogEntries([logEntry(1, "logged while open")]));
expect(shownMessages()).toEqual(["logged while open"]);
});
it("filters by message regardless of case", async () => {
setLogEntries([
logEntry(1, "Search started"),
logEntry(2, "Model loaded"),
logEntry(3, "search finished"),
]);
const user = userEvent.setup();
renderLogsModal();
await user.type(screen.getByPlaceholderText("Filter logs..."), "SEARCH");
expect(shownMessages()).toEqual(["Search started", "search finished"]);
});
it("goes back to the first page when the filter changes", async () => {
setLogEntries(Array.from({ length: 12 }, (_, index) => logEntry(index)));
const user = userEvent.setup();
renderLogsModal();
await user.click(screen.getByRole("button", { name: "3" }));
expect(shownMessages()).toEqual(["entry 10", "entry 11"]);
await user.type(screen.getByPlaceholderText("Filter logs..."), "entry");
expect(shownMessages()).toEqual([
"entry 0",
"entry 1",
"entry 2",
"entry 3",
"entry 4",
]);
});
it("clears the filter with its clear button", async () => {
setLogEntries([logEntry(1, "alpha"), logEntry(2, "beta")]);
const user = userEvent.setup();
renderLogsModal();
expect(
screen.queryByRole("button", { name: "Clear filter" }),
).not.toBeInTheDocument();
const filter = screen.getByPlaceholderText("Filter logs...");
await user.type(filter, "alp");
expect(shownMessages()).toEqual(["alpha"]);
await user.click(screen.getByRole("button", { name: "Clear filter" }));
expect(filter).toHaveValue("");
expect(shownMessages()).toEqual(["alpha", "beta"]);
expect(
screen.queryByRole("button", { name: "Clear filter" }),
).not.toBeInTheDocument();
});
it("downloads every entry as logs.json, not only the filtered ones", async () => {
const entries = [logEntry(1, "alpha"), logEntry(2, "beta")];
setLogEntries(entries);
const createObjectURL = vi.fn((_blob: Blob) => "blob:logs");
const revokeObjectURL = vi.fn();
URL.createObjectURL = createObjectURL;
URL.revokeObjectURL = revokeObjectURL;
let downloadedAs: string | undefined;
vi.spyOn(HTMLAnchorElement.prototype, "click").mockImplementation(function (
this: HTMLAnchorElement,
) {
downloadedAs = this.download;
expect(this.href).toBe("blob:logs");
expect(this.isConnected).toBe(true);
});
const user = userEvent.setup();
renderLogsModal();
await user.type(screen.getByPlaceholderText("Filter logs..."), "alpha");
await user.click(screen.getByRole("button", { name: "Download Logs" }));
expect(downloadedAs).toBe("logs.json");
const blob = createObjectURL.mock.calls[0][0];
expect(blob.type).toBe("application/json");
expect(JSON.parse(await blob.text())).toEqual(entries);
expect(revokeObjectURL).toHaveBeenCalledWith("blob:logs");
expect(document.querySelector('a[download="logs.json"]')).toBeNull();
});
it("calls onClose when the modal is closed", async () => {
const onClose = vi.fn();
const user = userEvent.setup();
renderLogsModal(true, onClose);
await user.keyboard("{Escape}");
expect(onClose).toHaveBeenCalledTimes(1);
});
});