import * as React from 'react';
import {
  render,
  screen,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as ButtonBase from '@tesseract-design/web-base-button';
import { vi } from 'vitest';
import {
  ActionButton,
  ActionButtonType,
} from '.';

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

describe('ActionButton', () => {
  it('renders a button', () => {
    render(
      <ActionButton />
    );
    const button: HTMLButtonElement = screen.getByRole('button');
    expect(button).toBeInTheDocument();
    expect(button).toHaveProperty('type', 'button');
  });

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

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

  it('renders as a menu item', () => {
    render(
      <ActionButton
        menuItem
      />
    );
    const menuItemIndicator: HTMLElement = screen.getByTestId('menuItemIndicator');
    expect(menuItemIndicator).toBeInTheDocument();
  });

  it('handles click events', () => {
    const onClick = vi.fn().mockImplementationOnce((e) => { e.preventDefault() });
    render(
      <ActionButton
        onClick={onClick}
      />
    );
    const button: HTMLButtonElement = screen.getByRole('button');
    userEvent.click(button);
    expect(onClick).toBeCalled();
  });

  it('renders a compact button', () => {
    render(
      <ActionButton
        compact
      />
    );

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

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

  describe.each(Object.values(ButtonBase.ButtonSize))('on %s size', (size) => {
    it('renders button styles', () => {
      render(
        <ActionButton
          size={size}
        />
      );

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

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

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

    it('renders indicator styles', () => {
      render(
        <ActionButton
          size={size}
          menuItem
        />
      );

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

  it.each(Object.values(ButtonBase.ButtonVariant))('renders a button with variant %s', (variant) => {
    render(
      <ActionButton
        variant={variant}
      />
    );

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

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

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

  it('renders a block button', () => {
    render(
      <ActionButton
        block
      />
    );

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

  it('renders children', () => {
    render(
      <ActionButton>
        Foo
      </ActionButton>
    );

    const children: HTMLElement = screen.getByTestId('children');
    expect(children).toHaveTextContent('Foo');
  });

  it.each(Object.values(ActionButtonType))('renders a button with type %s', (buttonType) => {
    render(
      <ActionButton
        type={buttonType}
      />
    );
    const button: HTMLButtonElement = screen.getByRole('button');
    expect(button).toHaveProperty('type', buttonType);
  });

  it('renders a disabled button', () => {
    render(
      <ActionButton
        disabled
      />
    );
    const button: HTMLButtonElement = screen.getByRole('button');
    expect(button).toBeDisabled();
  });
});
