weekendHeroResource.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { readFile } from "node:fs/promises";
  2. import { JSDOM } from "jsdom";
  3. import { describe, expect, it, vi } from "vitest";
  4. const html = await readFile(
  5. new URL("../src/assets/cloud-content/weekend-plan-hero.html", import.meta.url),
  6. "utf8",
  7. );
  8. function fixture() {
  9. const raf = vi.fn(() => 1);
  10. const cancel = vi.fn();
  11. const dom = new JSDOM(html, {
  12. runScripts: "dangerously",
  13. pretendToBeVisual: true,
  14. beforeParse(window) {
  15. window.requestAnimationFrame = raf;
  16. window.cancelAnimationFrame = cancel;
  17. Object.defineProperty(window.HTMLElement.prototype, "getAnimations", { value: () => [] });
  18. },
  19. });
  20. const { window } = dom;
  21. const message = (type: string, extra = {}) =>
  22. window.dispatchEvent(
  23. new window.MessageEvent("message", {
  24. source: window,
  25. data: { channel: "zcode-cloud-hero-v1", instanceId: "hero", type, ...extra },
  26. }),
  27. );
  28. return { dom, window, message, raf, cancel };
  29. }
  30. describe("Weekend Hero resource lifecycle", () => {
  31. it("renders a decorative SVG for each benefit without interpreting benefit text as HTML", () => {
  32. const f = fixture();
  33. try {
  34. f.message("init", { data: { benefits: ["GLM tokens", "<b>benefit</b>"] } });
  35. const icons = f.window.document.querySelectorAll("#benefits svg.lucide-gift");
  36. expect(icons).toHaveLength(2);
  37. expect(icons[0]!.getAttribute("aria-hidden")).toBe("true");
  38. expect(f.window.document.querySelector("#benefits b")).toBeNull();
  39. } finally {
  40. f.dom.window.close();
  41. }
  42. });
  43. it("ends entry state and allows replay without leaving the native back hidden", () => {
  44. const f = fixture();
  45. try {
  46. f.message("init", { data: {}, reducedMotion: false });
  47. const surface = f.window.document.getElementById("ticket-flip")!;
  48. expect(surface.classList.contains("is-entering")).toBe(true);
  49. surface.dispatchEvent(
  50. Object.assign(new f.window.Event("animationend"), { animationName: "ticket-enter" }),
  51. );
  52. expect(surface.classList.contains("is-entering")).toBe(false);
  53. f.window.document.getElementById("replay")!.click();
  54. expect(surface.classList.contains("is-entering")).toBe(true);
  55. } finally {
  56. f.dom.window.close();
  57. }
  58. });
  59. it("does not schedule idle motion before init or under reduced motion", () => {
  60. const f = fixture();
  61. try {
  62. expect(f.raf).not.toHaveBeenCalled();
  63. f.message("init", { reducedMotion: true });
  64. expect(f.raf).not.toHaveBeenCalled();
  65. expect(
  66. f.window.document.getElementById("ticket-flip")!.classList.contains("is-entering"),
  67. ).toBe(false);
  68. } finally {
  69. f.dom.window.close();
  70. }
  71. });
  72. it("stops idle motion while hidden and on destroy, then resumes on visibility", () => {
  73. const f = fixture();
  74. try {
  75. f.message("init");
  76. f.raf.mockClear();
  77. f.message("visibility", { visible: false });
  78. expect(f.cancel).toHaveBeenCalled();
  79. expect(f.raf).not.toHaveBeenCalled();
  80. f.message("visibility", { visible: true });
  81. expect(f.raf).toHaveBeenCalledTimes(1);
  82. f.message("destroy");
  83. f.raf.mockClear();
  84. f.message("visibility", { visible: true });
  85. expect(f.raf).not.toHaveBeenCalled();
  86. } finally {
  87. f.dom.window.close();
  88. }
  89. });
  90. it("ignores initialization from a source other than parent", () => {
  91. const f = fixture();
  92. try {
  93. f.window.dispatchEvent(
  94. new f.window.MessageEvent("message", {
  95. source: null,
  96. data: {
  97. channel: "zcode-cloud-hero-v1",
  98. instanceId: "fake",
  99. type: "init",
  100. data: { planName: "forged" },
  101. },
  102. }),
  103. );
  104. expect(f.window.document.getElementById("plan-name")!.textContent).toBe("");
  105. } finally {
  106. f.dom.window.close();
  107. }
  108. });
  109. });