Here is how I wrote test to cover my back button directive in AngularJS.
my directive look like this:
app.directive('goBack', function ($window) {
return {
template: '<button id="button_goback" title="Go Back to Previous Page" class="btn btn-default" style="">Back</button>',
restrict: 'E',
replace: true,
link: function (scope, element) {
element.bind('click', function () {
$window.history.back();
});
}
};
});
and what I want to test is:
- I don't have to test JavaScript library to see if actually url was changed
- I want to make sure when this button is clicked, history.back is called
and here is my test look like:
it('should call history.back when back button is clicked', inject(function () {
spyOn($window.history, 'back');
element.find('button').click();
expect($window.history.back).toHaveBeenCalled();
}));