import { jsx } from "react/jsx-runtime";
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import { ThemeProvider, AutoComplete } from "../..";
describe("<AutoComplete />", () => {
  describe("Snapshots", () => {
    it("should match with default component", () => {
      const { container } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(AutoComplete, {}) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with a full width component", () => {
      const { container } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(AutoComplete, { full: true }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with options list", () => {
      const { getByDisplayValue, container } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(AutoComplete, { value: "s", options: ["first", "second"] }) })
      );
      fireEvent.focus(getByDisplayValue("s"));
      expect(container).toMatchSnapshot();
    });
    it("should match with full width options list", () => {
      const { getByDisplayValue, container } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(AutoComplete, { value: "s", full: true, options: ["first", "second"] }) })
      );
      fireEvent.focus(getByDisplayValue("s"));
      expect(container).toMatchSnapshot();
    });
  });
  describe("Options list", () => {
    it("should filter the options", () => {
      const { container, getByDisplayValue } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(AutoComplete, { value: "secon", options: ["first", "second", "third"] }) })
      );
      fireEvent.focus(getByDisplayValue("secon"));
      expect(container.querySelector("ul").firstChild.textContent).toBe(
        "second"
      );
    });
    it("should not filter the options", () => {
      const { container, getByDisplayValue } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(
          AutoComplete,
          {
            value: "secon",
            options: ["first", "second", "third"],
            shouldFilterOptions: false
          }
        ) })
      );
      fireEvent.focus(getByDisplayValue("secon"));
      expect(container.querySelector("ul").firstChild.textContent).toBe(
        "first"
      );
      expect(container.querySelector("ul").childNodes.length).toBe(3);
    });
    it("should close options list when clean button is clicked", () => {
      const { container, getByDisplayValue, getByRole } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(AutoComplete, { value: "secon", options: ["first", "second", "third"] }) })
      );
      fireEvent.focus(getByDisplayValue("secon"));
      fireEvent.click(getByRole("button", { name: /clear/i }));
      expect(container.querySelector("ul")).toBe(null);
    });
  });
  describe("Event listeners", () => {
    it("should call onChangeMock", () => {
      const onChangeMock = jest.fn();
      const onCleanMock = jest.fn();
      const onSelectMock = jest.fn();
      const { getByDisplayValue } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(
          AutoComplete,
          {
            value: "New",
            options: ["New York"],
            onChange: onChangeMock
          }
        ) })
      );
      fireEvent.change(getByDisplayValue("New"), { target: { value: "a" } });
      expect(onChangeMock).toHaveBeenCalledWith("a");
      expect(onSelectMock).not.toHaveBeenCalled();
      expect(onCleanMock).not.toHaveBeenCalled();
    });
    it("should call onCleanMock", () => {
      const onCleanMock = jest.fn();
      const onSelectMock = jest.fn();
      const onChangeMock = jest.fn();
      const { getByDisplayValue, getByRole } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(
          AutoComplete,
          {
            value: "New",
            options: ["New York"],
            onClean: onCleanMock,
            onSelect: onSelectMock,
            onChange: onChangeMock
          }
        ) })
      );
      fireEvent.focus(getByDisplayValue("New"));
      fireEvent.click(getByRole("button", { name: /clear/i }));
      expect(onCleanMock).toHaveBeenCalledWith("");
      expect(onSelectMock).not.toHaveBeenCalled();
      expect(onChangeMock).toHaveBeenCalledWith("");
    });
    it("should call onSelectMock", () => {
      const onSelectMock = jest.fn();
      const onCleanMock = jest.fn();
      const onChangeMock = jest.fn();
      const { getByDisplayValue, getByRole } = render(
        /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(
          AutoComplete,
          {
            value: "New",
            options: ["New York"],
            onSelect: onSelectMock,
            onClean: onCleanMock,
            onChange: onChangeMock
          }
        ) })
      );
      fireEvent.focus(getByDisplayValue("New"));
      fireEvent.click(getByRole("option"));
      expect(onSelectMock).toHaveBeenCalledWith("New York");
      expect(onChangeMock).toHaveBeenCalledWith("New York");
      expect(onCleanMock).not.toHaveBeenCalled();
    });
  });
});
