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

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

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

  it('renders a compact switch', () => {
    render(
      <ToggleSwitch
        compact
      />
    );

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

  it('renders a block switch', () => {
    render(
      <ToggleSwitch
        block
      />
    );

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

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

  it('handles click events', () => {
    const onClick = vi.fn().mockImplementationOnce((e) => { e.preventDefault(); })
    render(
      <ToggleSwitch
        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(
      <ToggleSwitch
        onChange={onChange}
      />
    );
    const checkbox: HTMLInputElement = screen.getByRole('checkbox');
    userEvent.click(checkbox);
    expect(onChange).toBeCalled();
  });
});
