Jsdom: SVGGElement๊ฐ€ ์ •์˜๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2019๋…„ 12์›” 03์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: jsdom/jsdom

SVGGElement( MDN ๋ฌธ์„œ )๊ฐ€ jsdom์—์„œ ์ง€์›๋˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๊นŒ?

๊ธฐ๋ณธ ์ •๋ณด:

  • Node.js ๋ฒ„์ „: 10.150.
  • jsdom ๋ฒ„์ „: 11.5.1

์ตœ์†Œํ•œ์˜ ์žฌ์ƒ์‚ฐ ์ผ€์ด์Šค

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 ์ฐพ์Šต๋‹ˆ๋‹ค)

๋…ธํŠธ

๋‚ด๋ถ€์ ์œผ๋กœ jsdom์„ ์‚ฌ์šฉํ•˜๋Š” Jest๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•  ๋•Œ ๋ฒ„๊ทธ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค๊ณ  ๋งํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋‚ด๊ฐ€ ์–ป์€ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.

โ— 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)

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์‹ค์ œ๋กœ SVG g ์š”์†Œ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ๋ณด์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ SVG ์ง€์›์€ ๊ทธ๋‹ค์ง€ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  3 ๋Œ“๊ธ€

SVGElement ๊ฐ€ ์ง€์›๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๊ท€ํ•˜์˜ ์ฝ”๋“œ๋Š” Node.js ์ „์—ญ์—์„œ SVGElement ๋ฅผ ์ฐพ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 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 ์ง€์›์€ ๊ทธ๋‹ค์ง€ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰