Files
fusion/packages/dashboard/app/components/__tests__/InlineCreateCard.test.tsx
Dustin Byrne c2166723ee feat(HAI-064): add blur-to-cancel behavior for empty InlineCreateCard
- Add focusout listener that cancels InlineCreateCard when focus leaves with empty input
- Preserve card when focus moves between elements inside the card
- Treat whitespace-only input as empty for cancel purposes
- Add comprehensive tests for blur-to-cancel behavior
2026-03-26 00:01:29 -04:00

68 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { InlineCreateCard } from "../InlineCreateCard";
// Mock lucide-react
vi.mock("lucide-react", () => ({
Link: () => null,
}));
// Mock the api module
vi.mock("../../api", () => ({
uploadAttachment: vi.fn(),
}));
function renderCard() {
const props = {
tasks: [],
onSubmit: vi.fn().mockResolvedValue({ id: "HAI-001" }),
onCancel: vi.fn(),
addToast: vi.fn(),
};
const result = render(<InlineCreateCard {...props} />);
return { ...result, props };
}
describe("InlineCreateCard blur-to-cancel", () => {
it("calls onCancel when focus leaves the card with empty input", () => {
const { props } = renderCard();
const textarea = screen.getByPlaceholderText("What needs to be done?");
textarea.focus();
fireEvent.focusOut(textarea, { relatedTarget: null });
expect(props.onCancel).toHaveBeenCalledTimes(1);
});
it("does NOT call onCancel when focus leaves with non-empty input", () => {
const { props } = renderCard();
const textarea = screen.getByPlaceholderText("What needs to be done?");
fireEvent.change(textarea, { target: { value: "Some task description" } });
fireEvent.focusOut(textarea, { relatedTarget: null });
expect(props.onCancel).not.toHaveBeenCalled();
});
it("does NOT call onCancel when focus moves to another element inside the card", () => {
const { props } = renderCard();
const textarea = screen.getByPlaceholderText("What needs to be done?");
const depsButton = screen.getByText(/Deps/);
textarea.focus();
fireEvent.focusOut(textarea, { relatedTarget: depsButton });
expect(props.onCancel).not.toHaveBeenCalled();
});
it("calls onCancel when blur with only whitespace input", () => {
const { props } = renderCard();
const textarea = screen.getByPlaceholderText("What needs to be done?");
fireEvent.change(textarea, { target: { value: " " } });
fireEvent.focusOut(textarea, { relatedTarget: null });
expect(props.onCancel).toHaveBeenCalledTimes(1);
});
});