import { renderHook, act } from "@testing-library/react";
import assert from "node:assert/strict";
import { afterEach, describe, it } from "node:test";
import { useFormValidation } from "./useFormValidation";
import { constants } from "../../constants";

describe("useFormValidation", () => {
  afterEach(() => {
    constants.EventBusInstance.clear();
    document.body.innerHTML = "";
  });

  it("updates isValid state for input validation events", () => {
    const target = document.createElement("div");
    document.body.appendChild(target);
    const ref = { current: target };

    const { result } = renderHook(() => useFormValidation(ref, null, "input"));

    act(() => {
      target.dispatchEvent(new window.CustomEvent(constants.formBubbles.inputValid, {
        detail: { isValid: true },
      }));
    });

    assert.strictEqual(result.current.isValid, true);

    act(() => {
      target.dispatchEvent(new window.CustomEvent(constants.formBubbles.inputInvalid, {
        detail: { isValid: false },
      }));
    });

    assert.strictEqual(result.current.isValid, false);
    assert.deepStrictEqual(result.current.invalidInputNames, []);
  });
});
