| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- // @vitest-environment jsdom
- import { createElement } from "react";
- import {
- act,
- cleanup,
- fireEvent,
- render,
- screen,
- } from "@testing-library/react";
- import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
- import type { ZCodeConfigOption } from "@zcode/shared";
- import { ThoughtLevelCycleControl } from "@/chat-input-toolbar/ThoughtLevelCycleControl.js";
- import { TooltipProvider } from "@/components/ui/tooltip.js";
- class TestPointerEvent extends MouseEvent {
- readonly pointerId: number;
- readonly pointerType: string;
- constructor(
- type: string,
- init: MouseEventInit & { pointerId?: number; pointerType?: string } = {},
- ) {
- super(type, init);
- this.pointerId = init.pointerId ?? 1;
- this.pointerType = init.pointerType ?? "mouse";
- }
- }
- const intl = {
- formatMessage({ id }: { id: string }) {
- return id;
- },
- };
- const option: ZCodeConfigOption = {
- id: "thought_level",
- category: "thought_level",
- type: "select",
- currentValue: "high",
- options: [
- { value: "low", name: "Low" },
- { value: "high", name: "High" },
- ],
- };
- function renderControl(params?: {
- disabled?: boolean;
- onCurrentValueCommit?: (value: string) => void;
- onValueChange?: (value: string) => void;
- }) {
- return render(
- createElement(
- TooltipProvider,
- null,
- createElement(ThoughtLevelCycleControl, {
- disabled: params?.disabled,
- intl,
- onCurrentValueCommit: params?.onCurrentValueCommit,
- onValueChange: params?.onValueChange ?? vi.fn(),
- option,
- restoreFocusSelector: null,
- triggerRef: { current: null },
- }),
- ),
- );
- }
- function openSelect() {
- const trigger = screen.getByRole("combobox");
- fireEvent.keyDown(trigger, { key: "ArrowDown" });
- return trigger;
- }
- function getOption(value: string) {
- return screen.getByTestId(`chat-thought-level-select-item-${value}`);
- }
- describe("ThoughtLevelCycleControl selection commit", () => {
- beforeAll(() => {
- Object.defineProperty(window, "PointerEvent", {
- configurable: true,
- value: TestPointerEvent,
- });
- Object.defineProperties(HTMLElement.prototype, {
- hasPointerCapture: {
- configurable: true,
- value: () => false,
- },
- releasePointerCapture: {
- configurable: true,
- value: () => undefined,
- },
- scrollIntoView: {
- configurable: true,
- value: () => undefined,
- },
- });
- });
- afterEach(() => {
- cleanup();
- });
- it("keeps the effective default selected and commits a mouse confirmation", async () => {
- const onValueChange = vi.fn();
- const onCurrentValueCommit = vi.fn();
- renderControl({ onCurrentValueCommit, onValueChange });
- const trigger = openSelect();
- const currentOption = getOption("high");
- expect(currentOption.getAttribute("data-state")).toBe("checked");
- fireEvent.pointerDown(currentOption, {
- button: 0,
- pointerId: 2,
- pointerType: "mouse",
- });
- fireEvent.pointerUp(currentOption, {
- button: 0,
- pointerId: 2,
- pointerType: "mouse",
- });
- await act(async () => undefined);
- expect(trigger.getAttribute("aria-expanded")).toBe("false");
- expect(onCurrentValueCommit).toHaveBeenCalledOnce();
- expect(onCurrentValueCommit).toHaveBeenCalledWith("high");
- expect(onValueChange).not.toHaveBeenCalled();
- });
- it.each(["Enter", " "])(
- "commits a keyboard confirmation with %s",
- async (key) => {
- const onCurrentValueCommit = vi.fn();
- renderControl({ onCurrentValueCommit });
- openSelect();
- fireEvent.keyDown(getOption("high"), { key });
- await act(async () => undefined);
- expect(onCurrentValueCommit).toHaveBeenCalledOnce();
- expect(onCurrentValueCommit).toHaveBeenCalledWith("high");
- },
- );
- it("commits a touch tap on the current value", async () => {
- const onCurrentValueCommit = vi.fn();
- renderControl({ onCurrentValueCommit });
- openSelect();
- const currentOption = getOption("high");
- fireEvent.pointerDown(currentOption, {
- button: 0,
- pointerId: 3,
- pointerType: "touch",
- });
- fireEvent.pointerUp(currentOption, {
- button: 0,
- pointerId: 3,
- pointerType: "touch",
- });
- fireEvent.click(currentOption);
- await act(async () => undefined);
- expect(onCurrentValueCommit).toHaveBeenCalledOnce();
- expect(onCurrentValueCommit).toHaveBeenCalledWith("high");
- });
- it("does not commit a touch scroll that ends over the current item", async () => {
- const onCurrentValueCommit = vi.fn();
- renderControl({ onCurrentValueCommit });
- openSelect();
- const currentOption = getOption("high");
- fireEvent.pointerDown(currentOption, {
- button: 0,
- clientY: 10,
- pointerId: 4,
- pointerType: "touch",
- });
- fireEvent.pointerMove(currentOption, {
- button: 0,
- clientY: 80,
- pointerId: 4,
- pointerType: "touch",
- });
- fireEvent.pointerUp(currentOption, {
- button: 0,
- clientY: 80,
- pointerId: 4,
- pointerType: "touch",
- });
- await act(async () => undefined);
- expect(onCurrentValueCommit).not.toHaveBeenCalled();
- });
- it("does not treat typeahead Space as a selection commit", async () => {
- const onCurrentValueCommit = vi.fn();
- renderControl({ onCurrentValueCommit });
- openSelect();
- const currentOption = getOption("high");
- fireEvent.keyDown(currentOption, { key: "h" });
- fireEvent.keyDown(currentOption, { key: " " });
- await act(async () => undefined);
- expect(onCurrentValueCommit).not.toHaveBeenCalled();
- });
- });
|