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 { ToggleButton } from '.';

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

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

  it('should render an indeterminate checkbox', () => {
    render(
      <ToggleButton
        indeterminate
      />
    );
    const checkbox = screen.getByRole('checkbox');
    expect(checkbox).toHaveProperty('indeterminate', true);
  });

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

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

  describe('on indeterminate', () => {
    it('should render an indeterminate checkbox', () => {
      render(
        <ToggleButton
          indeterminate
        />
      );
      const checkbox = screen.getByRole('checkbox');
      expect(checkbox).toHaveProperty('indeterminate', true);
    });

    it('should acknowledge passed ref', () => {
      const ref = React.createRef<HTMLInputElement>()
      render(
        <ToggleButton
          indeterminate
          ref={ref}
        />
      );
      expect(ref.current).toHaveProperty('indeterminate', true);
    });
  });

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

  it('should render a compact button', () => {
    render(
      <ToggleButton
        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(
        <ToggleButton
          size={size}
        />
      );

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

    it('should render badge styles', () => {
      render(
        <ToggleButton
          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(
      <ToggleButton
        variant={variant}
      />
    );

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

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

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

  it('should render a block button', () => {
    render(
      <ToggleButton
        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(
      <ToggleButton>
        Foo
      </ToggleButton>
    );

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

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