Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test on destroy scope

How to unit testing an $destroy event of a Directive in angularjs?

I have the code in my directive:

scope.$on('$destroy', function () {
    //clean something
});

My test code:

it('on destroy',function(){
    scope.$destroy();
    scope.$digest();

    //expect everything done?
});

Any suggestion!

like image 996
Bita Ho Avatar asked Sep 27 '13 11:09

Bita Ho


1 Answers

You should test the code that is executed within the $destroy event. Here's a contrived example using a controller:

Test

it('sets destroyed to true when the scope is destroyed', function() {
  // Arrange
  $scope.destroyed = false;

  // Act
  $scope.$destroy();

  // Assert
  expect($scope.destroyed).toBe(true);
});

Controller

app.controller('MainCtrl', function($scope) {
  $scope.$on('$destroy', function() {
    $scope.destroyed = true;
  });
});

Plunker here.

like image 170
Michael Benford Avatar answered Sep 28 '22 19:09

Michael Benford