import * as React from 'react';
import {
  render,
  screen
} from '@testing-library/react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
import * as ButtonBase from '@tesseract-design/web-base-button';
import * as CheckControlBase from '@tesseract-design/web-base-checkcontrol';
import { RadioButton } from '.';

jest.mock('@tesseract-design/web-base-button');
jest.mock('@tesseract-design/web-base-checkcontrol');

describe('RadioButton', () => {
  it('should render a radio button', () => {
    render(
      <RadioButton />
    );
    const checkbox = screen.getByRole('radio');
    expect(checkbox).toBeInTheDocument();
  });

  it('should render a subtext', () => {
    render(
      <RadioButton
        subtext="subtext"
      />
    );
    const subtext: HTMLElement = screen.getByTestId('subtext');
    expect(subtext).toBeInTheDocument();
  });

  it('should render a badge', () => {
    render(
      <RadioButton
        badge="badge"
      />
    );
    const badge: HTMLElement = screen.getByTestId('badge');
    expect(badge).toBeInTheDocument();
  });
  
  it('should handle click events', () => {
    const onClick = jest.fn();
    render(
      <RadioButton
        onClick={onClick}
      />
    );
    const button: HTMLInputElement = screen.getByRole('radio');
    userEvent.click(button);
    expect(onClick).toBeCalled();
  });

  it('should render a compact button', () => {
    render(
      <RadioButton
        compact
      />
    );

    expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
      compact: true,
    }));

    expect(ButtonBase.Label).toBeCalledWith(expect.objectContaining({
      compact: true,
    }));
  });
  
  describe.each([
    ButtonBase.ButtonSize.SMALL,
    ButtonBase.ButtonSize.MEDIUM,
    ButtonBase.ButtonSize.LARGE,
  ])('on %s size', (size) => {
    it('should render button styles', () => {
      render(
        <RadioButton
          size={size}
        />
      );

      expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
        size,
      }));
    });

    it('should render badge styles', () => {
      render(
        <RadioButton
          size={size}
          badge="badge"
        />
      );

      expect(ButtonBase.BadgeContainer).toBeCalledWith(expect.objectContaining({
        size,
      }));
    });
  });

  it.each([
    ButtonBase.ButtonVariant.OUTLINE,
    ButtonBase.ButtonVariant.FILLED,
  ])('should render a button with variant %s', (variant) => {
    render(
      <RadioButton
        variant={variant}
      />
    );

    expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
      variant,
    }));
  });

  it('should render a bordered button', () => {
    render(
      <RadioButton
        border
      />
    );

    expect(ButtonBase.Border).toBeCalledWith(expect.objectContaining({
      border: true,
    }));
  });

  it('should render a block button', () => {
    render(
      <RadioButton
        block
      />
    );

    expect(ButtonBase.Button).toBeCalledWith(expect.objectContaining({
      block: true,
    }));

    expect(ButtonBase.Border).toBeCalledWith(expect.objectContaining({
      block: true,
    }));

    expect(CheckControlBase.ClickAreaWrapper).toBeCalledWith(expect.objectContaining({
      block: true,
    }));
  });

  it('should render children', () => {
    render(
      <RadioButton>
        Foo
      </RadioButton>
    );

    const children: HTMLElement = screen.getByTestId('children');
    expect(children).toHaveTextContent('Foo');
  });
  
  it('should render a disabled button', () => {
    render(
      <RadioButton
        disabled
      />
    );
    const radio: HTMLButtonElement = screen.getByRole('radio');
    expect(radio).toBeDisabled();
  });

  it('should handle change events', () => {
    const onChange = jest.fn();
    render(
      <RadioButton
        onChange={onChange}
      />
    );
    const radio: HTMLInputElement = screen.getByRole('radio');
    userEvent.click(radio);
    expect(onChange).toBeCalled();
  });
});
