// @vitest-environment node import { describe, expect, it } from "vitest"; import { htmlHasGsapCore, insertGsapCoreBeforeHeadPlugin } from "./gsapCoreScript"; const script = (src: string) => `
`; const PLUGIN = "https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/MotionPathPlugin.min.js"; const CORE = ``; describe("htmlHasGsapCore", () => { it.each([ "https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/gsap.min.js", "https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.5/gsap.js", "/api/vendor/npm/gsap@3.15.0/dist/gsap.min.js", "./vendor/gsap.min.js", "assets/gsap-3.14.2.min.js", "gsap.min.js?v=2", ])("finds GSAP core at %s", (src) => { expect(htmlHasGsapCore(script(src))).toBe(true); }); it.each([ "https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/MotionPathPlugin.min.js", "https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/CustomEase.min.js", "/api/vendor/npm/gsap@3.15.0/dist/MotionPathPlugin.min.js", "./vendor/gsap/ScrollTrigger.js", ])("does not treat the plugin %s as GSAP core", (src) => { expect(htmlHasGsapCore(script(src))).toBe(false); }); it("reads core from a producer inlining marker, but not a plugin's", () => { const inlined = (src: string) => ``; expect(htmlHasGsapCore(inlined("https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"))).toBe( true, ); expect( htmlHasGsapCore(inlined("https://cdn.jsdelivr.net/npm/gsap@3/dist/MotionPathPlugin.min.js")), ).toBe(false); }); it("reads core from its license banner, but not a plugin's", () => { const banner = (name: string) => ``; expect(htmlHasGsapCore(banner("GSAP"))).toBe(true); expect(htmlHasGsapCore(banner("MotionPathPlugin"))).toBe(false); }); it.each([ "gsap.registerPlugin(MotionPathPlugin);", 'gsap.defaults({ ease: "none" }); gsap.config({ nullTargetWarn: false });', 'gsap.to("#a", { x: 1 }); /* GreenSock */', ])("does not treat the authored usage %s as GSAP core", (usage) => { const html = ``; expect(htmlHasGsapCore(html)).toBe(false); }); it("reads core from its library body", () => { const body = `function S(t,e){console.warn("Invalid property",t,"set to",e,"Missing plugin? gsap.registerPlugin()")}`; expect(htmlHasGsapCore(``)).toBe(true); }); it("ignores GSAP core inside a ", () => { const src = "https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"; expect(htmlHasGsapCore(``)).toBe(false); }); }); describe("insertGsapCoreBeforeHeadPlugin", () => { it.each([ ["a plugin src", ``], ["a local plugin", ``], ["an inlined plugin", ``], [ "a plugin license banner", ``, ], ])("puts core before %s in the head", (_name, plugin) => { const html = `${plugin}`; const out = insertGsapCoreBeforeHeadPlugin(html, CORE) ?? ""; expect(out.indexOf(CORE)).toBeGreaterThan(out.indexOf("var a;")); expect(out.indexOf(CORE)).toBeLessThan(out.indexOf(plugin)); }); it("works on a bare head fragment", () => { const out = insertGsapCoreBeforeHeadPlugin(``, CORE) ?? ""; expect(out.startsWith(CORE)).toBe(true); }); it("leaves the head alone when the only plugin is in the body or a template", () => { const tag = ``; expect( insertGsapCoreBeforeHeadPlugin(`${tag}`, CORE), ).toBeNull(); expect( insertGsapCoreBeforeHeadPlugin(`${tag}`, CORE), ).toBeNull(); }); });