import { jsx } from "react/jsx-runtime";
import React from "react";
import { render, fireEvent } from "@testing-library/react-native";
import { ThemeProvider, Input } from "../..";
describe("<Input />", () => {
  describe("Snapshots", () => {
    it("should match with default input", () => {
      const { toJSON } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, {}) })
      );
      expect(toJSON()).toMatchSnapshot();
    });
    it("should match with label", () => {
      const { toJSON } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input" }) })
      );
      expect(toJSON()).toMatchSnapshot();
    });
    it("should match when input is focused", () => {
      const { toJSON, getByTestId } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input", testID: "input-test" }) })
      );
      fireEvent(getByTestId("input-test"), "focus");
      expect(toJSON()).toMatchSnapshot();
    });
    it("should match with disabled input", () => {
      const { toJSON } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { disabled: true }) })
      );
      expect(toJSON()).toMatchSnapshot();
    });
    it("should match with error", () => {
      const { toJSON } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { error: "Error message" }) })
      );
      expect(toJSON()).toMatchSnapshot();
    });
    it("should match with helper text and max length", () => {
      const { toJSON } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { helper: "Helper text", maxLength: 20 }) })
      );
      expect(toJSON()).toMatchSnapshot();
    });
    it("should match with helper text, max length and hideMaxLength", () => {
      const { toJSON } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { helper: "Helper text", maxLength: 20, hideMaxLength: true }) })
      );
      expect(toJSON()).toMatchSnapshot();
    });
    it("should match with full width", () => {
      const { toJSON } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Label", full: true }) })
      );
      expect(toJSON()).toMatchSnapshot();
    });
  });
  describe("Events", () => {
    it("should call onChangeText", () => {
      const onChangeTextMock = jest.fn();
      const { getByTestId } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(
          Input,
          {
            label: "Input",
            testID: "input-test",
            onChangeText: onChangeTextMock
          }
        ) })
      );
      fireEvent.changeText(getByTestId("input-test"), "foo");
      expect(onChangeTextMock).toHaveBeenCalled();
    });
    it("should not call onChangeText when input is disabled", () => {
      const onChangeTextMock = jest.fn();
      const { getByTestId } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(
          Input,
          {
            label: "Input",
            testID: "input-test",
            onChangeText: onChangeTextMock,
            disabled: true
          }
        ) })
      );
      fireEvent(getByTestId("input-test"), "focus");
      expect(onChangeTextMock).not.toHaveBeenCalled();
    });
    it("should call onFocus", () => {
      const onFocusMock = jest.fn();
      const { getByTestId } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input", testID: "input-test", onFocus: onFocusMock }) })
      );
      fireEvent(getByTestId("input-test"), "focus");
      expect(onFocusMock).toHaveBeenCalled();
    });
    it("should call onBlur", () => {
      const onBlurMock = jest.fn();
      const { getByTestId } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input", testID: "input-test", onBlur: onBlurMock }) })
      );
      fireEvent(getByTestId("input-test"), "focus");
      fireEvent(getByTestId("input-test"), "blur");
      expect(onBlurMock).toHaveBeenCalled();
    });
  });
  describe("maxLength", () => {
    it("should update maxLength counter when add character", () => {
      const { getByText, rerender } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input", maxLength: 10 }) })
      );
      expect(getByText("0/10")).toBeTruthy();
      rerender(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input", value: "foo", maxLength: 10 }) })
      );
      expect(getByText("3/10")).toBeTruthy();
    });
  });
  describe("clean button", () => {
    it("should call onClean when press clean button", () => {
      const onCleanMock = jest.fn();
      const { getByRole } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input", value: "foo", onClean: onCleanMock }) })
      );
      fireEvent.press(getByRole("button"));
      expect(onCleanMock).toHaveBeenCalledWith("");
    });
    it("should test if clean button is present", () => {
      const { rerender, queryByRole } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input" }) })
      );
      expect(queryByRole("button")).toBeNull();
      rerender(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Input, { label: "Input", value: "foo" }) })
      );
      expect(queryByRole("button")).not.toBeNull();
    });
  });
});
