workspacePurposeSection.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. import { WorkspacePurposeSection } from "@/WorkspaceSidebar/WorkspacePurposeSection.js";
  5. const sortable = vi.hoisted(() => ({
  6. active: null as { id: string } | null,
  7. activeIndex: -1,
  8. index: 0,
  9. isOver: false,
  10. isDragging: false,
  11. }));
  12. vi.mock("@dnd-kit/sortable", () => ({
  13. useSortable: () => ({
  14. attributes: { role: "button", tabIndex: 0 },
  15. listeners: {},
  16. setActivatorNodeRef: vi.fn(),
  17. setNodeRef: vi.fn(),
  18. transform: null,
  19. transition: undefined,
  20. ...sortable,
  21. }),
  22. }));
  23. vi.mock("@dnd-kit/utilities", () => ({
  24. CSS: { Transform: { toString: vi.fn() } },
  25. }));
  26. function renderSection(open: boolean, inset = false) {
  27. return renderToStaticMarkup(
  28. createElement(
  29. WorkspacePurposeSection,
  30. {
  31. title: "Projects",
  32. open,
  33. onOpenChange: vi.fn(),
  34. action: createElement("button", { "data-testid": "section-action" }, "Add"),
  35. testId: "purpose-section",
  36. sortableId: "projects",
  37. dragHandleLabel: "Move Projects section",
  38. inset,
  39. },
  40. createElement("div", { "data-testid": "section-content" }, "Details"),
  41. ),
  42. );
  43. }
  44. describe("WorkspacePurposeSection", () => {
  45. beforeEach(() => {
  46. Object.assign(sortable, {
  47. active: null,
  48. activeIndex: -1,
  49. index: 0,
  50. isOver: false,
  51. isDragging: false,
  52. });
  53. });
  54. it("renders the expanded state with content and a down chevron", () => {
  55. const html = renderSection(true);
  56. expect(html).toContain('aria-expanded="true"');
  57. expect(html).toContain('data-purpose-section-chevron="expanded"');
  58. expect(html).toContain('data-testid="section-content"');
  59. });
  60. it("renders the collapsed state with a right chevron and hidden content", () => {
  61. const html = renderSection(false);
  62. expect(html).toContain('aria-expanded="false"');
  63. expect(html).toContain('data-purpose-section-chevron="collapsed"');
  64. expect(html).not.toContain('data-testid="section-content"');
  65. });
  66. it("keeps the action outside the collapse trigger and exposes accessible visibility fallbacks", () => {
  67. const html = renderSection(true);
  68. const triggerEndIndex = html.indexOf("</button>");
  69. const dragTriggerIndex = html.indexOf('data-purpose-section-drag-trigger="projects"');
  70. const actionIndex = html.indexOf('data-testid="section-action"');
  71. expect(triggerEndIndex).toBeGreaterThan(-1);
  72. expect(dragTriggerIndex).toBeGreaterThan(-1);
  73. expect(dragTriggerIndex).toBeLessThan(triggerEndIndex);
  74. expect(actionIndex).toBeGreaterThan(triggerEndIndex);
  75. expect(html).not.toContain("data-purpose-section-drag-handle");
  76. expect(html).not.toContain("lucide-grip-vertical");
  77. expect(html).toContain('aria-description="Move Projects section"');
  78. expect(html).toContain("touch-none");
  79. expect(html).toContain("group-hover/purpose-section:opacity-100");
  80. expect(html).toContain("group-focus-within/purpose-section:opacity-100");
  81. expect(html).toContain("has-[[data-state=open]]:opacity-100");
  82. expect(html).toContain("[@media(hover:none)]:opacity-100");
  83. });
  84. it.each([
  85. [1, 0, "before"],
  86. [0, 1, "after"],
  87. ] as const)(
  88. "shows the target edge for source %s and target %s",
  89. (activeIndex, index, position) => {
  90. Object.assign(sortable, {
  91. active: { id: "conversations" },
  92. activeIndex,
  93. index,
  94. isOver: true,
  95. });
  96. const html = renderSection(true);
  97. expect(html).toContain('data-testid="purpose-section-drop-indicator"');
  98. expect(html).toContain(`data-drop-position="${position}"`);
  99. expect(html).toContain("bg-icon-blue");
  100. expect(html).toContain("pointer-events-none");
  101. expect(html).toContain("border-icon-blue");
  102. },
  103. );
  104. it("does not mark the dragged section, unrelated targets, or cancelled drags", () => {
  105. Object.assign(sortable, {
  106. active: { id: "projects" },
  107. activeIndex: 0,
  108. isOver: true,
  109. isDragging: true,
  110. });
  111. expect(renderSection(true)).not.toContain("purpose-section-drop-indicator");
  112. Object.assign(sortable, { isDragging: false, isOver: false });
  113. expect(renderSection(true)).not.toContain("purpose-section-drop-indicator");
  114. Object.assign(sortable, { active: null, isOver: true });
  115. expect(renderSection(true)).not.toContain("purpose-section-drop-indicator");
  116. });
  117. it("uses one shared child inset when rendered below a Source band", () => {
  118. const html = renderSection(true, true);
  119. expect(html).toContain('data-source-hierarchy-children="true"');
  120. expect(html).toContain("ml-6");
  121. expect(html).toContain("mr-1");
  122. });
  123. });