Π― ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Ρ, ΡΡΠΎ SVGGElement ( Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°ΡΠΈΡ MDN ) Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅ΡΡΡ Π² jsdom?
const { JSDOM } = require("jsdom");
const options = {
... your options here ...
};
const dom = new JSDOM(`
<svg>
<g id="g">
</svg>
`, options);
const g = dom.window.document.querySelector("#g");
if (g instanceof SVGGElement) { // throws "SVGGElement is not defined"
console.log("ok");
}
https://jsfiddle.net/p3ag4v85/
(ΠΏΡΠΎΠ²Π΅ΡΡΡΠ΅ Π² ΠΊΠΎΠ½ΡΠΎΠ»ΠΈ ΠΈ Π½Π°ΠΉΠ΄ΠΈΡΠ΅ ok
)
Π― Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡΠΊΠ°Π·Π°ΡΡ, ΡΡΠΎ Π·Π°ΠΌΠ΅ΡΠΈΠ» ΠΎΡΠΈΠ±ΠΊΡ, ΠΊΠΎΠ³Π΄Π° ΡΠ΅ΡΡΠΈΡΠΎΠ²Π°Π» ΡΠ²ΠΎΠΉ ΠΊΠΎΠ΄ Ρ ΠΏΠΎΠΌΠΎΡΡΡ Jest, ΡΠΎ Π΅ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π» jsdom Π²Π½ΡΡΡΠΈ. ΠΠΎΡ ΠΎΡΠΈΠ±ΠΊΠ°, ΠΊΠΎΡΠΎΡΡΡ Ρ ΠΏΠΎΠ»ΡΡΠΈΠ»:
β should throw an Error if g element is not found
expect(received).toThrowError(expected)
Expected message: "group element not found"
Received message: "SVGGElement is not defined"
46 | if (!(svgElement instanceof SVGSVGElement))
47 | throw new Error("svg element not found");
> 48 | if (!(groupElement instanceof SVGGElement))
| ^
49 | throw new Error("g element not found");
50 | this.groupElement = groupElement;
51 | this.groupBBoxWidth = 0;
at new Lib (lib/js/index.js:48:39)
at test/MyLib/constructor.test.js:86:15
at Object.<anonymous> (node_modules/expect/build/toThrowMatchers.js:81:11)
at Object.toThrowError (node_modules/expect/build/index.js:342:33)
at Object.<anonymous> (test/MyLib/constructor.test.js:87:7)
85 | expect(
86 | () => new Lib({ svgIdentifier: "svg", groupIdentifier: "g" })
> 87 | ).toThrowError(new TypeError("group element not found"));
| ^
88 | });
89 |
at Object.<anonymous> (test/MyLib/constructor.test.js:87:7)
SVGElement
ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅ΡΡΡ. ΠΠ΄Π½Π°ΠΊΠΎ Π²Π°Ρ ΠΊΠΎΠ΄ ΠΈΡΠ΅Ρ SVGElement
Π² Π³Π»ΠΎΠ±Π°Π»ΡΠ½ΠΎΠΌ Node.js. ΠΠ°ΠΌ Π½ΡΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ dom.window.SVGElement
.
ΠΠ³Π°. ΠΡΠΎ Π΄Π΅ΠΉΡΡΠ²ΠΈΡΠ΅Π»ΡΠ½ΠΎ ΠΎΡΠΈΠ±ΠΊΠ°. Π‘ΠΏΠ΅ΡΠΈΡΠΈΠΊΠ°ΡΠΈΡ, ΠΏΠΎΡ ΠΎΠΆΠ΅, Π½Π΅ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎ ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π° Π΄Π»Ρ Π³ΡΡΠΏΠΏ SVG.
<!DOCTYPE html>
<html>
<body>
<svg id="svg">
<g id="group"></g>
</svg>
<script>
"use strict";
window.addEventListener("load", function() {
const svg = document.getElementById("svg");
const group = document.getElementById("group");
console.dir(svg.constructor); // Ζ SVGSVGElement()
console.dir(group.constructor); // Ζ SVGGElement()
});
</script>
</body>
</html>
"use strict";
const { JSDOM } = require("jsdom");
const dom = new JSDOM(`
<svg id="svg">
<g id="group"></g>
</svg>
`);
const svg = dom.window.document.getElementById("svg");
const group = dom.window.document.getElementById("group");
console.log(svg.constructor); // [Function: SVGSVGElement]
console.log(group.constructor); // [Function: SVGElement]
Π Π΅ΠΏΠΎΠ·ΠΈΡΠΎΡΠΈΠΉ Π²ΠΎΡΠΏΡΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½ΠΈΡ GitHub .
ΠΠ΅ΠΉΡΡΠ²ΠΈΡΠ΅Π»ΡΠ½ΠΎ, ΠΏΠΎΡ
ΠΎΠΆΠ΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΌΡ, ΡΡΠΎ ΠΌΡ Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅ΠΌ ΡΠ»Π΅ΠΌΠ΅Π½Ρ SVG g
. ΠΠ°ΡΠ° ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠ° SVG Π½Π° Π΄Π°Π½Π½ΡΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ Π½Π΅ ΡΠ»ΠΈΡΠΊΠΎΠΌ Ρ
ΠΎΡΠΎΡΠ°.
Π‘Π°ΠΌΡΠΉ ΠΏΠΎΠ»Π΅Π·Π½ΡΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΉ
ΠΠ΅ΠΉΡΡΠ²ΠΈΡΠ΅Π»ΡΠ½ΠΎ, ΠΏΠΎΡ ΠΎΠΆΠ΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΌΡ, ΡΡΠΎ ΠΌΡ Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅ΠΌ ΡΠ»Π΅ΠΌΠ΅Π½Ρ SVG
g
. ΠΠ°ΡΠ° ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠ° SVG Π½Π° Π΄Π°Π½Π½ΡΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ Π½Π΅ ΡΠ»ΠΈΡΠΊΠΎΠΌ Ρ ΠΎΡΠΎΡΠ°.