import { describe, it, expect } from "vitest";
import {
  renderWithProviders as render,
  screen,
  fireEvent,
  dispatchBeforeUnload,
} from "../utils/test-utils";
import "@testing-library/jest-dom/vitest";
import CustomMethodCardWizard from "../../app/(app)/create/components/CustomMethodCardWizard";

describe("CustomMethodCardWizard — tab close guard", () => {
  it("does not block unload when the wizard is open but unchanged", async () => {
    render(
      <CustomMethodCardWizard
        isOpen
        onClose={() => {
          /* noop */
        }}
        onFinalize={() => {
          /* noop */
        }}
      />,
    );
    await screen.findByPlaceholderText("Policy name");
    expect(dispatchBeforeUnload()).toBe(false);
  });

  it("blocks unload after the user types a policy name", async () => {
    render(
      <CustomMethodCardWizard
        isOpen
        onClose={() => {
          /* noop */
        }}
        onFinalize={() => {
          /* noop */
        }}
      />,
    );
    const name = await screen.findByPlaceholderText("Policy name");
    fireEvent.change(name, { target: { value: "Garden hours" } });
    expect(dispatchBeforeUnload()).toBe(true);
  });
});
