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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With