Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

4.4 KB
Raw Blame
import { act, createElement, type ReactElement } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, test } from "vitest";
import { RadioGroup, RadioGroupItem } from "./radio-group";

(
  globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;

const renderIntoContainer = (element: ReactElement) => {
  const container = document.createElement("div");
  document.body.appendChild(container);
  const root = createRoot(container);
  act(() => {
    root.render(element);
  });
  return {
    container,
    unmount: () =>
      act(() => {
        root.unmount();
        container.remove();
      }),
  };
};

const getRadio = (container: HTMLElement) => {
  const radio = container.querySelector('[role="radio"]');
  if (!radio) throw new Error("radio not found");
  return radio as HTMLElement;
};

describe("RadioGroupItem", () => {
  afterEach(() => {
    document.body.innerHTML = "";
  });

  test("prevents the radio control from shrinking in long option labels", () => {
    const { container, unmount } = renderIntoContainer(
      createElement(
        RadioGroup,
        { value: "workspace", onValueChange: () => undefined },
        createElement(
          RadioGroupItem,
          { value: "workspace" },
          createElement(
            "span",
            null,
            "Use issue to request, review, rollout, and version database changes"
          )
        )
      )
    );

    expect(getRadio(container).className).toContain("size-4");
    expect(getRadio(container).className).toContain("shrink-0");

    unmount();
  });

  test("uses the semantic accent token for checked and focus states", () => {
    const { container, unmount } = renderIntoContainer(
      createElement(
        RadioGroup,
        { value: "workspace", onValueChange: () => undefined },
        createElement(RadioGroupItem, { value: "workspace" }, "Workspace")
      )
    );

    expect(getRadio(container).className).toContain(
      "data-[checked]:border-[rgb(var(--color-accent))]"
    );
    expect(getRadio(container).className).toContain(
      "focus-visible:ring-[rgb(var(--color-accent))]"
    );

    unmount();
  });

  test("allows the radio control to be offset for rich labels", () => {
    const { container, unmount } = renderIntoContainer(
      createElement(
        RadioGroup,
        { value: "workspace", onValueChange: () => undefined },
        createElement(
          RadioGroupItem,
          { value: "workspace", radioClassName: "mt-1" },
          createElement("div", null, "Workspace")
        )
      )
    );

    expect(getRadio(container).className).toContain("mt-1");

    unmount();
  });

  // The cursor follows the radio's resolved disabled state rather than this
  // component's own prop, because Base UI resolves that state from the group
  // too. Asserted on the attribute the CSS selects: `[data-disabled]` on the
  // radio, as a direct child of the label.
  test.each([
    ["the option's own prop", { group: false }],
    ["the group's disabled state", { group: true }],
  ])("a not-allowed cursor follows %s", (_label, { group }) => {
    const { container, unmount } = renderIntoContainer(
      createElement(
        RadioGroup,
        {
          value: "workspace",
          onValueChange: () => undefined,
          ...(group ? { disabled: true } : {}),
        },
        createElement(
          RadioGroupItem,
          { value: "workspace", ...(group ? {} : { disabled: true }) },
          "Workspace"
        )
      )
    );

    expect(container.querySelector("label > [data-disabled]")).not.toBeNull();

    unmount();
  });

  // The selector is scoped to the direct child because an item may wrap a
  // disabled control of its own — InstanceFormBody's "Custom" option holds a
  // number input that is disabled until that option is picked.
  test("a disabled control inside an option does not disable the option", () => {
    const { container, unmount } = renderIntoContainer(
      createElement(
        RadioGroup,
        { value: "default", onValueChange: () => undefined },
        createElement(
          RadioGroupItem,
          { value: "custom" },
          createElement("input", { type: "number", disabled: true })
        )
      )
    );

    expect(container.querySelector("input:disabled")).not.toBeNull();
    expect(container.querySelector("label > [data-disabled]")).toBeNull();

    unmount();
  });
});