Add missing component and hook tests, raise coverage thresholds

13 new test files for untested components (color-palette, player-management,
stat-block, settings-modal, export/import dialogs, bulk-import-prompt,
source-fetch-prompt, player-character-section) and hooks (use-long-press,
use-swipe-to-dismiss, use-bulk-import, use-initiative-rolls). Expand
combatant-row tests with inline editing, HP popover, and condition picker.

Component coverage: 59% → 80% lines, 55% → 71% branches
Hook coverage: 72% → 83% lines, 55% → 66% branches

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-03-29 23:55:21 +02:00
parent 08b5db81ad
commit b229a0dac7
15 changed files with 1802 additions and 4 deletions

View File

@@ -0,0 +1,124 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { SourceFetchPrompt } from "../source-fetch-prompt.js";
const MONSTER_MANUAL_REGEX = /Monster Manual/;
afterEach(cleanup);
const mockFetchAndCacheSource = vi.fn();
const mockUploadAndCacheSource = vi.fn();
vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: () => ({
fetchAndCacheSource: mockFetchAndCacheSource,
uploadAndCacheSource: mockUploadAndCacheSource,
}),
}));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({
getDefaultFetchUrl: (code: string) =>
`https://example.com/bestiary/${code}.json`,
getSourceDisplayName: (code: string) =>
code === "MM" ? "Monster Manual" : code,
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
}));
function renderPrompt(sourceCode = "MM") {
const onSourceLoaded = vi.fn();
const result = render(
<SourceFetchPrompt
sourceCode={sourceCode}
onSourceLoaded={onSourceLoaded}
/>,
);
return { ...result, onSourceLoaded };
}
describe("SourceFetchPrompt", () => {
afterEach(() => {
vi.clearAllMocks();
});
it("renders source name, URL input, Load and Upload buttons", () => {
renderPrompt();
expect(screen.getByText(MONSTER_MANUAL_REGEX)).toBeInTheDocument();
expect(
screen.getByDisplayValue("https://example.com/bestiary/MM.json"),
).toBeInTheDocument();
expect(screen.getByText("Load")).toBeInTheDocument();
expect(screen.getByText("Upload file")).toBeInTheDocument();
});
it("Load calls fetchAndCacheSource and onSourceLoaded on success", async () => {
mockFetchAndCacheSource.mockResolvedValueOnce(undefined);
const user = userEvent.setup();
const { onSourceLoaded } = renderPrompt();
await user.click(screen.getByText("Load"));
await waitFor(() => {
expect(mockFetchAndCacheSource).toHaveBeenCalledWith(
"MM",
"https://example.com/bestiary/MM.json",
);
expect(onSourceLoaded).toHaveBeenCalled();
});
});
it("fetch error shows error message", async () => {
mockFetchAndCacheSource.mockRejectedValueOnce(new Error("Network error"));
const user = userEvent.setup();
renderPrompt();
await user.click(screen.getByText("Load"));
await waitFor(() => {
expect(screen.getByText("Network error")).toBeInTheDocument();
});
});
it("upload file calls uploadAndCacheSource and onSourceLoaded", async () => {
mockUploadAndCacheSource.mockResolvedValueOnce(undefined);
const user = userEvent.setup();
const { onSourceLoaded } = renderPrompt();
const file = new File(['{"monster":[]}'], "bestiary-mm.json", {
type: "application/json",
});
const fileInput = document.querySelector(
'input[type="file"]',
) as HTMLInputElement;
await user.upload(fileInput, file);
await waitFor(() => {
expect(mockUploadAndCacheSource).toHaveBeenCalledWith("MM", {
monster: [],
});
expect(onSourceLoaded).toHaveBeenCalled();
});
});
it("upload error shows error message", async () => {
mockUploadAndCacheSource.mockRejectedValueOnce(new Error("Invalid format"));
const user = userEvent.setup();
renderPrompt();
const file = new File(['{"bad": true}'], "bad.json", {
type: "application/json",
});
const fileInput = document.querySelector(
'input[type="file"]',
) as HTMLInputElement;
await user.upload(fileInput, file);
await waitFor(() => {
expect(screen.getByText("Invalid format")).toBeInTheDocument();
});
});
});