Skip to content

The platform for continuously improving AI agents.

1.4 KB
Raw Blame
// @vitest-environment jsdom
// covers: console-logic
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, expect, it, vi } from "vitest";

import { SearchableSelect } from "./searchable-select";

afterEach(() => {
  cleanup();
  vi.unstubAllGlobals();
});

it("finds a model by its exact ID and selects it with the keyboard", () => {
  const onChange = vi.fn();
  vi.stubGlobal(
    "ResizeObserver",
    class {
      observe() {}
      unobserve() {}
      disconnect() {}
    }
  );
  const scrollIntoView = Element.prototype.scrollIntoView;
  Element.prototype.scrollIntoView = () => {};
  try {
    render(
      <SearchableSelect
        ariaLabel="Model"
        onChange={onChange}
        options={[
          { label: "Small", value: "vendor/small" },
          { label: "Large", value: "vendor/large" },
        ]}
        value=""
      />
    );
    fireEvent.click(screen.getByRole("button", { name: "Model" }));
    const search = screen.getByRole("combobox");
    fireEvent.change(search, { target: { value: "vendor/large" } });
    expect(screen.getAllByRole("option")).toHaveLength(1);
    fireEvent.keyDown(search, { key: "Enter" });
    expect(onChange).toHaveBeenCalledExactlyOnceWith("vendor/large");
    expect(screen.queryByRole("combobox")).toBeNull();
  } finally {
    Element.prototype.scrollIntoView = scrollIntoView;
  }
});