I have a directive that adds a validator on my inputs to be used by ng-messages :
angular.module('app.module.submodule').directive('dateFormatFr', function () {
return {
require: 'ngModel',
link: linkValidator
};
});
function linkValidator(scope, elm, attrs, ctrl) {
ctrl.$validators.datefr = function (modelValue) {
if (ctrl.$isEmpty(modelValue)) {
return true;
}
if (!(modelValue instanceof Date)) {
// this check avoid errors on datepickers
return modelValue.match('^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$');
}
return true;
};
}
And I'm trying to unit test it (using ng-describe) :
ngDescribe({
name: 'linkValidator',
modules: 'app.module.submodule',
inject: [],
mocks: {},
only: true,
tests: function () {
var $compile,
$scope,
form;
beforeEach(inject(function (_$compile_, _$rootScope_) {
// The injector unwraps the underscores (_) from around the parameter names when matching (Angular docs)
$compile = _$compile_;
$scope = _$rootScope_;
var tpl = angular.element(
'<form name="form">' +
'<input ng-model="model.date" name="dateField" date-format-fr />' +
'</form>'
);
$scope.model = {
date: null
};
$compile(tpl)($scope);
form = $scope.form;
}));
it('should be valid when the field value is well formatted (DD/MM/AAAA)', function () {
form.dateField.$setViewValue('01/01/2001');
$scope.$digest();
expect(form.dateField.$valid).toBe(true);
expect($scope.model.date).toEqual('01/01/2001');
});
it('should be invalid when date is badly formatted', function () {
form.dateField.$setViewValue('30/02/2001');
$scope.$digest();
expect(form.dateField.$valid).toBe(false);
expect($scope.model.date).toBeUndefined();
});
}
});
But there are 2 problems (that may have the same cause ??) :
- the first test fails because
$scope.model.date
isundefined
(it should be updated with'01/01/2001'
as it is a valid input). I noticed that the$modelValue
fromform.dateField
is also undefined. I know that$scope.model.date
is updated, because if it won't, it would benull
. But whyundefined
? - the second test fails because
$valid
staystrue
(it should be updated withfalse
as it is invalid). Again, I ask : why ?
I tried to compare my code to this post answer but I don't see where is my error.
Please note that the directive is working well in my app forms (model is updating as expected and ng-messages work too), so I have no clue what I'm doing wrong here.
I'm working with Angular 1.4.7, if it helps..
EDIT :
Tried this method with the same result. The $modeValue
keeps being undefined whereas $viewValue
and $$rawModelValue
are updated with the correct String.
These are logs from my test :
console.log($scope.localModel);
// {date:null} before digest
// {date: undefined} after digest
console.log(form.dateField.$viewValue);
// NaN before digest
// '01/01/2001' after digest
console.log(form.dateField.$modelValue);
// NaN before digest
// undefined after digest
console.log(form.dateField.$$rawModelValue);
// undefined before digest
// '01/01/2001' after digest