automationInstructionsComposer.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 { AutomationInstructionsTextarea } from "@/settings/AutomationInstructionsComposer.js";
  6. afterEach(() => {
  7. cleanup();
  8. });
  9. function ControlledInstructions() {
  10. const [value, setValue] = useState("");
  11. return createElement(AutomationInstructionsTextarea, {
  12. "aria-label": "Instructions",
  13. value,
  14. onChange: (event) => setValue(event.target.value),
  15. });
  16. }
  17. describe("AutomationInstructionsTextarea", () => {
  18. it("grows through seven lines, then scrolls, and shrinks after content is deleted", () => {
  19. const originalScrollHeight = Object.getOwnPropertyDescriptor(
  20. HTMLTextAreaElement.prototype,
  21. "scrollHeight",
  22. );
  23. Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", {
  24. configurable: true,
  25. get() {
  26. const lineCount = Math.max(1, this.value.split("\n").length);
  27. return lineCount * 20 + 16;
  28. },
  29. });
  30. try {
  31. render(createElement(ControlledInstructions));
  32. const textarea = screen.getByRole("textbox", {
  33. name: "Instructions",
  34. }) as HTMLTextAreaElement;
  35. expect(textarea.style.height).toBe("116px");
  36. expect(textarea.style.overflowY).toBe("hidden");
  37. fireEvent.input(textarea, {
  38. target: { value: Array.from({ length: 6 }, () => "line").join("\n") },
  39. });
  40. expect(textarea.style.height).toBe("136px");
  41. expect(textarea.style.overflowY).toBe("hidden");
  42. fireEvent.input(textarea, {
  43. target: { value: Array.from({ length: 8 }, () => "line").join("\n") },
  44. });
  45. expect(textarea.style.height).toBe("156px");
  46. expect(textarea.style.overflowY).toBe("auto");
  47. fireEvent.input(textarea, { target: { value: "line" } });
  48. expect(textarea.style.height).toBe("116px");
  49. expect(textarea.style.overflowY).toBe("hidden");
  50. } finally {
  51. if (originalScrollHeight) {
  52. Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", originalScrollHeight);
  53. } else {
  54. delete (HTMLTextAreaElement.prototype as unknown as Record<string, unknown>).scrollHeight;
  55. }
  56. }
  57. });
  58. it("preserves a desktop manual height while long content keeps internal scrolling", () => {
  59. const originalScrollHeight = Object.getOwnPropertyDescriptor(
  60. HTMLTextAreaElement.prototype,
  61. "scrollHeight",
  62. );
  63. Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", {
  64. configurable: true,
  65. get() {
  66. const lineCount = Math.max(1, this.value.split("\n").length);
  67. return lineCount * 20 + 16;
  68. },
  69. });
  70. try {
  71. render(createElement(ControlledInstructions));
  72. const textarea = screen.getByRole("textbox", {
  73. name: "Instructions",
  74. }) as HTMLTextAreaElement;
  75. textarea.style.height = "220px";
  76. fireEvent.input(textarea, {
  77. target: {
  78. value: Array.from({ length: 12 }, () => "line").join("\n"),
  79. },
  80. });
  81. expect(textarea.style.height).toBe("220px");
  82. expect(textarea.style.overflowY).toBe("auto");
  83. fireEvent.input(textarea, { target: { value: "line" } });
  84. expect(textarea.style.height).toBe("220px");
  85. expect(textarea.style.overflowY).toBe("hidden");
  86. } finally {
  87. if (originalScrollHeight) {
  88. Object.defineProperty(HTMLTextAreaElement.prototype, "scrollHeight", originalScrollHeight);
  89. } else {
  90. delete (HTMLTextAreaElement.prototype as unknown as Record<string, unknown>).scrollHeight;
  91. }
  92. }
  93. });
  94. });