Skip to content

Commit

Permalink
test(ngModel): group validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Narretz committed Nov 17, 2014
1 parent 4d4e603 commit c9899c5
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,72 +484,75 @@ describe('NgModelController', function() {

});

describe('validations pipeline', function() {
describe('validation', function() {

it('should perform validations when $validate() is called', function() {
scope.$apply('value = ""');

var validatorResult = false;
ctrl.$validators.someValidator = function(value) {
return validatorResult;
};

ctrl.$validate();
describe('$validate', function() {
it('should perform validations when $validate() is called', function() {
scope.$apply('value = ""');

expect(ctrl.$valid).toBe(false);
var validatorResult = false;
ctrl.$validators.someValidator = function(value) {
return validatorResult;
};

validatorResult = true;
ctrl.$validate();
ctrl.$validate();

expect(ctrl.$valid).toBe(true);
});
expect(ctrl.$valid).toBe(false);

it('should always perform validations using the parsed model value', function() {
var captures;
ctrl.$validators.raw = function() {
captures = arguments;
return captures[0];
};
validatorResult = true;
ctrl.$validate();

ctrl.$parsers.push(function(value) {
return value.toUpperCase();
expect(ctrl.$valid).toBe(true);
});

ctrl.$setViewValue('my-value');
describe('view -> model update', function() {
it('should always perform validations using the parsed model value', function() {
var captures;
ctrl.$validators.raw = function() {
captures = arguments;
return captures[0];
};

expect(captures).toEqual(['MY-VALUE', 'my-value']);
});
ctrl.$parsers.push(function(value) {
return value.toUpperCase();
});

it('should always perform validations using the formatted view value', function() {
var captures;
ctrl.$validators.raw = function() {
captures = arguments;
return captures[0];
};
ctrl.$setViewValue('my-value');

ctrl.$formatters.push(function(value) {
return value + '...';
expect(captures).toEqual(['MY-VALUE', 'my-value']);
});

scope.$apply('value = "matias"');
it('should always perform validations using the formatted view value', function() {
var captures;
ctrl.$validators.raw = function() {
captures = arguments;
return captures[0];
};

expect(captures).toEqual(['matias', 'matias...']);
});
ctrl.$formatters.push(function(value) {
return value + '...';
});

it('should only perform validations if the view value is different', function() {
var count = 0;
ctrl.$validators.countMe = function() {
count++;
};
scope.$apply('value = "matias"');

ctrl.$setViewValue('my-value');
expect(count).toBe(1);
expect(captures).toEqual(['matias', 'matias...']);
});

ctrl.$setViewValue('my-value');
expect(count).toBe(1);
it('should only perform validations if the view value is different', function() {
var count = 0;
ctrl.$validators.countMe = function() {
count++;
};

ctrl.$setViewValue('your-value');
expect(count).toBe(2);
ctrl.$setViewValue('my-value');
expect(count).toBe(1);

ctrl.$setViewValue('my-value');
expect(count).toBe(1);

ctrl.$setViewValue('your-value');
expect(count).toBe(2);
});
});

it('should perform validations twice each time the model value changes within a digest', function() {
Expand Down

0 comments on commit c9899c5

Please sign in to comment.