import { jest } from "@jest/globals";
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import DateInput from "../pages/GlobalConfig/Inputs/DateInput.js";

describe("DateInput Component", () => {
  const defaultProps = {
    field: {
      Id: "1",
      Label: "Test Date",
      ProValue: "2023-06-15",
      Placeholder: "Select date",
    },
    disabled: false,
    onChange: jest.fn(),
    error: false,
    helperText: "",
  };

  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("should render date input with label", () => {
    render(<DateInput {...defaultProps} />);

    expect(screen.getByLabelText("Test Date")).toBeInTheDocument();
  });

  it("should display the provided value", () => {
    render(<DateInput {...defaultProps} />);

    expect(screen.getByDisplayValue("2023-06-15")).toBeInTheDocument();
  });

  it("should call onChange when date is changed", () => {
    render(<DateInput {...defaultProps} />);

    const input = screen.getByLabelText("Test Date");
    fireEvent.change(input, { target: { value: "2023-07-20" } });

    expect(defaultProps.onChange).toHaveBeenCalledWith("2023-07-20");
  });

  it("should be disabled when disabled prop is true", () => {
    render(<DateInput {...defaultProps} disabled={true} />);

    expect(screen.getByLabelText("Test Date")).toBeDisabled();
  });

  it("should show error state when error prop is true", () => {
    render(<DateInput {...defaultProps} error={true} helperText="Date is required" />);

    expect(screen.getByText("Date is required")).toBeInTheDocument();
  });

  it("should render with empty value when ProValue is not provided", () => {
    const propsWithoutValue = {
      ...defaultProps,
      field: {
        ...defaultProps.field,
        ProValue: undefined,
      },
    };

    render(<DateInput {...propsWithoutValue} />);

    expect(screen.getByLabelText("Test Date")).toBeInTheDocument();
  });

  it("should have correct id attribute", () => {
    render(<DateInput {...defaultProps} />);

    expect(document.getElementById("field-1")).toBeInTheDocument();
  });
});
