diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 17aab79183c4..5ba057647357 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -3849,6 +3849,102 @@ describe('input', function() { }); }); + describe('ngRequired', function() { + + describe('when the ngRequired expression initially evaluates to true', function() { + + it('should be valid even if value is 0', function() { + compileInput(''); + + changeInputValueTo('0'); + expect(inputElm).toBeValid(); + expect(scope.value).toBe(0); + expect(scope.form.numberInput.$error.required).toBeFalsy(); + }); + + it('should be valid even if value 0 is set from model', function() { + compileInput(''); + + scope.$apply('value = 0'); + + expect(inputElm).toBeValid(); + expect(inputElm.val()).toBe('0'); + expect(scope.form.numberInput.$error.required).toBeFalsy(); + }); + + it('should register required on non boolean elements', function() { + compileInput('
'); + + scope.$apply("value = ''"); + + expect(inputElm).toBeInvalid(); + expect(scope.form.numberInput.$error.required).toBeTruthy(); + }); + + it('should change from invalid to valid when the value is empty and the ngRequired expression changes to false', function() { + compileInput(''); + + scope.$apply('ngRequiredExpr = true'); + + expect(inputElm).toBeInvalid(); + expect(scope.value).toBeUndefined(); + expect(scope.form.numberInput.$error.required).toBeTruthy(); + + scope.$apply('ngRequiredExpr = false'); + + expect(inputElm).toBeValid(); + expect(scope.value).toBeUndefined(); + expect(scope.form.numberInput.$error.required).toBeFalsy(); + }); + }); + + describe('when the ngRequired expression initially evaluates to false', function() { + + it('should be valid even if value is empty', function() { + compileInput(''); + + expect(inputElm).toBeValid(); + expect(scope.value).toBeUndefined(); + expect(scope.form.numberInput.$error.required).toBeFalsy(); + expect(scope.form.numberInput.$error.number).toBeFalsy(); + }); + + it('should be valid if value is non-empty', function() { + compileInput(''); + + changeInputValueTo('42'); + expect(inputElm).toBeValid(); + expect(scope.value).toBe(42); + expect(scope.form.numberInput.$error.required).toBeFalsy(); + }); + + it('should not register required on non boolean elements', function() { + compileInput('
'); + + scope.$apply("value = ''"); + + expect(inputElm).toBeValid(); + expect(scope.form.numberInput.$error.required).toBeFalsy(); + }); + + it('should change from valid to invalid when the value is empty and the ngRequired expression changes to true', function() { + compileInput(''); + + scope.$apply('ngRequiredExpr = false'); + + expect(inputElm).toBeValid(); + expect(scope.value).toBeUndefined(); + expect(scope.form.numberInput.$error.required).toBeFalsy(); + + scope.$apply('ngRequiredExpr = true'); + + expect(inputElm).toBeInvalid(); + expect(scope.value).toBeUndefined(); + expect(scope.form.numberInput.$error.required).toBeTruthy(); + }); + }); + }); + describe('minlength', function() { it('should invalidate values that are shorter than the given minlength', function() {