|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { extractMediaSrcMutations } from "./mediaSrcMutation.js"; |
| 3 | + |
| 4 | +describe("extractMediaSrcMutations", () => { |
| 5 | + it.each([ |
| 6 | + { |
| 7 | + label: "block", |
| 8 | + script: `const media = document.getElementById("video"); { const media = document.getElementById("image"); media.src = "image.png"; } media.src = "new.mp4";`, |
| 9 | + }, |
| 10 | + { |
| 11 | + label: "sibling functions", |
| 12 | + script: `function updateImage() { const media = document.getElementById("image"); media.src = "image.png"; } function updateVideo() { const media = document.getElementById("video"); media.src = "new.mp4"; }`, |
| 13 | + }, |
| 14 | + { |
| 15 | + label: "arrow function", |
| 16 | + script: `const media = document.getElementById("video"); const updateImage = () => { const media = document.getElementById("image"); media.src = "image.png"; }; media.src = "new.mp4";`, |
| 17 | + }, |
| 18 | + ])("keeps $label bindings separate", ({ script }) => { |
| 19 | + expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([ |
| 20 | + "#image", |
| 21 | + "#video", |
| 22 | + ]); |
| 23 | + }); |
| 24 | + |
| 25 | + it.each([ |
| 26 | + { |
| 27 | + label: "parameter", |
| 28 | + script: `const media = document.getElementById("video"); function update(media) { media.src = "image.png"; } media.src = "new.mp4";`, |
| 29 | + }, |
| 30 | + { |
| 31 | + label: "destructured parameter", |
| 32 | + script: `const media = document.getElementById("video"); function update({ media }) { media.src = "image.png"; } media.src = "new.mp4";`, |
| 33 | + }, |
| 34 | + { |
| 35 | + label: "unknown local", |
| 36 | + script: `const media = document.getElementById("video"); { const media = findImage(); media.src = "image.png"; } media.src = "new.mp4";`, |
| 37 | + }, |
| 38 | + { |
| 39 | + label: "destructured local", |
| 40 | + script: `const media = document.getElementById("video"); { const { media } = assets; media.src = "image.png"; } media.src = "new.mp4";`, |
| 41 | + }, |
| 42 | + { |
| 43 | + label: "catch parameter", |
| 44 | + script: `const media = document.getElementById("video"); try { update(); } catch(media) { media.src = "image.png"; } media.src = "new.mp4";`, |
| 45 | + }, |
| 46 | + { |
| 47 | + label: "loop variable", |
| 48 | + script: `const media = document.getElementById("video"); for (const media of images) { media.src = "image.png"; } media.src = "new.mp4";`, |
| 49 | + }, |
| 50 | + ])("does not inherit an outer target through a $label", ({ script }) => { |
| 51 | + expect(extractMediaSrcMutations(script)).toEqual([ |
| 52 | + { selector: "#video", operation: "src_assignment", raw: `media.src = "new.mp4"` }, |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("resolves a closure and its scoped aliases", () => { |
| 57 | + const script = `const media = document.getElementById("video"); { const alias = media; function update() { alias.setAttribute("src", "new.mp4"); } }`; |
| 58 | + expect(extractMediaSrcMutations(script)).toEqual([ |
| 59 | + { |
| 60 | + selector: "#video", |
| 61 | + operation: "set_attribute", |
| 62 | + raw: `alias.setAttribute("src", "new.mp4")`, |
| 63 | + }, |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it("keeps var declarations in their enclosing function", () => { |
| 68 | + const script = `function update() { { var media = document.getElementById("video"); } media.src = "new.mp4"; }`; |
| 69 | + expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([ |
| 70 | + "#video", |
| 71 | + ]); |
| 72 | + }); |
| 73 | + |
| 74 | + it("retains a parameter that is redeclared and initialized with var", () => { |
| 75 | + const script = `function update(media) { var media = document.getElementById("video"); media.src = "new.mp4"; }`; |
| 76 | + expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([ |
| 77 | + "#video", |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("does not inherit an outer target through a named class expression", () => { |
| 82 | + const script = `const media = document.getElementById("video"); const View = class media { update() { media.src = "not-a-video"; } }; media.src = "new.mp4";`; |
| 83 | + expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([ |
| 84 | + "#video", |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("does not leak block declarations outside their scope", () => { |
| 89 | + const script = `{ const media = document.getElementById("video"); } media.src = "new.mp4";`; |
| 90 | + expect(extractMediaSrcMutations(script)).toEqual([]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("retains direct and computed DOM lookups", () => { |
| 94 | + const script = `document["getElementById"]("video")["src"] = "new.mp4"; document.querySelector("#audio").setAttribute("SRC", "new.wav");`; |
| 95 | + expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([ |
| 96 | + "#video", |
| 97 | + "#audio", |
| 98 | + ]); |
| 99 | + }); |
| 100 | + |
| 101 | + it("keeps module parsing and rejects malformed scripts", () => { |
| 102 | + expect( |
| 103 | + extractMediaSrcMutations( |
| 104 | + `export const media = document.getElementById("video"); media.src = "new.mp4";`, |
| 105 | + ), |
| 106 | + ).toHaveLength(1); |
| 107 | + expect(extractMediaSrcMutations("const media = ;")).toEqual([]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("does not recurse through circular aliases", () => { |
| 111 | + expect(extractMediaSrcMutations(`let a = b; let b = a; a.src = "new.mp4";`)).toEqual([]); |
| 112 | + }); |
| 113 | +}); |
0 commit comments