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,134 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { createRef } from "react";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { polyfillDialog } from "../../__tests__/polyfill-dialog.js";
import { AllProviders } from "../../__tests__/test-providers.js";
import {
PlayerCharacterSection,
type PlayerCharacterSectionHandle,
} from "../player-character-section.js";
const CREATE_FIRST_PC_REGEX = /create your first player character/i;
beforeAll(() => {
polyfillDialog();
Object.defineProperty(globalThis, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
});
afterEach(cleanup);
vi.mock("../../persistence/encounter-storage.js", () => ({
loadEncounter: () => null,
saveEncounter: () => {},
}));
vi.mock("../../persistence/player-character-storage.js", () => ({
loadPlayerCharacters: () => [],
savePlayerCharacters: () => {},
}));
vi.mock("../../adapters/bestiary-cache.js", () => ({
loadAllCachedCreatures: () => Promise.resolve(new Map()),
isSourceCached: () => Promise.resolve(false),
cacheSource: () => Promise.resolve(),
getCachedSources: () => Promise.resolve([]),
clearSource: () => Promise.resolve(),
clearAll: () => Promise.resolve(),
}));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
getDefaultFetchUrl: () => "",
getSourceDisplayName: (code: string) => code,
}));
function renderSection() {
const ref = createRef<PlayerCharacterSectionHandle>();
const result = render(<PlayerCharacterSection ref={ref} />, {
wrapper: AllProviders,
});
return { ...result, ref };
}
describe("PlayerCharacterSection", () => {
it("openManagement ref handle opens the management dialog", async () => {
const { ref } = renderSection();
const handle = ref.current;
if (!handle) throw new Error("ref not set");
act(() => handle.openManagement());
// Management dialog should now be open with its title visible
await waitFor(() => {
const dialogs = document.querySelectorAll("dialog");
const managementDialog = Array.from(dialogs).find((d) =>
d.textContent?.includes("Player Characters"),
);
expect(managementDialog).toHaveAttribute("open");
});
});
it("creating a character from management opens create modal", async () => {
const user = userEvent.setup();
const { ref } = renderSection();
const handle = ref.current;
if (!handle) throw new Error("ref not set");
act(() => handle.openManagement());
await user.click(
screen.getByRole("button", {
name: CREATE_FIRST_PC_REGEX,
}),
);
// Create modal should now be visible
await waitFor(() => {
expect(screen.getByPlaceholderText("Character name")).toBeInTheDocument();
});
});
it("saving a new character and returning to management", async () => {
const user = userEvent.setup();
const { ref } = renderSection();
const handle = ref.current;
if (!handle) throw new Error("ref not set");
act(() => handle.openManagement());
await user.click(
screen.getByRole("button", {
name: CREATE_FIRST_PC_REGEX,
}),
);
// Fill in the create form
await user.type(screen.getByPlaceholderText("Character name"), "Aria");
await user.type(screen.getByPlaceholderText("AC"), "16");
await user.type(screen.getByPlaceholderText("Max HP"), "30");
await user.click(screen.getByRole("button", { name: "Create" }));
// Should return to management dialog showing the new character
await waitFor(() => {
expect(screen.getByText("Aria")).toBeInTheDocument();
});
});
});