vino
vino

Reputation: 57

Unit Testing Angular Function using Karma Jasmine

I have a function like this inside a controller

$scope.closeMenu = function () {
                    $('#menu').hide();
                };

If a functions returns a value I am able to test using expect($scope.value).toEqual(); How do we test the above function using jasmine. Please help thanks

Upvotes: 0

Views: 268

Answers (1)

Michel
Michel

Reputation: 28229

If a functions returns a value I am able to test using expect($scope.value).toEqual(); How do we test the above function using jasmine

You should rewrite your function, so as it only set a variable of the model. Then it will be testable using the way you already know in Jasmine. One of the point of angular is that you don't manipulate the DOM from the controller. Providing you follow these guidelines, controllers are far easier to test.

Moreover, in your case rewriting is very straightforward:

$scope.closeMenu = function () {
    $scope.isOpen = false;
};

Template:

... id="menu" ng-show="isOpen" ...

In case you still need to test some characteristics of a DOM element, for example it's visibility, jasmine-jquery might be useful for you. Example:

expect($('#menu')).toBeHidden()

Upvotes: 1

Related Questions