|
| 1 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | + |
| 3 | +import { __resetScriptCache, loadScript, type LoadScriptOptions } from "../loadScript"; |
| 4 | + |
| 5 | +const SCRIPT_SRC = "https://example.com/script.js"; |
| 6 | + |
| 7 | +describe("loadScript", () => { |
| 8 | + beforeEach(() => { |
| 9 | + document.querySelectorAll("script").forEach((s) => s.remove()); |
| 10 | + __resetScriptCache(); |
| 11 | + }); |
| 12 | + |
| 13 | + test("rejects if src is not provided", async () => { |
| 14 | + await expect(loadScript("")).rejects.toThrow('No "src" provided for createScript'); |
| 15 | + }); |
| 16 | + |
| 17 | + test("resolves immediately if script already exists in DOM", async () => { |
| 18 | + const script = document.createElement("script"); |
| 19 | + script.src = SCRIPT_SRC; |
| 20 | + document.head.appendChild(script); |
| 21 | + |
| 22 | + const resolved = await loadScript(SCRIPT_SRC); |
| 23 | + expect(resolved).toBe(script); |
| 24 | + }); |
| 25 | + |
| 26 | + test("injects script only once per src when innerHTML is not set", async () => { |
| 27 | + const promise1 = loadScript(SCRIPT_SRC, { async: true }); |
| 28 | + const promise2 = loadScript(SCRIPT_SRC, { async: true }); |
| 29 | + |
| 30 | + const scripts = document.querySelectorAll(`script[src='${SCRIPT_SRC}']`); |
| 31 | + expect(scripts.length).toBe(1); |
| 32 | + |
| 33 | + scripts[0]?.dispatchEvent(new Event("load")); |
| 34 | + |
| 35 | + const s1 = await promise1; |
| 36 | + const s2 = await promise2; |
| 37 | + |
| 38 | + expect(s1).toBe(s2); |
| 39 | + }); |
| 40 | + |
| 41 | + test("injects script multiple times when different innerHTML is set", async () => { |
| 42 | + const promise1 = loadScript(SCRIPT_SRC, { |
| 43 | + innerHTML: `{ "symbol": "AAPL" }`, |
| 44 | + }); |
| 45 | + const promise2 = loadScript(SCRIPT_SRC, { |
| 46 | + innerHTML: `{ "symbol": "TSLA" }`, |
| 47 | + }); |
| 48 | + |
| 49 | + const scripts = document.querySelectorAll(`script[src='${SCRIPT_SRC}']`); |
| 50 | + expect(scripts.length).toBe(2); |
| 51 | + |
| 52 | + scripts[0]?.dispatchEvent(new Event("load")); |
| 53 | + scripts[1]?.dispatchEvent(new Event("load")); |
| 54 | + |
| 55 | + const s1 = await promise1; |
| 56 | + const s2 = await promise2; |
| 57 | + |
| 58 | + expect(s1).not.toBe(s2); |
| 59 | + }); |
| 60 | + |
| 61 | + test("evicts stale cache when script was removed from DOM", async () => { |
| 62 | + const promise1 = loadScript(SCRIPT_SRC); |
| 63 | + const script1 = document.querySelector(`script[src="${SCRIPT_SRC}"]`)!; |
| 64 | + script1.dispatchEvent(new Event("load")); |
| 65 | + |
| 66 | + const resolved1 = await promise1; |
| 67 | + expect(resolved1).toBe(script1); |
| 68 | + |
| 69 | + // Simulate user removing the script from the DOM |
| 70 | + script1.remove(); |
| 71 | + |
| 72 | + // Second load: should not return the same script, should re-inject |
| 73 | + const promise2 = loadScript(SCRIPT_SRC); |
| 74 | + const script2 = document.querySelector(`script[src="${SCRIPT_SRC}"]`)!; |
| 75 | + |
| 76 | + // Manually resolve the second injected script |
| 77 | + script2.dispatchEvent(new Event("load")); |
| 78 | + const resolved2 = await promise2; |
| 79 | + |
| 80 | + expect(resolved2).not.toBe(resolved1); |
| 81 | + }); |
| 82 | + |
| 83 | + test("assigns all direct script properties correctly", async () => { |
| 84 | + const props = { |
| 85 | + async: false, |
| 86 | + defer: true, |
| 87 | + fetchPriority: "low", |
| 88 | + noModule: true, |
| 89 | + id: "test-script", |
| 90 | + type: "text/javascript", |
| 91 | + crossOrigin: "anonymous", |
| 92 | + referrerPolicy: "origin", |
| 93 | + integrity: "sha384-abc123", |
| 94 | + nonce: "xyz123", |
| 95 | + textContent: "Hello, world!", |
| 96 | + } as LoadScriptOptions; |
| 97 | + |
| 98 | + const promise = loadScript(SCRIPT_SRC, props); |
| 99 | + |
| 100 | + const [script] = document.querySelectorAll(`script[src="${SCRIPT_SRC}"]`); |
| 101 | + expect(script).toBeDefined(); |
| 102 | + |
| 103 | + script?.dispatchEvent(new Event("load")); |
| 104 | + const resolved = await promise; |
| 105 | + |
| 106 | + expect(resolved.async).toBe(false); |
| 107 | + expect(resolved.id).toBe("test-script"); |
| 108 | + expect(resolved.defer).toBe(true); |
| 109 | + expect(resolved.fetchPriority).toBe("low"); |
| 110 | + expect(resolved.noModule).toBe(true); |
| 111 | + expect(resolved.type).toBe("text/javascript"); |
| 112 | + expect(resolved.crossOrigin).toBe("anonymous"); |
| 113 | + expect(resolved.referrerPolicy).toBe("origin"); |
| 114 | + expect(resolved.integrity).toBe("sha384-abc123"); |
| 115 | + expect(resolved.nonce).toBe("xyz123"); |
| 116 | + expect(resolved.textContent).toBe("Hello, world!"); |
| 117 | + }); |
| 118 | + |
| 119 | + test("applies additional script attributes via setAttribute", async () => { |
| 120 | + const promise = loadScript(SCRIPT_SRC, { "data-id": "custom-script-id" }); |
| 121 | + |
| 122 | + const [script] = document.querySelectorAll(`script[src="${SCRIPT_SRC}"]`); |
| 123 | + expect(script).toBeDefined(); |
| 124 | + |
| 125 | + script?.dispatchEvent(new Event("load")); |
| 126 | + const resolved = await promise; |
| 127 | + |
| 128 | + expect(resolved.getAttribute("data-id")).toBe("custom-script-id"); |
| 129 | + }); |
| 130 | + |
| 131 | + test("resolves when script loads", async () => { |
| 132 | + const promise = loadScript(SCRIPT_SRC); |
| 133 | + const script = document.querySelector(`script[src="${SCRIPT_SRC}"]`)!; |
| 134 | + |
| 135 | + script.dispatchEvent(new Event("load")); |
| 136 | + |
| 137 | + const resolved = await promise; |
| 138 | + expect(resolved).toBe(script); |
| 139 | + }); |
| 140 | + |
| 141 | + test("rejects when script fails to load", async () => { |
| 142 | + const promise = loadScript(SCRIPT_SRC); |
| 143 | + const script = document.querySelector(`script[src="${SCRIPT_SRC}"]`)!; |
| 144 | + |
| 145 | + const errorEvent = new Event("error"); |
| 146 | + |
| 147 | + script.dispatchEvent(errorEvent); |
| 148 | + |
| 149 | + await expect(promise).rejects.toBe(errorEvent); |
| 150 | + }); |
| 151 | + |
| 152 | + test("calls onLoad handler when provided after script loads", async () => { |
| 153 | + const onLoad = vi.fn(); |
| 154 | + |
| 155 | + const promise = loadScript(SCRIPT_SRC, { onLoad }); |
| 156 | + const script = document.querySelector(`script[src="${SCRIPT_SRC}"]`)!; |
| 157 | + |
| 158 | + script.dispatchEvent(new Event("load")); |
| 159 | + |
| 160 | + const resolved = await promise; |
| 161 | + |
| 162 | + expect(onLoad).toHaveBeenCalledOnce(); |
| 163 | + expect(resolved).toBe(script); |
| 164 | + }); |
| 165 | + |
| 166 | + test("calls onError handler when script fails to load", async () => { |
| 167 | + const onError = vi.fn(); |
| 168 | + |
| 169 | + const promise = loadScript(SCRIPT_SRC, { onError }); |
| 170 | + |
| 171 | + const script = document.querySelector(`script[src="${SCRIPT_SRC}"]`)!; |
| 172 | + |
| 173 | + const errorEvent = new Event("error"); |
| 174 | + |
| 175 | + script.dispatchEvent(errorEvent); |
| 176 | + |
| 177 | + await expect(promise).rejects.toBe(errorEvent); |
| 178 | + |
| 179 | + expect(onError).toHaveBeenCalledOnce(); |
| 180 | + }); |
| 181 | +}); |
0 commit comments