I have an angularjs app, which validates certain input fields. I was looking to write unit tests via Jasmine to test and maintain the validity of these fields.
NOTE: The validation works fine normally, just with jasmine, it doesn't seem to update. The unit tests have no syntax errors, but simply result in:
Error: Expected false to equal true.
at new jasmine.ExpectationResult
at null.toEqual
at null.<anonymous>
at jasmine.Block.execute
at jasmine.Queue.next_
at chrome-extension
For instance, I have, in the directives:
}).directive('billingNumberPopup', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
scope.$watch(
function() {
return ctrl.$viewValue;
},
function(value){
numValidation(value);
}
);
function numValidation(viewValue){
if (!viewValue || viewValue == "" || (!viewValue.toString().match(/[a-z]/gi) && viewValue.toString().match(/[0-9]/g).length == 6)){
ctrl.$setValidity('billingNumber',true);
}
else
{
ctrl.$setValidity('billingNumber',false);
}
and then from my unit tests...
it('Check if validation works', function(){
var len = $scope.dataToPost.length;
$scope.addRow();
console.log("Hi");
$scope.$apply(function(){
$scope.dataToPost[len].billingNumber = "HELLO";});
$scope.$apply();
console.log($scope.dataToPost[len].billingNumber);
console.log($("input[ng-model='d.billingNumber']"));
expect($("input[ng-model='d.billingNumber']")[len].classList.contains("ng-invalid")).toEqual(true);
});
where "HELLO" is not a valid billing number, and scope.dataToPost is the data that is binded to the input fields. I would assume, that changing the value, and calling $scope.$apply would trigger validation, any suggestions?