collapsible.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. Collapsible,
  6. CollapsibleContent,
  7. CollapsibleTrigger,
  8. } from "../src/components/ui/collapsible.js";
  9. describe("Collapsible", () => {
  10. it("keeps the closed animation end frame while content is still mounted", () => {
  11. const html = renderToStaticMarkup(
  12. createElement(
  13. Collapsible,
  14. { defaultOpen: false },
  15. createElement(CollapsibleTrigger, null, "Toggle"),
  16. createElement(CollapsibleContent, { forceMount: true }, "Details"),
  17. ),
  18. );
  19. expect(html).toContain("data-[state=closed]:[animation-fill-mode:forwards]");
  20. expect(html).toContain(
  21. "group-data-[state=closed]/collapsible-content:[animation-fill-mode:forwards]",
  22. );
  23. expect(html).toContain('data-slot="collapsible-content"');
  24. expect(html).toContain('data-state="closed"');
  25. const contentTag = html.match(
  26. /<div[^>]*data-slot="collapsible-content"[^>]*>/,
  27. )?.[0];
  28. expect(contentTag).toBeDefined();
  29. expect(contentTag).not.toMatch(/\shidden(?:=|\s|>)/u);
  30. });
  31. it("does not expose collapsible animation layers to default CSS transitions", () => {
  32. const html = renderToStaticMarkup(
  33. createElement(
  34. Collapsible,
  35. { defaultOpen: true },
  36. createElement(CollapsibleTrigger, null, "Toggle"),
  37. createElement(CollapsibleContent, null, "Details"),
  38. ),
  39. );
  40. expect(html).toContain("transition-none");
  41. expect(html).toContain(
  42. "transition-none duration-300 ease-in-out group-data-[state=open]/collapsible-content:animate-in",
  43. );
  44. });
  45. });