I am trying to unit test a function that is bound to the ngClick directive. It looks something like this for now as we've just started on this project and before I get to far I want some test coverage:
vm.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
vm.opened = true;
};
I unit test like this:
describe('Unit: simpleSearchController', function(){
//include main module
beforeEach(module('myApp'));
var ctrl, scope, event ;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope){
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
// Create the controller and alias access using controllerAs
ctrl = $controller('simpleSearchController as vm', {
$scope: scope
});
}));
// unit tests
it('should set vm.opened to true', function(){
event = scope.$broadcast("click");
expect(event).toBeDefined();
scope.vm.open(event);
expect(event.defaultPrevented).toBeTruthy();
expect(scope.vm.opened).toBeTruthy();
});
});
When Karma runs the test I get this error:
TypeError: $event.stopPropagation is not a function.
Any ideas?