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 CheckControlBase from '@tesseract-design/web-base-checkcontrol';
import { vi } from 'vitest';
import { ToggleTickBox } from '.';

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

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

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

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

  it('renders a compact tick box', () => {
    render(
      <ToggleTickBox
        compact
      />
    );

    expect(CheckControlBase.CheckIndicatorArea).toBeCalledWith(expect.objectContaining({
      compact: true,
    }));
  });

  it('renders a block tick box', () => {
    render(
      <ToggleTickBox
        block
      />
    );

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

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

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

  it('handles change events', () => {
    const onChange = vi.fn().mockImplementationOnce((e) => { e.preventDefault(); })
    render(
      <ToggleTickBox
        onChange={onChange}
      />
    );
    const checkbox: HTMLInputElement = screen.getByRole('checkbox');
    userEvent.click(checkbox);
    expect(onChange).toBeCalled();
  });
});
