// packages/core/tests/components/dialog/dialog-rendering.test.tsx
import { render, screen } from "@solidjs/testing-library";
import { createSignal } from "solid-js";
import { describe, expect, it } from "vitest";
import { Dialog } from "../../../src/components/dialog/index";

describe("Dialog rendering", () => {
  it("renders children", () => {
    render(() => (
      <Dialog defaultOpen>
        <Dialog.Content>
          <Dialog.Title>Hello</Dialog.Title>
        </Dialog.Content>
      </Dialog>
    ));
    expect(screen.getByText("Hello")).toBeTruthy();
  });

  it("does not render content when closed by default", () => {
    render(() => (
      <Dialog>
        <Dialog.Content>
          <Dialog.Title>Hidden</Dialog.Title>
        </Dialog.Content>
      </Dialog>
    ));
    expect(screen.queryByText("Hidden")).toBeNull();
  });

  it("renders content when defaultOpen is true", () => {
    render(() => (
      <Dialog defaultOpen>
        <Dialog.Content>
          <Dialog.Title>Visible</Dialog.Title>
        </Dialog.Content>
      </Dialog>
    ));
    expect(screen.getByText("Visible")).toBeTruthy();
  });

  it("renders content when controlled open is true", () => {
    render(() => (
      <Dialog open={true} onOpenChange={() => {}}>
        <Dialog.Content>
          <Dialog.Title>Controlled</Dialog.Title>
        </Dialog.Content>
      </Dialog>
    ));
    expect(screen.getByText("Controlled")).toBeTruthy();
  });

  it("closes when controlled open is set to false", () => {
    const [open, setOpen] = createSignal(true);
    render(() => (
      <Dialog open={open()} onOpenChange={setOpen}>
        <Dialog.Content>
          <Dialog.Title>Toggled</Dialog.Title>
        </Dialog.Content>
      </Dialog>
    ));
    expect(screen.getByText("Toggled")).toBeTruthy();
    setOpen(false);
    expect(screen.queryByText("Toggled")).toBeNull();
  });
});
