mardi 3 février 2015

Unit Testing Custom Directive. A Password validation. AngularJS

I have this AngularJs Directive and i need to do a unit test for this using karma-jasmine tester. The directive matches two input fields and sets the form valid if the two input text matches. ( a password validator)





angular.module('UserValidation', []).directive('validPasswordC', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue, $scope) {
var noMatch = viewValue != scope.myForm.password.$viewValue;
ctrl.$setValidity('noMatch', !noMatch)
})
}
}
});



And this is my HTML form





<form name='myForm'>
<!-- other stuff -->

<label>Password</label><br/>
<input type='password'ng-model='password' name='password' required ng-minlength="8" ng-maxlength="20" ng-pattern="/(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])(?=.*[!@#$%^&*><.+_])/" />
<label for="password_c">Confirm Password</label>
<input type="password" id="password_c" name="password_c" ng-model="formData.password_c" valid-password-c required />

<!-- other stuff here -->

<button ng-click='adduser()' class="btn btn-default" ng-disabled="!myForm.$valid"> Add User </button>

</form>



and this is the TEST case i'm trying





describe("should match the password", function () {
var scope, myform;
beforeEach(module('UserValidation'));
beforeEach(inject(function ($compile, $rootScope) {
scope=$rootScope;
var element = angular.element(
'<form name="myForm">' +
'<input type="password" id="password_c" name="password_c" ng-model="formData.password_c" valid-password-c required />' +
'</form>'
);
scope.model = {some: null };
$compile(element)(scope);
scope.$digest();
myform = scope.myForm;
}));
it('should validate the directive', function () {
//how do i test this when it actually validates two input fields
// i cant seem to find out how, PS im new to angular
})



});



Aucun commentaire:

Enregistrer un commentaire