From 5350280057c9c7bfdb9a56dfe542f60eac015723 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 15 Aug 2026 12:57:02 +0200 Subject: [PATCH] Modernize the `XRefMock` unit-test helper --- test/unit/name_number_tree_spec.js | 10 ++++---- test/unit/primitives_spec.js | 2 +- test/unit/test_utils.js | 38 ++++++++++++++++-------------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/test/unit/name_number_tree_spec.js b/test/unit/name_number_tree_spec.js index 3432c51101a8b..48e5103dbb8ce 100644 --- a/test/unit/name_number_tree_spec.js +++ b/test/unit/name_number_tree_spec.js @@ -21,7 +21,7 @@ import { XRefMock } from "./test_utils.js"; describe("NameOrNumberTree", function () { describe("NameTree", function () { it("should return an empty map when root is null", function () { - const xref = new XRefMock([]); + const xref = new XRefMock(); const tree = new NameTree(null, xref); expect(tree.getAll().size).toEqual(0); }); @@ -30,7 +30,7 @@ describe("NameOrNumberTree", function () { const root = new Dict(); root.set("Names", ["alpha", "value_a", "beta", "value_b"]); - const xref = new XRefMock([]); + const xref = new XRefMock(); const tree = new NameTree(root, xref); const map = tree.getAll(); @@ -66,7 +66,7 @@ describe("NameOrNumberTree", function () { const root = new Dict(); root.set("Kids", [inlineLeaf]); - const xref = new XRefMock([]); + const xref = new XRefMock(); const tree = new NameTree(root, xref); // Should not throw even though the kid is an inline Dict (not a Ref). @@ -126,7 +126,7 @@ describe("NameOrNumberTree", function () { const root = new Dict(); root.set("Nums", [1, "one", 2, "two"]); - const xref = new XRefMock([]); + const xref = new XRefMock(); const tree = new NumberTree(root, xref); const map = tree.getAll(); @@ -144,7 +144,7 @@ describe("NameOrNumberTree", function () { const root = new Dict(); root.set("Kids", [inlineLeaf]); - const xref = new XRefMock([]); + const xref = new XRefMock(); const tree = new NumberTree(root, xref); const map = tree.getAll(); diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index c047dfa615856..fb31e53f08da1 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -127,7 +127,7 @@ describe("primitives", function () { const dict = new Dict(null); expect(dict.xref).toBeNull(); - const xref = new XRefMock([]); + const xref = new XRefMock(); dict.assignXref(xref); expect(dict.xref).toEqual(xref); }); diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index cd1cbea0aa054..02e1e0bb92f9d 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -123,36 +123,38 @@ function getCrossOriginHostname(hostname) { } class XRefMock { - constructor(array) { - this._map = Object.create(null); - this._newTemporaryRefNum = null; - this._newPersistentRefNum = null; - this.stream = new NullStream(); - - for (const key in array) { - const obj = array[key]; - this._map[obj.ref.toString()] = obj.data; + #map = new Map(); + + #newPersistentRefNum = null; + + #newTemporaryRefNum = null; + + stream = new NullStream(); + + constructor(array = []) { + for (const { ref, data } of array) { + this.#map.set(ref.toString(), data); } } getNewPersistentRef(obj) { - if (this._newPersistentRefNum === null) { - this._newPersistentRefNum = Object.keys(this._map).length || 1; + if (this.#newPersistentRefNum === null) { + this.#newPersistentRefNum = this.#map.size || 1; } - const ref = Ref.get(this._newPersistentRefNum++, 0); - this._map[ref.toString()] = obj; + const ref = Ref.get(this.#newPersistentRefNum++, 0); + this.#map.set(ref.toString(), obj); return ref; } getNewTemporaryRef() { - if (this._newTemporaryRefNum === null) { - this._newTemporaryRefNum = Object.keys(this._map).length || 1; + if (this.#newTemporaryRefNum === null) { + this.#newTemporaryRefNum = this.#map.size || 1; } - return Ref.get(this._newTemporaryRefNum++, 0); + return Ref.get(this.#newTemporaryRefNum++, 0); } resetNewTemporaryRef() { - this._newTemporaryRefNum = null; + this.#newTemporaryRefNum = null; } countUpdatesAfter(offset) { @@ -160,7 +162,7 @@ class XRefMock { } fetch(ref) { - return this._map[ref.toString()]; + return this.#map.get(ref.toString()); } async fetchAsync(ref) {