test(collection-pool): cover MarkReadyDialog behaviour
This commit is contained in:
parent
58d7c90e77
commit
1698e3d05b
1 changed files with 126 additions and 0 deletions
126
src/components/ui/mark-ready-dialog.test.tsx
Normal file
126
src/components/ui/mark-ready-dialog.test.tsx
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||
|
||||
import { MarkReadyDialog } from "./mark-ready-dialog";
|
||||
|
||||
const PO_LABEL = "PO-2026-07-009";
|
||||
const ITEM_LABEL = "Magnetic drill";
|
||||
|
||||
describe("MarkReadyDialog", () => {
|
||||
beforeEach(() => {
|
||||
class ResizeObserverMock {
|
||||
observe() {}
|
||||
unobserve() {}
|
||||
disconnect() {}
|
||||
}
|
||||
vi.stubGlobal("ResizeObserver", ResizeObserverMock);
|
||||
window.HTMLElement.prototype.scrollIntoView = vi.fn();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
});
|
||||
|
||||
it("renders the PO and item in the title", () => {
|
||||
render(
|
||||
<MarkReadyDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
onSubmit={vi.fn()}
|
||||
poLabel={PO_LABEL}
|
||||
itemLabel={ITEM_LABEL}
|
||||
/>,
|
||||
);
|
||||
expect(
|
||||
screen.getByText(`Mark ready: ${PO_LABEL} · ${ITEM_LABEL}`),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("disables Save when the textarea is empty", () => {
|
||||
render(
|
||||
<MarkReadyDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
onSubmit={vi.fn()}
|
||||
poLabel={PO_LABEL}
|
||||
itemLabel={ITEM_LABEL}
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByRole("button", { name: /save & mark ready/i })).toBeDisabled();
|
||||
});
|
||||
|
||||
it("disables Save when the textarea is whitespace only", () => {
|
||||
render(
|
||||
<MarkReadyDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
onSubmit={vi.fn()}
|
||||
poLabel={PO_LABEL}
|
||||
itemLabel={ITEM_LABEL}
|
||||
/>,
|
||||
);
|
||||
fireEvent.change(screen.getByRole("textbox"), {
|
||||
target: { value: " \n " },
|
||||
});
|
||||
expect(screen.getByRole("button", { name: /save & mark ready/i })).toBeDisabled();
|
||||
});
|
||||
|
||||
it("enables Save and submits the trimmed note", () => {
|
||||
const onSubmit = vi.fn();
|
||||
render(
|
||||
<MarkReadyDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
onSubmit={onSubmit}
|
||||
poLabel={PO_LABEL}
|
||||
itemLabel={ITEM_LABEL}
|
||||
/>,
|
||||
);
|
||||
fireEvent.change(screen.getByRole("textbox"), {
|
||||
target: { value: " spoke to David " },
|
||||
});
|
||||
const save = screen.getByRole("button", { name: /save & mark ready/i });
|
||||
expect(save).not.toBeDisabled();
|
||||
fireEvent.click(save);
|
||||
expect(onSubmit).toHaveBeenCalledWith("spoke to David");
|
||||
});
|
||||
|
||||
it("renders a server error without closing the modal", () => {
|
||||
render(
|
||||
<MarkReadyDialog
|
||||
open
|
||||
onOpenChange={vi.fn()}
|
||||
onSubmit={vi.fn()}
|
||||
poLabel={PO_LABEL}
|
||||
itemLabel={ITEM_LABEL}
|
||||
serverError="A note is required when marking ready."
|
||||
/>,
|
||||
);
|
||||
expect(
|
||||
screen.getByText(/a note is required when marking ready/i),
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getByRole("alert")).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByRole("dialog", {
|
||||
name: `Mark ready: ${PO_LABEL} · ${ITEM_LABEL}`,
|
||||
}),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("does not call onSubmit when the dialog is closed", () => {
|
||||
const onSubmit = vi.fn();
|
||||
const onOpenChange = vi.fn();
|
||||
render(
|
||||
<MarkReadyDialog
|
||||
open={false}
|
||||
onOpenChange={onOpenChange}
|
||||
onSubmit={onSubmit}
|
||||
poLabel={PO_LABEL}
|
||||
itemLabel={ITEM_LABEL}
|
||||
/>,
|
||||
);
|
||||
const save = screen.queryByRole("button", { name: /save & mark ready/i });
|
||||
expect(save).toBeNull();
|
||||
expect(onSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue