automationInstructionsComposer.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // @vitest-environment jsdom
  2. import { createElement, useState } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import {
  6. AUTOMATION_INSTRUCTIONS_TOOLBAR_TRIGGER_CLASSNAME,
  7. AutomationInstructionsComposer,
  8. AutomationInstructionsTextarea,
  9. } from "@/settings/AutomationInstructionsComposer.js";
  10. afterEach(() => {
  11. cleanup();
  12. });
  13. function ControlledInstructions() {
  14. const [value, setValue] = useState("");
  15. return createElement(AutomationInstructionsTextarea, {
  16. "aria-label": "Instructions",
  17. value,
  18. onChange: (event) => setValue(event.target.value),
  19. });
  20. }
  21. describe("AutomationInstructionsTextarea", () => {
  22. it("moves the shared Input border states from the surface parent to the textarea", () => {
  23. const { container } = render(
  24. createElement(
  25. AutomationInstructionsComposer,
  26. null,
  27. createElement(ControlledInstructions),
  28. ),
  29. );
  30. const composer = container.firstElementChild;
  31. const textarea = screen.getByRole("textbox", { name: "Instructions" });
  32. expect(composer?.classList.contains("border-input-border")).toBe(false);
  33. expect(composer?.classList.contains("border-0")).toBe(true);
  34. expect(composer?.classList.contains("bg-surface")).toBe(true);
  35. expect(textarea.classList).toContain("border-input-border");
  36. expect(textarea.classList).toContain("hover:border-input-border-hover");
  37. expect(textarea.classList).toContain("focus:border-input-border-focused");
  38. });
  39. it("uses secondary color for idle controls until hover or menu expansion", () => {
  40. const classes =
  41. AUTOMATION_INSTRUCTIONS_TOOLBAR_TRIGGER_CLASSNAME.split(" ");
  42. expect(classes).toContain("text-foreground-subtle");
  43. expect(classes).not.toContain("text-foreground");
  44. expect(classes).toContain("hover:text-foreground");
  45. expect(classes).toContain("aria-expanded:text-foreground");
  46. });
  47. it("disables manual resize on desktop and mobile", () => {
  48. render(createElement(ControlledInstructions));
  49. const textarea = screen.getByRole("textbox", {
  50. name: "Instructions",
  51. });
  52. expect(textarea.classList).toContain("resize-none");
  53. expect(textarea.classList).not.toContain("sm:resize-y");
  54. expect(textarea.classList).toContain("max-h-39");
  55. expect(textarea.classList).not.toContain("sm:max-h-none");
  56. });
  57. it("grows through seven lines, then scrolls, and shrinks after content is deleted", () => {
  58. const originalScrollHeight = Object.getOwnPropertyDescriptor(
  59. HTMLTextAreaElement.prototype,
  60. "scrollHeight",
  61. );
  62. Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", {
  63. configurable: true,
  64. get() {
  65. const lineCount = Math.max(1, this.value.split("\n").length);
  66. return lineCount * 20 + 16;
  67. },
  68. });
  69. try {
  70. render(createElement(ControlledInstructions));
  71. const textarea = screen.getByRole("textbox", {
  72. name: "Instructions",
  73. }) as HTMLTextAreaElement;
  74. expect(textarea.style.height).toBe("116px");
  75. expect(textarea.style.overflowY).toBe("hidden");
  76. fireEvent.input(textarea, {
  77. target: { value: Array.from({ length: 6 }, () => "line").join("\n") },
  78. });
  79. expect(textarea.style.height).toBe("136px");
  80. expect(textarea.style.overflowY).toBe("hidden");
  81. fireEvent.input(textarea, {
  82. target: { value: Array.from({ length: 8 }, () => "line").join("\n") },
  83. });
  84. expect(textarea.style.height).toBe("156px");
  85. expect(textarea.style.overflowY).toBe("auto");
  86. fireEvent.input(textarea, { target: { value: "line" } });
  87. expect(textarea.style.height).toBe("116px");
  88. expect(textarea.style.overflowY).toBe("hidden");
  89. } finally {
  90. if (originalScrollHeight) {
  91. Object.defineProperty(
  92. HTMLTextAreaElement.prototype,
  93. "scrollHeight",
  94. originalScrollHeight,
  95. );
  96. } else {
  97. delete (
  98. HTMLTextAreaElement.prototype as unknown as Record<string, unknown>
  99. ).scrollHeight;
  100. }
  101. }
  102. });
  103. it("preserves a desktop manual height while long content keeps internal scrolling", () => {
  104. const originalScrollHeight = Object.getOwnPropertyDescriptor(
  105. HTMLTextAreaElement.prototype,
  106. "scrollHeight",
  107. );
  108. Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", {
  109. configurable: true,
  110. get() {
  111. const lineCount = Math.max(1, this.value.split("\n").length);
  112. return lineCount * 20 + 16;
  113. },
  114. });
  115. try {
  116. render(createElement(ControlledInstructions));
  117. const textarea = screen.getByRole("textbox", {
  118. name: "Instructions",
  119. }) as HTMLTextAreaElement;
  120. textarea.style.height = "220px";
  121. fireEvent.input(textarea, {
  122. target: {
  123. value: Array.from({ length: 12 }, () => "line").join("\n"),
  124. },
  125. });
  126. expect(textarea.style.height).toBe("220px");
  127. expect(textarea.style.overflowY).toBe("auto");
  128. fireEvent.input(textarea, { target: { value: "line" } });
  129. expect(textarea.style.height).toBe("220px");
  130. expect(textarea.style.overflowY).toBe("hidden");
  131. } finally {
  132. if (originalScrollHeight) {
  133. Object.defineProperty(
  134. HTMLTextAreaElement.prototype,
  135. "scrollHeight",
  136. originalScrollHeight,
  137. );
  138. } else {
  139. delete (
  140. HTMLTextAreaElement.prototype as unknown as Record<string, unknown>
  141. ).scrollHeight;
  142. }
  143. }
  144. });
  145. });