When working with an angular directive, I am currently using $(element).fdatepicker()
. How can I mock or stub this function in a jasmine test for the directive?
If I don't stub it, I encounter the following error:
TypeError: 'undefined' is not a function (evaluating '$(element).fdatepicker({
The directive code snippet is as follows:
angular.module('admin').directive("datePicker", function($http) {
return {
require: "ngModel",
link: function(scope, element, attrs, ngModelCtrl) {
if (!ngModelCtrl) {
return;
}
$(element).fdatepicker();
ngModelCtrl.$parsers.unshift(function(value) {
// parser logic
});
return ngModelCtrl.$formatters.unshift(function(value) {
// formatter logic
});
}
};
});
Below are my test cases:
describe('datePicker directive', function() {
beforeEach(function() {
var element;
module('admin');
element = angular.element("<input ng-model='myDate' date-picker></input>");
inject(function($rootScope, $compile) {
var scope;
scope = $rootScope.$new();
$compile(element)(scope);
scope.$digest();
});
});
it('should correctly parse the value', function() {
// ...
});
it('should properly format the value', function() {
// ...
});
});