S
S
studentLesson2022-04-21 14:30:38
JavaScript
studentLesson, 2022-04-21 14:30:38

How to get coordinates using getBoundingClientRect in node.js environment?

Hello guys! Maybe someone knows, tell me please, the method does not work getBoundingClientRectin node.js, that is, through Mocha.
I explain: I want to write a simple test for some 2 elements (nodes). I use mochaand use to build a house-tree jsdom-global.
It goes something like this:

require('jsdom-global')(`
  <!doctype html>
  <html>
    <body>
      <div class="main">
        <button>
          <span>Нажми</span>
        </button>
      </div>
      <div>
        <div class="main__section">
          <ul>
            <li>Пункт 1</li>
            <li>Пункт 2</li>
          </ul>
        </div>
      </div>
    </body>
  </html>
`);


Next, I get the element and apply some styles (positioning) to it.
const mainNode = document.querySelector('.main');

mainNode.style.width = 100;
mainNode.style.height = 50;
mainNode.style.top = '200px';
mainNode.style.right = '300px';
mainNode.style.bottom = '25px';
mainNode.style.left = '100px';


I check the application of styles - window.getComputedStyle(mainNode)- styles are applied
But if you check the position of the element, then mainNode.getBoundingClientRect()- it returns everything by zero (((

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question