cloudDialogMediaHero.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // @vitest-environment jsdom
  2. import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { CloudDialogHero } from "@/components/cloud-content-dialog/CloudDialogHero.js";
  6. import { validateCloudLottieData } from "@/components/cloud-content-dialog/cloudLottieRuntime.js";
  7. const player = vi.hoisted(() => ({
  8. play: vi.fn(),
  9. pause: vi.fn(),
  10. destroy: vi.fn(),
  11. setSpeed: vi.fn(),
  12. goToAndStop: vi.fn(),
  13. addEventListener: vi.fn(),
  14. }));
  15. const loadAnimation = vi.hoisted(() => vi.fn(() => player));
  16. vi.mock("lottie-web/build/player/lottie_light_canvas.js", () => ({ default: { loadAnimation } }));
  17. afterEach(() => {
  18. cleanup();
  19. vi.unstubAllGlobals();
  20. document.documentElement.classList.remove("dark");
  21. vi.clearAllMocks();
  22. });
  23. const base = { locale: "en-US", title: "Hero" };
  24. describe("media Hero", () => {
  25. it("renders image, switches theme source and exposes load failure", async () => {
  26. render(
  27. createElement(CloudDialogHero, {
  28. ...base,
  29. hero: {
  30. type: "image",
  31. src: "https://example.com/light.png",
  32. darkSrc: "https://example.com/dark.png",
  33. alt: "Preview",
  34. fit: "contain",
  35. },
  36. }),
  37. );
  38. expect(screen.getByAltText("Preview").getAttribute("src")).toContain("light.png");
  39. act(() => {
  40. document.documentElement.classList.add("dark");
  41. });
  42. await waitFor(() =>
  43. expect(screen.getByAltText("Preview").getAttribute("src")).toContain("dark.png"),
  44. );
  45. fireEvent.error(screen.getByAltText("Preview"));
  46. expect(screen.getByTestId("cloud-dialog-image-hero").getAttribute("data-status")).toBe("error");
  47. });
  48. it("renders muted inline video and pauses on unmount", () => {
  49. const pause = vi.spyOn(HTMLMediaElement.prototype, "pause").mockImplementation(() => {});
  50. const { unmount } = render(
  51. createElement(CloudDialogHero, {
  52. ...base,
  53. hero: {
  54. type: "video",
  55. src: "https://example.com/demo.mp4",
  56. poster: "https://example.com/poster.png",
  57. muted: true,
  58. loop: true,
  59. },
  60. }),
  61. );
  62. const video = screen.getByTestId("cloud-dialog-video-hero") as HTMLVideoElement;
  63. expect(video.muted).toBe(true);
  64. expect(video.playsInline).toBe(true);
  65. expect(video.loop).toBe(true);
  66. unmount();
  67. expect(pause).toHaveBeenCalled();
  68. pause.mockRestore();
  69. });
  70. it("loads validated Lottie data into light canvas and destroys the player", async () => {
  71. vi.stubGlobal(
  72. "fetch",
  73. vi.fn(
  74. async () =>
  75. new Response(
  76. JSON.stringify({
  77. v: "5.13.0",
  78. w: 400,
  79. h: 300,
  80. fr: 30,
  81. ip: 0,
  82. op: 60,
  83. layers: [],
  84. assets: [],
  85. }),
  86. ),
  87. ),
  88. );
  89. const { unmount } = render(
  90. createElement(CloudDialogHero, {
  91. ...base,
  92. hero: { type: "lottie", src: "https://example.com/hero.json", autoplay: true, speed: 1.5 },
  93. }),
  94. );
  95. await waitFor(() => expect(loadAnimation).toHaveBeenCalled());
  96. expect(loadAnimation.mock.calls[0]?.[0]).toMatchObject({
  97. renderer: "canvas",
  98. autoplay: false,
  99. animationData: { w: 400 },
  100. });
  101. expect(player.setSpeed).toHaveBeenCalledWith(1.5);
  102. unmount();
  103. expect(player.destroy).toHaveBeenCalled();
  104. });
  105. it("rejects external assets, expressions and unreasonable dimensions", () => {
  106. const data = { w: 400, h: 300, fr: 30, ip: 0, op: 60, layers: [] };
  107. expect(() => validateCloudLottieData(data)).not.toThrow();
  108. expect(() =>
  109. validateCloudLottieData({ ...data, assets: [{ p: "https://evil.example/a.png" }] }),
  110. ).toThrow();
  111. expect(() => validateCloudLottieData({ ...data, layers: [{ x: "alert(1)" }] })).toThrow();
  112. expect(() => validateCloudLottieData({ ...data, w: 99999 })).toThrow();
  113. });
  114. });