I have a method init
my controller
like below, how can i test this:
vm.init = function ($event) {
var target = angular.element($event.target);
if(target.prop('tagName').toUpperCase() == 'A') {
return false;
}
};
TestSpec
beforeEach(function(){
vm.init = sinon.spy();
compiled = $compile(template)($scope);
$scope.$digest();
mockEvent = new Event('click');
sinon.spy(mockEvent, 'preventDefault');
});
it('init() method should return false', function() {
vm.init(mockEvent);
});
TypeError: Cannot read property 'target' of undefined
How can i pass the $event
here, below is my html where the event is triggered.
template = '<div id="fre"><span ng-click="init($event)"></span></div>';
You can mock the $event
creating an object with a target property set to any HTML string.
it('init() method should return false', function() {
var mockEvent = {
target: '<a href="#">Click</a>'
};
expect(controller.init(mockEvent)).toBe(false)
});