Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS unit testing error when using clientWidth

In my code there's a line:

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

It works fine when running the app but when unit testing I get error:

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

How to solve this issue?

like image 702
Julius Avatar asked Nov 01 '22 18:11

Julius


1 Answers

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

Remember to remove mock/spy on angular element after test is completed as it will interfere with the jasmine framework which uses that internally.

like image 50
Shanu Gupta Avatar answered Nov 11 '22 14:11

Shanu Gupta