|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { applyCaptionOverrides } from "./captionOverrides"; |
| 4 | + |
| 5 | +function installCaptionOverrideFetch(overrides: unknown[]) { |
| 6 | + vi.stubGlobal("fetch", async () => ({ |
| 7 | + ok: true, |
| 8 | + async json() { |
| 9 | + return overrides; |
| 10 | + }, |
| 11 | + })); |
| 12 | +} |
| 13 | + |
| 14 | +function installGsapMock() { |
| 15 | + const setCalls: Array<{ target: Element; vars: Record<string, unknown> }> = []; |
| 16 | + const gsap = { |
| 17 | + set(target: Element, vars: Record<string, unknown>) { |
| 18 | + setCalls.push({ target, vars }); |
| 19 | + for (const [key, value] of Object.entries(vars)) { |
| 20 | + if (key === "fontSize" && typeof value === "string" && target instanceof HTMLElement) { |
| 21 | + target.style.fontSize = value; |
| 22 | + } |
| 23 | + } |
| 24 | + }, |
| 25 | + killTweensOf() {}, |
| 26 | + getTweensOf() { |
| 27 | + return []; |
| 28 | + }, |
| 29 | + }; |
| 30 | + Object.defineProperty(window, "gsap", { |
| 31 | + configurable: true, |
| 32 | + value: gsap, |
| 33 | + }); |
| 34 | + return { setCalls }; |
| 35 | +} |
| 36 | + |
| 37 | +async function flushCaptionOverrides() { |
| 38 | + for (let i = 0; i < 4; i++) { |
| 39 | + await Promise.resolve(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + vi.unstubAllGlobals(); |
| 45 | + document.body.innerHTML = ""; |
| 46 | + Reflect.deleteProperty(window, "gsap"); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("applyCaptionOverrides", () => { |
| 50 | + it("reuses existing caption wrappers when overrides are applied more than once", async () => { |
| 51 | + const { setCalls } = installGsapMock(); |
| 52 | + installCaptionOverrideFetch([{ wordIndex: 0, x: 12, y: -4, scale: 1.2 }]); |
| 53 | + document.body.innerHTML = ` |
| 54 | + <div class="caption-group"> |
| 55 | + <span id="w0">Hello</span> |
| 56 | + </div> |
| 57 | + `; |
| 58 | + |
| 59 | + applyCaptionOverrides(); |
| 60 | + await flushCaptionOverrides(); |
| 61 | + applyCaptionOverrides(); |
| 62 | + await flushCaptionOverrides(); |
| 63 | + |
| 64 | + const group = document.querySelector(".caption-group"); |
| 65 | + const word = document.getElementById("w0"); |
| 66 | + const wrappers = group?.querySelectorAll('[data-caption-wrapper="true"]'); |
| 67 | + const wrapper = wrappers?.item(0); |
| 68 | + if (!group || !word || !wrapper) throw new Error("Expected wrapped caption word"); |
| 69 | + |
| 70 | + expect(wrappers).toHaveLength(1); |
| 71 | + expect(wrapper.parentElement).toBe(group); |
| 72 | + expect(word.parentElement).toBe(wrapper); |
| 73 | + expect(setCalls.map((call) => call.target)).toEqual([wrapper, wrapper]); |
| 74 | + }); |
| 75 | + |
| 76 | + it("treats a pre-wrapped word as the wordIndex target, not as another word", async () => { |
| 77 | + installGsapMock(); |
| 78 | + installCaptionOverrideFetch([{ wordIndex: 0, fontSize: 72 }]); |
| 79 | + document.body.innerHTML = ` |
| 80 | + <div class="caption-group"> |
| 81 | + <span data-caption-wrapper="true"> |
| 82 | + <span id="w0">Hello</span> |
| 83 | + </span> |
| 84 | + </div> |
| 85 | + `; |
| 86 | + |
| 87 | + applyCaptionOverrides(); |
| 88 | + await flushCaptionOverrides(); |
| 89 | + |
| 90 | + const word = document.getElementById("w0"); |
| 91 | + const wrapper = word?.parentElement; |
| 92 | + if (!(word instanceof HTMLElement) || !wrapper) { |
| 93 | + throw new Error("Expected pre-wrapped caption word"); |
| 94 | + } |
| 95 | + |
| 96 | + expect(word.style.fontSize).toBe("72px"); |
| 97 | + expect(wrapper.getAttribute("style") ?? "").not.toContain("font-size"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("resolves wordId overrides against the inner word of an existing wrapper", async () => { |
| 101 | + const { setCalls } = installGsapMock(); |
| 102 | + installCaptionOverrideFetch([{ wordId: "w0", x: 16 }]); |
| 103 | + document.body.innerHTML = ` |
| 104 | + <div class="caption-group"> |
| 105 | + <span data-caption-wrapper="true"> |
| 106 | + <span id="w0">Hello</span> |
| 107 | + </span> |
| 108 | + </div> |
| 109 | + `; |
| 110 | + |
| 111 | + applyCaptionOverrides(); |
| 112 | + await flushCaptionOverrides(); |
| 113 | + |
| 114 | + const word = document.getElementById("w0"); |
| 115 | + const wrapper = word?.parentElement; |
| 116 | + if (!(word instanceof HTMLElement) || !wrapper) { |
| 117 | + throw new Error("Expected pre-wrapped caption word"); |
| 118 | + } |
| 119 | + |
| 120 | + expect(setCalls).toHaveLength(1); |
| 121 | + expect(setCalls[0]?.target).toBe(wrapper); |
| 122 | + expect(setCalls[0]?.vars).toEqual({ x: 16 }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments