I built a new directive in Angular (1.2.29) and I am trying to test it using karma, however I am having some problems when checking if the field is invalid, it seems it's always valid.
Here is my directive:
var EmailOrUrlValidation = function emailOrUrlValidation() {
console.log("on directive 0");
return {
require: 'ngModel',
restrict: 'A',
scope: {
checkDirty: '=validationCheckDirty'
},
link: function (scope, element, attrs, ctrl) {
console.log("on directive 1");
scope.$watch(function () {
scope.checkDirty = scope.checkDirty ? true : false;
element.attr('maxlength', 50);
ctrl.$parsers.unshift(validateEmailOrUrl);
ctrl.$formatters.unshift(validateEmailOrUrl);
});
function validateEmailOrUrl(value) {
console.log("on directive 2");
var urlRegexp = "myurlregex";
var emailPattern = "myemailregex";
if (!value) {
ctrl.$setValidity("valid", true);
} else if (scope.checkDirty && !ctrl.$dirty) {
ctrl.$setValidity("valid", true);
} else {
var emailRegex = new RegExp(emailPattern);
var emailOrUrlValid = urlRegexp.test(value) || emailRegex.test(value);
ctrl.$setValidity("valid", emailOrUrlValid);
}
return value;
}
}
};
};
this.app = angular.module('shared.validations', [])
.directive('emailOrUrlValidation', [emailOrUrlValidation])
Here is my unit testing (I followed this example https://stackoverflow.com/a/22772504/1031021):
describe('EmailOrUrlValidation tests', function () {
var $scope, form, element;
beforeEach(module("shared.validations"));
beforeEach(inject(function ($compile, $rootScope) {
$scope = $rootScope;
element = angular.element(
'<form name="form">' +
'<input data-ng-model="model.emailorurl" name="emailorurl" email-or-url-validation data-ng-required="true" validation-check-dirty="true" />' +
'</form>'
);
$scope.model = { emailorurl: null }
$compile(element)($scope);
form = $scope.form;
}));
describe('invalid url', function () {
it('should pass with valid url', function () {
form.emailorurl.$setViewValue('www.google.pt');
$scope.$digest();
expect(form.emailorurl.$invalid).to.be.false;
});
it('should not pass with invalid url', function () {
form.emailorurl.$setViewValue('testing');
$scope.$digest();
expect(form.emailorurl.$invalid).to.be.true;
});
});
});
However one of the assertions fail, because $invalid is always false...
I inserted some logs inside the function validateEmailOrUrl and its never shown on my console, however logs outside of function appear, I assume the function its not being triggered.
Can someone help me? What is wrong here?