import { describe, expect, it, vi } from "vitest"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { ColonyTakeoverSection } from "./colony-takeover-section"; import { initialInspectionValues } from "@/app/(app)/inspections/new/inspection-form-values"; const colony = { occupiedFrames: 6, broodStatus: "AVERAGE", feedStatus: "GOOD" } as const; function values(overrides = {}) { return { ...initialInspectionValues("colony-1"), ...overrides }; } function renderSection(props: Partial> = {}) { const onChange = vi.fn(); render( , ); return onChange; } describe("ColonyTakeoverSection", () => { it("shows each filled field as „bisher → neu“ with a checked box", () => { renderSection(); expect(screen.getByRole("checkbox", { name: "Besetzte Waben: 6 → 9" })).toBeChecked(); expect(screen.getByRole("checkbox", { name: "Brut: Mittel → Gut" })).toBeChecked(); expect(screen.getByRole("checkbox", { name: "Futter: Ausreichend → Niedrig" })).toBeChecked(); }); it("lists only fields filled in this inspection", () => { renderSection({ values: values({ feedStatus: "CRITICAL" }), selected: ["feedStatus"] }); expect(screen.getAllByRole("checkbox")).toHaveLength(1); expect(screen.getByRole("checkbox", { name: "Futter: Ausreichend → Kritisch" })).toBeChecked(); }); it("says „noch nicht erfasst“ for a colony card without a value", () => { renderSection({ colony: { occupiedFrames: null, broodStatus: null, feedStatus: null }, values: values({ feedStatus: "WEAK" }), selected: [], }); expect( screen.getByRole("checkbox", { name: "Futter: noch nicht erfasst → Niedrig" }), ).not.toBeChecked(); }); it("reports the new selection when a box is toggled", async () => { const onChange = renderSection(); await userEvent.click(screen.getByRole("checkbox", { name: "Brut: Mittel → Gut" })); expect(onChange).toHaveBeenCalledWith(["occupiedFrames", "feedStatus"]); }); it("adds a box back in the fixed field order", async () => { const onChange = renderSection({ selected: ["feedStatus"] }); await userEvent.click(screen.getByRole("checkbox", { name: "Besetzte Waben: 6 → 9" })); expect(onChange).toHaveBeenCalledWith(["occupiedFrames", "feedStatus"]); }); it("announces how many values are taken over", () => { renderSection({ selected: ["feedStatus"] }); expect(screen.getByRole("status")).toHaveTextContent("1 Wert wird auf die Volkskarte übernommen."); }); it("is hidden without colonies:update", () => { renderSection({ canUpdateColony: false }); expect(screen.queryByRole("group", { name: /Volkskarte/ })).not.toBeInTheDocument(); expect(screen.queryByRole("checkbox")).not.toBeInTheDocument(); }); it("is hidden when nothing was filled in, but keeps its status region mounted", () => { renderSection({ values: values(), selected: [] }); expect(screen.queryByRole("group", { name: /Volkskarte/ })).not.toBeInTheDocument(); expect(screen.getByRole("status")).toBeInTheDocument(); }); it("says „bisher unbekannt“ when the card values are not known (cache empty)", () => { renderSection({ colony: null, selected: [] }); expect(screen.getByRole("checkbox", { name: "Futter: bisher unbekannt → Niedrig" })).toBeInTheDocument(); expect(screen.getByRole("checkbox", { name: "Besetzte Waben: bisher unbekannt → 9" })).toBeInTheDocument(); }); it("can be operated with Tab and Space", async () => { const onChange = renderSection(); await userEvent.tab(); expect(screen.getByRole("checkbox", { name: "Besetzte Waben: 6 → 9" })).toHaveFocus(); await userEvent.keyboard(" "); expect(onChange).toHaveBeenCalledWith(["broodStatus", "feedStatus"]); }); });