workspacePurposeSection.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { WorkspacePurposeSection } from "@/WorkspaceSidebar/WorkspacePurposeSection.js";
  5. vi.mock("@dnd-kit/sortable", () => ({
  6. useSortable: () => ({
  7. attributes: { role: "button", tabIndex: 0 },
  8. listeners: {},
  9. setActivatorNodeRef: vi.fn(),
  10. setNodeRef: vi.fn(),
  11. transform: null,
  12. transition: undefined,
  13. isDragging: false,
  14. }),
  15. }));
  16. vi.mock("@dnd-kit/utilities", () => ({
  17. CSS: { Transform: { toString: vi.fn() } },
  18. }));
  19. function renderSection(open: boolean) {
  20. return renderToStaticMarkup(
  21. createElement(
  22. WorkspacePurposeSection,
  23. {
  24. title: "Projects",
  25. open,
  26. onOpenChange: vi.fn(),
  27. action: createElement("button", { "data-testid": "section-action" }, "Add"),
  28. testId: "purpose-section",
  29. sortableId: "projects",
  30. dragHandleLabel: "Move Projects section",
  31. },
  32. createElement("div", { "data-testid": "section-content" }, "Details"),
  33. ),
  34. );
  35. }
  36. describe("WorkspacePurposeSection", () => {
  37. it("renders the expanded state with content and a down chevron", () => {
  38. const html = renderSection(true);
  39. expect(html).toContain('aria-expanded="true"');
  40. expect(html).toContain('data-purpose-section-chevron="expanded"');
  41. expect(html).toContain('data-testid="section-content"');
  42. });
  43. it("renders the collapsed state with a right chevron and hidden content", () => {
  44. const html = renderSection(false);
  45. expect(html).toContain('aria-expanded="false"');
  46. expect(html).toContain('data-purpose-section-chevron="collapsed"');
  47. expect(html).not.toContain('data-testid="section-content"');
  48. });
  49. it("keeps the action outside the collapse trigger and exposes accessible visibility fallbacks", () => {
  50. const html = renderSection(true);
  51. const triggerEndIndex = html.indexOf("</button>");
  52. const dragHandleIndex = html.indexOf('data-purpose-section-drag-handle="projects"');
  53. const actionIndex = html.indexOf('data-testid="section-action"');
  54. expect(triggerEndIndex).toBeGreaterThan(-1);
  55. expect(dragHandleIndex).toBeGreaterThan(triggerEndIndex);
  56. expect(actionIndex).toBeGreaterThan(triggerEndIndex);
  57. expect(html).toContain('aria-label="Move Projects section"');
  58. expect(html).toContain("touch-none");
  59. expect(html).toContain("group-hover/purpose-section:opacity-100");
  60. expect(html).toContain("group-focus-within/purpose-section:opacity-100");
  61. expect(html).toContain("has-[[data-state=open]]:opacity-100");
  62. expect(html).toContain("[@media(hover:none)]:opacity-100");
  63. });
  64. it("uses the same typography as the pinned section title", () => {
  65. const html = renderSection(true);
  66. expect(html).toContain("text-ui-base font-medium text-foreground-subtlest");
  67. expect(html).not.toContain("text-ui-xs");
  68. });
  69. });