import { render, screen, fireEvent } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { ToggleSwitch } from "./ToggleSwitch";

test("renders unchecked", () => {
  render(<ToggleSwitch checked={false} onChange={() => {}} />);
  expect(screen.getByRole("checkbox")).not.toBeChecked();
});

test("renders checked", () => {
  render(<ToggleSwitch checked={true} onChange={() => {}} />);
  expect(screen.getByRole("checkbox")).toBeChecked();
});

test("calls onChange with true when clicked unchecked", () => {
  const onChange = vi.fn();

  render(<ToggleSwitch checked={false} onChange={onChange} />);
  fireEvent.click(screen.getByRole("checkbox"));

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

test("calls onChange with false when clicked checked", () => {
  const onChange = vi.fn();

  render(<ToggleSwitch checked={true} onChange={onChange} />);
  fireEvent.click(screen.getByRole("checkbox"));

  expect(onChange).toHaveBeenCalledWith(false);
});
