|
| 1 | +// Import Node.js Dependencies |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import assert from "node:assert/strict"; |
| 4 | + |
| 5 | +// Import Internal Dependencies |
| 6 | +import { makePrefixRemover } from "../../src/utils/index.js"; |
| 7 | + |
| 8 | +describe("makePrefixRemover", () => { |
| 9 | + it("returns the original string when no dot is present", () => { |
| 10 | + const strip = makePrefixRemover(["window"]); |
| 11 | + assert.strictEqual(strip("foo"), "foo"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns the original string when the identifier is not at the start of the string", () => { |
| 15 | + const strip = makePrefixRemover(["window"]); |
| 16 | + assert.strictEqual(strip("foo.window"), "foo.window"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("removes a matching prefix at the start of the expression", () => { |
| 20 | + const strip = makePrefixRemover(["window", "globalThis"]); |
| 21 | + assert.strictEqual(strip("window.bar"), "bar"); |
| 22 | + assert.strictEqual(strip("globalThis.console"), "console"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns the original string when no prefix matches", () => { |
| 26 | + const strip = makePrefixRemover(["window"]); |
| 27 | + assert.strictEqual(strip("document.title"), "document.title"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("handles nested member expressions", () => { |
| 31 | + const strip = makePrefixRemover(["window"]); |
| 32 | + assert.strictEqual(strip("window.document.title"), "document.title"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("accepts any iterable of prefixes", () => { |
| 36 | + const strip = makePrefixRemover(new Set(["window"])); |
| 37 | + assert.strictEqual(strip("window.location"), "location"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("uses the first matching prefix based on input order", () => { |
| 41 | + const strip1 = makePrefixRemover(["window.document", "window"]); |
| 42 | + assert.strictEqual(strip1("window.document.title"), "title"); |
| 43 | + const strip2 = makePrefixRemover(["window", "window.document"]); |
| 44 | + assert.strictEqual(strip2("window.document.title"), "document.title"); |
| 45 | + }); |
| 46 | +}); |
0 commit comments