"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_react2 = require("@testing-library/react");
var import__ = require("../..");
describe("<AutoComplete />", () => {
  describe("Snapshots", () => {
    it("should match with default component", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.AutoComplete, {}) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with a full width component", () => {
      const { container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.AutoComplete, { full: true }) })
      );
      expect(container).toMatchSnapshot();
    });
    it("should match with options list", () => {
      const { getByDisplayValue, container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.AutoComplete, { value: "s", options: ["first", "second"] }) })
      );
      import_react2.fireEvent.focus(getByDisplayValue("s"));
      expect(container).toMatchSnapshot();
    });
    it("should match with full width options list", () => {
      const { getByDisplayValue, container } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.AutoComplete, { value: "s", full: true, options: ["first", "second"] }) })
      );
      import_react2.fireEvent.focus(getByDisplayValue("s"));
      expect(container).toMatchSnapshot();
    });
  });
  describe("Options list", () => {
    it("should filter the options", () => {
      const { container, getByDisplayValue } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.AutoComplete, { value: "secon", options: ["first", "second", "third"] }) })
      );
      import_react2.fireEvent.focus(getByDisplayValue("secon"));
      expect(container.querySelector("ul").firstChild.textContent).toBe(
        "second"
      );
    });
    it("should not filter the options", () => {
      const { container, getByDisplayValue } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import__.AutoComplete,
          {
            value: "secon",
            options: ["first", "second", "third"],
            shouldFilterOptions: false
          }
        ) })
      );
      import_react2.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 } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.AutoComplete, { value: "secon", options: ["first", "second", "third"] }) })
      );
      import_react2.fireEvent.focus(getByDisplayValue("secon"));
      import_react2.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 } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import__.AutoComplete,
          {
            value: "New",
            options: ["New York"],
            onChange: onChangeMock
          }
        ) })
      );
      import_react2.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 } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import__.AutoComplete,
          {
            value: "New",
            options: ["New York"],
            onClean: onCleanMock,
            onSelect: onSelectMock,
            onChange: onChangeMock
          }
        ) })
      );
      import_react2.fireEvent.focus(getByDisplayValue("New"));
      import_react2.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 } = (0, import_react2.render)(
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.ThemeProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import__.AutoComplete,
          {
            value: "New",
            options: ["New York"],
            onSelect: onSelectMock,
            onClean: onCleanMock,
            onChange: onChangeMock
          }
        ) })
      );
      import_react2.fireEvent.focus(getByDisplayValue("New"));
      import_react2.fireEvent.click(getByRole("option"));
      expect(onSelectMock).toHaveBeenCalledWith("New York");
      expect(onChangeMock).toHaveBeenCalledWith("New York");
      expect(onCleanMock).not.toHaveBeenCalled();
    });
  });
});
