import React from 'react';
import { act, cleanup, fireEvent } from '@testing-library/react';
import { render } from '../../../utils/theme-render-wrapper';
import { DatePicker } from './date-picker';
import type { DatePickerProps } from './date-picker';

beforeEach(() => {
  jest.spyOn(console, 'warn').mockImplementation(() => {
    // Disable a warning
  });
});

afterEach(cleanup);

const endDate = null;
const startDate = null;

const onChange = jest.fn();

const input: DatePickerProps['input'] = {
  value: { endDate, startDate },
  onChange
};

describe('<DatePicker />', () => {
  // To do - issue where we check for element disappearance but there's slight delay before it removed.
  //   it(`Should render DatePicker and open and close the menu with mouse`, () => {
  //     const { queryByTestId, container } = render(
  //       <DatePicker input={input} />
  //     );
  //     const openIconEl = queryByTestId('openIcon');
  //     openIconEl?.click();

  //     let popupEl = queryByTestId('popup');
  //     expect(popupEl).toBeTruthy();

  //     const weekContainerEl = popupEl?.querySelector('.react-datepicker__week');

  //     const day1El = weekContainerEl?.children[0] as HTMLElement;
  //     const day2El = weekContainerEl?.children[1] as HTMLElement;
  //     day1El?.click();
  //     day2El?.click();

  //     act(() => {
  //       document.dispatchEvent(new Event('mousedown'));

  //       popupEl = container.querySelector("[data-testid='popup']");
  //       expect(popupEl).toBeFalsy();
  //     });
  //   });

  it(`Should handle clear`, () => {
    const date = new Date();

    const { queryByTestId, container } = render(
      <DatePicker
        input={{
          onChange,
          value: {
            startDate: date,
            endDate: date
          }
        }}
      />
    );

    const clearEl = queryByTestId('clearIcon');
    clearEl?.click();

    const inputEl = container.querySelector('input');
    expect(inputEl?.value).toBeFalsy();
  });
});
