import "@testing-library/jest-dom";
import React from "react";
import { render, fireEvent } from "@testing-library/react-native";
import { AtomCountryPicker } from ".";

jest.useFakeTimers();

describe("[React] AtomCountryPicker", () => {
  afterEach(() => {
    jest.clearAllMocks();
  });
  afterAll(() => {
    jest.resetAllMocks();
  });
  it("Rendered component", () => {
    const body = {
      disabled: false,
    };
    const component = render(<AtomCountryPicker {...body} />);
    expect(component).toBeTruthy();
  });
  it("Rendered component disabled", () => {
    const body = {
      disabled: true,
      value: "COL",
      importantItems: ["COL", "ECU", "MEX"],
    };
    const component = render(<AtomCountryPicker {...body} />);
    const select = component.getByPlaceholderText("+57 Colombia");
    select && fireEvent.press(select);
    const data = component.queryByTestId("options");
    expect(data).toBe(null);
  });

  it("Rendered component init values", () => {
    const body = {
      disabled: false,
      value: "COL",
      importantItems: ["COL", "ECU", "MEX"],
      name: "Label",
    };
    const component = render(<AtomCountryPicker {...body} />);
    const select = component.getByTestId("input-country-picker");
    fireEvent.press(select);
    const element = component.getByTestId("options");
    expect(element).not.toBe(null);
  });

  it("Rendered component set input value", () => {
    const body = {
      disabled: false,
      value: "COL",
      importantItems: ["COL", "ECU", "MEX"],
    };
    const component = render(<AtomCountryPicker {...body} />);
    const select = component.getByTestId("input-country-picker");
    select && fireEvent.press(select);
    select && fireEvent.changeText(select, "c");
    const element = component.getByTestId(`COL`);
    expect(element).toBeDefined();
  });

  it("Rendered component disabled set input value", () => {
    const body = {
      disabled: true,
      value: "COL",
      importantItems: ["COL", "ECU", "MEX"],
      name: "Label",
    };
    const component = render(<AtomCountryPicker {...body} />);
    const select = component.getByTestId("input-country-picker");
    select && fireEvent.press(select);
    select && fireEvent.changeText(select, "c");
    const element = component.queryByText("options");
    expect(element).toBe(null);
  });

  it("Rendered component readOnly set input value", () => {
    const body = {
      readOnly: true,
      value: "COL",
      importantItems: ["COL", "ECU", "MEX"],
      name: "Label",
    };
    const component = render(<AtomCountryPicker {...body} />);
    const select = component.getByTestId("input-country-picker");
    select && fireEvent.press(select);
    select && fireEvent.changeText(select, "c");
    const element = component.queryByText("options");
    expect(element).toBe(null);
  });

  it("Rendered component select Element", () => {
    const body = {
      value: "COL",
      importantItems: ["COL", "ECU", "MEX"],
      name: "label",
      hasError: true,
    };
    const component = render(<AtomCountryPicker {...body} />);
    const select = component.getByTestId("input-country-picker");
    fireEvent.press(select);
    fireEvent.changeText(select, "c");
    const element = component.getByTestId(`COL`);
    fireEvent.press(element);
    const options = component.queryByText("options");
    expect(options).toBe(null);
  });

  it("Rendered component set input different actual value", () => {
    const useRefSpy = jest
      .spyOn(React, "useRef")
      .mockReturnValueOnce({ current: {} });
    const body = {
      disabled: false,
      value: "COL",
      importantItems: ["COL", "ECU", "MEX"],
      name: "label",
    };
    const component = render(<AtomCountryPicker {...body} />);
    const select = component.getByTestId("input-country-picker");
    fireEvent.press(select);
    jest.runOnlyPendingTimers();
    fireEvent.changeText(select, "arge");
    const element = component.getByTestId(`ARG`);
    fireEvent.press(element);
    const options = component.queryByText("options");
    expect(options).toBe(null);
  });
});
