Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 8013145

Browse files
committed
test(ngModel): add two more tests to ngModel, make helpers more complex
1 parent 807fbf9 commit 8013145

File tree

1 file changed

+80
-9
lines changed

1 file changed

+80
-9
lines changed

test/ng/directive/inputSpec.js

+80-9
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ describe('NgModelController', function() {
935935
});
936936

937937
describe('initialization', function() {
938-
var formElm, inputElm, ctrl, scope, $compile, $compileProvider;
938+
var formElm, inputElm, ctrl, scope, $compile, $sniffer, $compileProvider, changeInputValueTo;
939939

940940
function compileInput(inputHtml) {
941941
inputElm = jqLite(inputHtml);
@@ -946,19 +946,25 @@ describe('initialization', function() {
946946
scope.$digest();
947947
}
948948

949-
function addValidator(isValid) {
949+
function addValidator(validity, shouldObserve) {
950+
if (!isDefined(shouldObserve)) {
951+
shouldObserve = true;
952+
}
953+
950954
$compileProvider.directive('obs', function() {
951955
return {
952956
require: 'ngModel',
953957
link: function(scope, element, attrs, ngModelCtrl) {
954958

955-
ngModelCtrl.$validators.obs = function(value) {
956-
return isValid;
959+
ngModelCtrl.$validators.obs = isFunction(validity) ? validity : function(value) {
960+
return validity;
957961
};
958962

959-
attrs.$observe('obs', function() {
960-
ngModelCtrl.$validate();
961-
});
963+
if (shouldObserve) {
964+
attrs.$observe('obs', function() {
965+
ngModelCtrl.$validate();
966+
});
967+
}
962968
}
963969
};
964970

@@ -995,16 +1001,22 @@ describe('initialization', function() {
9951001
$compileProvider = _$compileProvider_;
9961002
}));
9971003

998-
beforeEach(inject(function(_$compile_, _$rootScope_) {
1004+
beforeEach(inject(function(_$compile_, _$rootScope_, _$sniffer_) {
9991005
$compile = _$compile_;
1006+
$sniffer = _$sniffer_;
10001007
scope = _$rootScope_;
1008+
1009+
changeInputValueTo = function(value) {
1010+
inputElm.val(value);
1011+
browserTrigger(inputElm, $sniffer.hasEvent('input') ? 'input' : 'change');
1012+
};
10011013
}));
10021014

10031015
afterEach(function() {
10041016
dealoc(formElm);
10051017
});
10061018

1007-
// This fails with string formatter
1019+
// https://github.com/angular/angular.js/issues/9959
10081020
it('should not change model of type number to string with validator using observer', function() {
10091021
addValidator(true);
10101022
scope.value = 12345;
@@ -1018,6 +1030,7 @@ describe('initialization', function() {
10181030
expect(scope.ngChangeSpy).not.toHaveBeenCalled();
10191031
});
10201032

1033+
//https://github.com/angular/angular.js/issues/9063
10211034
it('should not set a null model that is invalid to undefined', function() {
10221035
addValidator(false);
10231036
scope.value = null;
@@ -1048,6 +1061,7 @@ describe('initialization', function() {
10481061
expect(scope.ngChangeSpy).not.toHaveBeenCalled();
10491062
});
10501063

1064+
// https://github.com/angular/angular.js/issues/10025
10511065
it('should not change a model that uses custom $formatters and $parsers', function() {
10521066
addValidator(true);
10531067
addFormatter(function (modelValue) {
@@ -1064,6 +1078,63 @@ describe('initialization', function() {
10641078
expect(inputElm).toBeValid();
10651079
expect(scope.value).toBe('abc');
10661080
});
1081+
1082+
describe('$validate', function() {
1083+
1084+
// Sanity test: since a parse error sets the modelValue to undefined, the
1085+
// $$rawModelValue will always be undefined, hence $validate does not have
1086+
// a 'good' value to update
1087+
it('should not update a model that has a parse error', function() {
1088+
scope.value = 'abc';
1089+
addParser(function() {
1090+
return undefined;
1091+
});
1092+
1093+
addValidator(true, false);
1094+
1095+
compileInput('<input type="text" name="textInput" obs parse ng-model="value"/>');
1096+
expect(inputElm).toBeValid();
1097+
expect(scope.value).toBe('abc');
1098+
1099+
changeInputValueTo('xyz');
1100+
expect(inputElm).toBeInvalid();
1101+
expect(scope.value).toBeUndefined();
1102+
expect(ctrl.$error.parse).toBe(true);
1103+
1104+
ctrl.$validate();
1105+
expect(inputElm).toBeInvalid();
1106+
expect(scope.value).toBeUndefined();
1107+
});
1108+
1109+
it('should restore the last valid modelValue when a validator becomes valid', function() {
1110+
scope.value = 'abc';
1111+
scope.count = 0;
1112+
1113+
addValidator(function() {
1114+
scope.count++;
1115+
dump('count', scope.count);
1116+
return scope.count === 1 ? true : scope.count === 2 ? false : true;
1117+
});
1118+
1119+
compileInput('<input type="text" name="textInput" obs ng-model="value"/>');
1120+
expect(inputElm).toBeValid();
1121+
expect(scope.value).toBe('abc');
1122+
expect(ctrl.$viewValue).toBe('abc');
1123+
1124+
ctrl.$validate();
1125+
scope.$digest();
1126+
expect(inputElm).toBeInvalid();
1127+
expect(scope.value).toBeUndefined();
1128+
expect(ctrl.$viewValue).toBe('abc');
1129+
1130+
ctrl.$validate();
1131+
scope.$digest();
1132+
expect(inputElm).toBeValid();
1133+
expect(scope.value).toBe('abc');
1134+
});
1135+
1136+
1137+
});
10671138
});
10681139

10691140
describe('ngModel', function() {

0 commit comments

Comments
 (0)