thoughtLevelCycleControlSelectionValue.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import {
  4. act,
  5. cleanup,
  6. fireEvent,
  7. render,
  8. screen,
  9. } from "@testing-library/react";
  10. import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
  11. import type { ZCodeConfigOption } from "@zcode/shared";
  12. import { ThoughtLevelCycleControl } from "@/chat-input-toolbar/ThoughtLevelCycleControl.js";
  13. import { TooltipProvider } from "@/components/ui/tooltip.js";
  14. class TestPointerEvent extends MouseEvent {
  15. readonly pointerId: number;
  16. readonly pointerType: string;
  17. constructor(
  18. type: string,
  19. init: MouseEventInit & { pointerId?: number; pointerType?: string } = {},
  20. ) {
  21. super(type, init);
  22. this.pointerId = init.pointerId ?? 1;
  23. this.pointerType = init.pointerType ?? "mouse";
  24. }
  25. }
  26. const intl = {
  27. formatMessage({ id }: { id: string }) {
  28. return id;
  29. },
  30. };
  31. const option: ZCodeConfigOption = {
  32. id: "thought_level",
  33. category: "thought_level",
  34. type: "select",
  35. currentValue: "high",
  36. options: [
  37. { value: "low", name: "Low" },
  38. { value: "high", name: "High" },
  39. ],
  40. };
  41. function renderControl(params?: {
  42. disabled?: boolean;
  43. onCurrentValueCommit?: (value: string) => void;
  44. onValueChange?: (value: string) => void;
  45. }) {
  46. return render(
  47. createElement(
  48. TooltipProvider,
  49. null,
  50. createElement(ThoughtLevelCycleControl, {
  51. disabled: params?.disabled,
  52. intl,
  53. onCurrentValueCommit: params?.onCurrentValueCommit,
  54. onValueChange: params?.onValueChange ?? vi.fn(),
  55. option,
  56. restoreFocusSelector: null,
  57. triggerRef: { current: null },
  58. }),
  59. ),
  60. );
  61. }
  62. function openSelect() {
  63. const trigger = screen.getByRole("combobox");
  64. fireEvent.keyDown(trigger, { key: "ArrowDown" });
  65. return trigger;
  66. }
  67. function getOption(value: string) {
  68. return screen.getByTestId(`chat-thought-level-select-item-${value}`);
  69. }
  70. describe("ThoughtLevelCycleControl selection commit", () => {
  71. beforeAll(() => {
  72. Object.defineProperty(window, "PointerEvent", {
  73. configurable: true,
  74. value: TestPointerEvent,
  75. });
  76. Object.defineProperties(HTMLElement.prototype, {
  77. hasPointerCapture: {
  78. configurable: true,
  79. value: () => false,
  80. },
  81. releasePointerCapture: {
  82. configurable: true,
  83. value: () => undefined,
  84. },
  85. scrollIntoView: {
  86. configurable: true,
  87. value: () => undefined,
  88. },
  89. });
  90. });
  91. afterEach(() => {
  92. cleanup();
  93. });
  94. it("keeps the effective default selected and commits a mouse confirmation", async () => {
  95. const onValueChange = vi.fn();
  96. const onCurrentValueCommit = vi.fn();
  97. renderControl({ onCurrentValueCommit, onValueChange });
  98. const trigger = openSelect();
  99. const currentOption = getOption("high");
  100. expect(currentOption.getAttribute("data-state")).toBe("checked");
  101. fireEvent.pointerDown(currentOption, {
  102. button: 0,
  103. pointerId: 2,
  104. pointerType: "mouse",
  105. });
  106. fireEvent.pointerUp(currentOption, {
  107. button: 0,
  108. pointerId: 2,
  109. pointerType: "mouse",
  110. });
  111. await act(async () => undefined);
  112. expect(trigger.getAttribute("aria-expanded")).toBe("false");
  113. expect(onCurrentValueCommit).toHaveBeenCalledOnce();
  114. expect(onCurrentValueCommit).toHaveBeenCalledWith("high");
  115. expect(onValueChange).not.toHaveBeenCalled();
  116. });
  117. it.each(["Enter", " "])(
  118. "commits a keyboard confirmation with %s",
  119. async (key) => {
  120. const onCurrentValueCommit = vi.fn();
  121. renderControl({ onCurrentValueCommit });
  122. openSelect();
  123. fireEvent.keyDown(getOption("high"), { key });
  124. await act(async () => undefined);
  125. expect(onCurrentValueCommit).toHaveBeenCalledOnce();
  126. expect(onCurrentValueCommit).toHaveBeenCalledWith("high");
  127. },
  128. );
  129. it("commits a touch tap on the current value", async () => {
  130. const onCurrentValueCommit = vi.fn();
  131. renderControl({ onCurrentValueCommit });
  132. openSelect();
  133. const currentOption = getOption("high");
  134. fireEvent.pointerDown(currentOption, {
  135. button: 0,
  136. pointerId: 3,
  137. pointerType: "touch",
  138. });
  139. fireEvent.pointerUp(currentOption, {
  140. button: 0,
  141. pointerId: 3,
  142. pointerType: "touch",
  143. });
  144. fireEvent.click(currentOption);
  145. await act(async () => undefined);
  146. expect(onCurrentValueCommit).toHaveBeenCalledOnce();
  147. expect(onCurrentValueCommit).toHaveBeenCalledWith("high");
  148. });
  149. it("does not commit a touch scroll that ends over the current item", async () => {
  150. const onCurrentValueCommit = vi.fn();
  151. renderControl({ onCurrentValueCommit });
  152. openSelect();
  153. const currentOption = getOption("high");
  154. fireEvent.pointerDown(currentOption, {
  155. button: 0,
  156. clientY: 10,
  157. pointerId: 4,
  158. pointerType: "touch",
  159. });
  160. fireEvent.pointerMove(currentOption, {
  161. button: 0,
  162. clientY: 80,
  163. pointerId: 4,
  164. pointerType: "touch",
  165. });
  166. fireEvent.pointerUp(currentOption, {
  167. button: 0,
  168. clientY: 80,
  169. pointerId: 4,
  170. pointerType: "touch",
  171. });
  172. await act(async () => undefined);
  173. expect(onCurrentValueCommit).not.toHaveBeenCalled();
  174. });
  175. it("does not treat typeahead Space as a selection commit", async () => {
  176. const onCurrentValueCommit = vi.fn();
  177. renderControl({ onCurrentValueCommit });
  178. openSelect();
  179. const currentOption = getOption("high");
  180. fireEvent.keyDown(currentOption, { key: "h" });
  181. fireEvent.keyDown(currentOption, { key: " " });
  182. await act(async () => undefined);
  183. expect(onCurrentValueCommit).not.toHaveBeenCalled();
  184. });
  185. });