Skip to content

Commit a812954

Browse files
committed
Demonstrate tests for issue angular#14734
1 parent 3fe3da8 commit a812954

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/ng/directive/ngModel.js

+1
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ NgModelController.prototype = {
604604
var syncValidatorsValid = true;
605605
forEach(that.$validators, function(validator, name) {
606606
var result = validator(modelValue, viewValue);
607+
// var result = Boolean(validator(modelValue, viewValue));
607608
syncValidatorsValid = syncValidatorsValid && result;
608609
setValidity(name, result);
609610
});

test/ng/directive/ngModelSpec.js

+68
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,74 @@ describe('ngModel', function() {
847847
expect(ctrl.$valid).toBe(true);
848848
});
849849

850+
// See also the test on L886.
851+
fit('should demonstrate how synchronous validators work now', function() {
852+
var curry = function(v) {
853+
return function() {
854+
return v;
855+
};
856+
};
857+
858+
var tester = function(v, e) {
859+
ctrl.$modelValue = undefined;
860+
ctrl.$validators.a = curry(v);
861+
862+
ctrl.$validate();
863+
expect(ctrl.$valid).toBe(e);
864+
};
865+
866+
// False tests
867+
tester(false, false);
868+
tester(undefined, undefined);
869+
tester(null, true);
870+
tester(0, true);
871+
tester(NaN, true);
872+
tester('', true);
873+
874+
// True tests
875+
tester(true, true);
876+
tester(1, true);
877+
tester('0', true);
878+
tester('false', true);
879+
tester([], true);
880+
tester({}, true);
881+
});
882+
883+
// Flip this test with the one above (L851) as well as line L606 with L607 in `ngModel.js`
884+
// The differences between the two would likely being a breaking change
885+
// Though the feature it is breaking was undocumented.
886+
xit('should demonstrate how synchronous validators will work after this change', function() {
887+
var curry = function(v) {
888+
return function() {
889+
return v;
890+
};
891+
};
892+
893+
var tester = function(v, e) {
894+
ctrl.$modelValue = undefined;
895+
ctrl.$validators.a = curry(v);
896+
897+
ctrl.$validate();
898+
expect(ctrl.$valid).toBe(e);
899+
};
900+
901+
// False tests
902+
tester(false, false);
903+
tester(undefined, false);
904+
tester(null, false);
905+
tester(0, false);
906+
tester(NaN, false);
907+
tester('', false);
908+
909+
// True tests
910+
tester(true, true);
911+
tester(1, true);
912+
tester('0', true);
913+
tester('false', true);
914+
tester([], true);
915+
tester({}, true);
916+
});
917+
850918

851919
it('should register invalid validations on the $error object', function() {
852920
var curry = function(v) {

0 commit comments

Comments
 (0)