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

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

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

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

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

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

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

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

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