I have a function in my directive controller and I am trying to test and couldn't find any resources. I have in before each
before each {
scope = $rootScope ;
$compile(element)(scope);
scope.$digest();
}
it('should update days when datepicker is changed', function () {
scope.seldate = new Date('4/11/2014');
scope.stdate = new Date('4/1/2014');
scop`enter code here`e.days = 10;
scope.$digest();
scope.$apply(function() {
scope.seldate = new Date('4/12/2014');
scope.datePickerChange(); // This is a function in my directive controller
});
expect(scope.days).toBe(11);
});
app.directive('mydirective',function(){
return {
restrict:'E',
scope:{
days: '=',
selectedDate: '=',
startDate: '='
},
$scope.datePickerChange = function () {
//Sod is helper for start of the day with hours/mins/seconds set to 0
$scope.days = moment(new Date($scope.selectedDate)).sod().diff($scope.getStartDate(), 'days');
};
};
});
This is throwing an error TypeError: Object # has no method 'datePickerChange'
Your directive declaration is invalid:
app.directive('mydirective',function(){
return {
restrict:'E',
scope:{
days: '=',
selectedDate: '=',
startDate: '='
},
link: function(scope, elem, attrs) {
scope.datePickerChange = function () {
//Sod is helper for start of the day with hours/mins/seconds set to 0
scope.days = moment(new Date($scope.selectedDate))
.sod().diff($scope.getStartDate(), 'days');
};
}
}
});
Beyond that to test your directives you should follow examples you can see in Angular's github repo. ngSwitch is a good example, IMO
The basic idea is you use $compile to execute your directive and examine it.
it('should do something', inject(function($rootScope, $compile) {
var scope = $rootScope.$new();
var element = $compile('<my-directive></my-directive>')(scope);
//assert things.
expect(scope.days).toEqual(somethingHere);
scope.datePickerChange();
expect(scope.days).toEqual(somethingElse);
});