"use strict";
const react_esm = require('./../../_node_modules/@testing-library/react/dist/@testing-library/react.esm.cjs');
const zod = require("zod");
const useForm = require("./useForm.cjs");
describe("useForm", () => {
  const nestedFormState = {
    nestedField1: "value",
    nestedField2: "value2",
    array: ["one", "two", "three"]
  };
  const rootFormState = {
    field1: "test",
    field2: 4,
    field3: true,
    field4: nestedFormState
  };
  const useTestHook = () => {
    return useForm.useForm(rootFormState, {
      validationErrors: [{ key: "field1", message: "error" }],
      validationSchema: {
        field1: zod.z.string(),
        field2: zod.z.number().max(3, "schema error 1"),
        field3: zod.z.boolean(),
        field4: {
          schema: {
            nestedField1: zod.z.string(),
            nestedField2: zod.z.string(),
            array: {
              itemSchema: zod.z.string(),
              opts: (arr) => arr.min(4, "schema error 2")
              // this will fail, length is 3
            }
          }
        }
      }
    });
  };
  it("should return the correct form state", () => {
    const { result } = react_esm.renderHook(() => useTestHook());
    expect(result.current.formState).toEqual(rootFormState);
  });
  it("should modify the root form state correctly on set", () => {
    var _a;
    const { result } = react_esm.renderHook(() => useTestHook());
    react_esm.act(() => {
      result.current.formProp("field1").set("f1-new");
    });
    expect((_a = result.current.formState) == null ? void 0 : _a.field1).toBe("f1-new");
  });
  it("should add passed in validation errors to the correct keys", () => {
    const { result } = react_esm.renderHook(() => useTestHook());
    expect(result.current.formProp("field1").bind().myValidationErrors).toEqual([{ key: "field1", message: "error" }]);
    expect(result.current.formProp("field2").bind().myValidationErrors).toEqual([]);
    expect(result.current.formProp("field3").bind().myValidationErrors).toEqual([]);
    expect(result.current.formProp("field4", "nestedField1").bind().myValidationErrors).toEqual([]);
  });
  it("should dispatch validation errors to the correct keys", () => {
    const { result } = react_esm.renderHook(() => useTestHook());
    react_esm.act(() => result.current.formProp("field1").bind().addValidationError("error-2"));
    expect(result.current.formProp("field1").bind().myValidationErrors).toEqual([
      { key: "field1", message: "error-2" },
      { key: "field1", message: "error" }
    ]);
    expect(result.current.formProp("field2").bind().myValidationErrors).toEqual([]);
    expect(result.current.formProp("field3").bind().myValidationErrors).toEqual([]);
    expect(result.current.formProp("field4").bind().myValidationErrors).toEqual([]);
  });
  it("should dispatch touch state `true` for the correct keys", () => {
    const { result } = react_esm.renderHook(() => useTestHook());
    react_esm.act(() => result.current.formProp("field1").bind().setTouched(true));
    expect(result.current.formProp("field1").bind().isTouched).toBe(true);
    expect(result.current.formProp("field2").bind().isTouched).toBe(false);
    expect(result.current.formProp("field3").bind().isTouched).toBe(false);
    expect(result.current.formProp("field4").bind().isTouched).toBe(false);
    expect(result.current.formProp("field4", "nestedField1").bind().isTouched).toBe(false);
  });
  it("should dispatch touch state `false` for the correct keys", () => {
    const { result } = react_esm.renderHook(() => useTestHook());
    react_esm.act(() => result.current.formProp("field1").bind().setTouched(false));
    expect(result.current.formProp("field1").bind().isTouched).toBe(false);
    expect(result.current.formProp("field2").bind().isTouched).toBe(false);
    expect(result.current.formProp("field3").bind().isTouched).toBe(false);
    expect(result.current.formProp("field4").bind().isTouched).toBe(false);
    expect(result.current.formProp("field4", "nestedField1").bind().isTouched).toBe(false);
  });
  it("returns a formProp function which takes property accessor args and returns a working single field validator", async () => {
    const { result } = react_esm.renderHook(() => useTestHook());
    const response = await react_esm.act(() => result.current.formProp("field2").validate());
    expect(response).toBe(false);
    const successResponse = await react_esm.act(() => result.current.formProp("field1").validate());
    expect(successResponse).toBe(true);
  });
});
//# sourceMappingURL=useForm.spec.cjs.map
