import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { Switch } from './switch';

describe('Switch Component', () => {
  it('renders switch', () => {
    render(<Switch />);

    const switchElement = screen.getByRole('checkbox');
    expect(switchElement).toBeInTheDocument();
    expect(switchElement).not.toBeChecked();
  });

  it('renders switch element', () => {
    render(<Switch />);

    const switchElement = screen.getByRole('checkbox');
    expect(switchElement).toBeInTheDocument();
  });

  it('handles checked state', () => {
    render(<Switch checked />);

    const switchElement = screen.getByRole('checkbox');
    expect(switchElement).toBeChecked();
  });

  it('handles onChange callback', () => {
    const handleChange = vi.fn();
    render(<Switch onChange={handleChange} />);

    const switchElement = screen.getByRole('checkbox');
    fireEvent.click(switchElement);

    expect(handleChange).toHaveBeenCalledTimes(1);
  });

  it('handles onCheckedChange callback', () => {
    const handleCheckedChange = vi.fn();
    render(<Switch onCheckedChange={handleCheckedChange} />);

    const switchElement = screen.getByRole('checkbox');
    fireEvent.click(switchElement);

    expect(handleCheckedChange).toHaveBeenCalledWith(true);
  });

  it('handles disabled state', () => {
    render(<Switch disabled />);

    const switchElement = screen.getByRole('checkbox');
    expect(switchElement).toBeDisabled();
  });

  it('toggles checked state on click', () => {
    render(<Switch />);

    const switchElement = screen.getByRole('checkbox');
    expect(switchElement).not.toBeChecked();

    fireEvent.click(switchElement);
    expect(switchElement).toBeChecked();

    fireEvent.click(switchElement);
    expect(switchElement).not.toBeChecked();
  });

  it('applies custom className', () => {
    render(<Switch className="custom-switch" />);

    const switchElement = screen.getByRole('checkbox');
    expect(switchElement.closest('label')).toHaveClass('custom-switch');
  });
});
