weekendHeroResource.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 the supplied gift 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("viewBox")).toBe("0 0 24 24");
  38. expect(icons[0]!.getAttribute("stroke")).toBe("currentColor");
  39. expect(icons[0]!.getAttribute("aria-hidden")).toBe("true");
  40. expect(
  41. Array.from(icons[0]!.querySelectorAll("path"), (path) => path.getAttribute("d")),
  42. ).toEqual([
  43. "M12 7v14",
  44. "M20 11v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2v-8",
  45. "M7.5 7a1 1 0 0 1 0-5A4.8 8 0 0 1 12 7a4.8 8 0 0 1 4.5-5 1 1 0 0 1 0 5",
  46. ]);
  47. expect(icons[0]!.querySelector("rect")!.getAttribute("width")).toBe("18");
  48. expect(f.window.document.querySelector("#benefits b")).toBeNull();
  49. expect(f.window.document.getElementById("benefits")!.textContent).not.toContain("◇");
  50. } finally {
  51. f.dom.window.close();
  52. }
  53. });
  54. it("ends entry state and allows replay without leaving the native back hidden", () => {
  55. const f = fixture();
  56. try {
  57. f.message("init", { data: {}, reducedMotion: false });
  58. const surface = f.window.document.getElementById("ticket-flip")!;
  59. expect(surface.classList.contains("is-entering")).toBe(true);
  60. surface.dispatchEvent(
  61. Object.assign(new f.window.Event("animationend"), { animationName: "ticket-enter" }),
  62. );
  63. expect(surface.classList.contains("is-entering")).toBe(false);
  64. f.window.document.getElementById("replay")!.click();
  65. expect(surface.classList.contains("is-entering")).toBe(true);
  66. } finally {
  67. f.dom.window.close();
  68. }
  69. });
  70. it("does not schedule idle motion before init or under reduced motion", () => {
  71. const f = fixture();
  72. try {
  73. expect(f.raf).not.toHaveBeenCalled();
  74. f.message("init", { reducedMotion: true });
  75. expect(f.raf).not.toHaveBeenCalled();
  76. expect(
  77. f.window.document.getElementById("ticket-flip")!.classList.contains("is-entering"),
  78. ).toBe(false);
  79. } finally {
  80. f.dom.window.close();
  81. }
  82. });
  83. it("stops idle motion while hidden and on destroy, then resumes on visibility", () => {
  84. const f = fixture();
  85. try {
  86. f.message("init");
  87. f.raf.mockClear();
  88. f.message("visibility", { visible: false });
  89. expect(f.cancel).toHaveBeenCalled();
  90. expect(f.raf).not.toHaveBeenCalled();
  91. f.message("visibility", { visible: true });
  92. expect(f.raf).toHaveBeenCalledTimes(1);
  93. f.message("destroy");
  94. f.raf.mockClear();
  95. f.message("visibility", { visible: true });
  96. expect(f.raf).not.toHaveBeenCalled();
  97. } finally {
  98. f.dom.window.close();
  99. }
  100. });
  101. it("ignores initialization from a source other than parent", () => {
  102. const f = fixture();
  103. try {
  104. f.window.dispatchEvent(
  105. new f.window.MessageEvent("message", {
  106. source: null,
  107. data: {
  108. channel: "zcode-cloud-hero-v1",
  109. instanceId: "fake",
  110. type: "init",
  111. data: { planName: "forged" },
  112. },
  113. }),
  114. );
  115. expect(f.window.document.getElementById("plan-name")!.textContent).toBe("");
  116. } finally {
  117. f.dom.window.close();
  118. }
  119. });
  120. });