UnularJS unit testing error when using clientWidth

There is a line in my code:

var contentWidth = angular.element(document.querySelector('.content'))[0].clientWidth; 

It works great when starting the application, but when unit testing, I get an error:

 TypeError: 'undefined' is not an object (evaluating 'angular.element(document.querySelector('.content'))[0].clientWidth') 

How to solve this problem?

+5
source share
1 answer

Mount the DOM when running the test or mock the document.querySelector and angular.element in case you don't need the same thing.

Remember to remove mock / spy on the angular element after completing the test, as this will interfere with the jasmine structure that uses this internally.

+1
source

Source: https://habr.com/ru/post/1200122/


All Articles