import { renderHook } from "@testing-library/react";
import { describe, it, expect } from "vitest";
import { useBeforeUnloadGuard } from "../../../app/hooks/useBeforeUnloadGuard";
import { dispatchBeforeUnload } from "../../utils/test-utils";

describe("useBeforeUnloadGuard", () => {
  it("does not intercept unload when disabled", () => {
    renderHook(() => useBeforeUnloadGuard(false));
    expect(dispatchBeforeUnload()).toBe(false);
  });

  it("prevents unload when enabled", () => {
    renderHook(() => useBeforeUnloadGuard(true));
    expect(dispatchBeforeUnload()).toBe(true);
  });

  it("drops the listener when enabled flips to false", () => {
    const { rerender } = renderHook(
      ({ enabled }) => useBeforeUnloadGuard(enabled),
      { initialProps: { enabled: true } },
    );
    expect(dispatchBeforeUnload()).toBe(true);
    rerender({ enabled: false });
    expect(dispatchBeforeUnload()).toBe(false);
  });
});
