import { cleanup, fireEvent } from '@testing-library/react';

import { ASSETS_URL } from '../../../consts/common';
import { ThemeProvider } from '../../@styles/theme-provider';
import { render } from '../../../utils/theme-render-wrapper';

import { AgentMinVersion } from './agent-min-version';
import type { AgentMinVersionProps } from './agent-min-version';

afterEach(cleanup);

const cellClickHandler = jest.fn();

const mockGroupCard: AgentMinVersionProps = {
  title: 'Windows',
  subtitle: 'Current Version: 7.2.0.125',
  icon: `${ASSETS_URL}/icons/icon_windows.svg`,
  valueList: [
    { name: '7.2.0.125', selected: false },
    { name: '7.2.12.8', selected: false },
    { name: '7.2.0.125', selected: false },
    { name: '7.2.12.11', selected: false },
    { name: '7.2.12.5', selected: false }
  ],
  btnOnClick: cellClickHandler
};

describe('<AgentMinVersion />', () => {
  it('should render successfully', () => {
    const { baseElement } = render(<AgentMinVersion {...mockGroupCard} />);
    expect(baseElement).toBeTruthy();
  });

  it('should focus and blur', () => {
    const { baseElement, getByRole } = render(
      <AgentMinVersion
        active={true}
        hovered={true}
        indicator={false}
        indicatorColor="inherit"
        {...mockGroupCard}
      />
    );
    fireEvent.blur(baseElement);

    const rowEl = getByRole('row');
    fireEvent.focus(rowEl);
    fireEvent.blur(rowEl);
  });

  it('should render mock and click', () => {
    const { getAllByRole, getAllByText, getByTestId } = render(
      <ThemeProvider>
        <AgentMinVersion onCellClick={cellClickHandler} {...mockGroupCard} />
      </ThemeProvider>
    );

    const firstCellEl = getAllByRole('cell')[0];
    firstCellEl.click();

    const selectEl = getByTestId('select');
    selectEl.click();

    const valueName = mockGroupCard.valueList[1].name;
    const valueNameEl = getAllByText(valueName);
    valueNameEl[0].click();

    const buttonEl = getByTestId('button');
    buttonEl.click();

    expect(cellClickHandler).toHaveBeenCalledTimes(4);
  });
});
