diff options
| author | Misko Hevery | 2010-05-10 20:24:20 -0700 | 
|---|---|---|
| committer | Misko Hevery | 2010-05-10 20:24:20 -0700 | 
| commit | 5dda723185a9037b7e92828d32430c21838ee216 (patch) | |
| tree | 15e12ff20e020bb524d704bc7e0d2f8d1f6f1b40 /test/widgetsSpec.js | |
| parent | f5027cc375cf29d8a78679297d9f6bdca9567eb7 (diff) | |
| download | angular.js-5dda723185a9037b7e92828d32430c21838ee216.tar.bz2 | |
improved handling of text fields when formater fails to prevent clobering of field
Diffstat (limited to 'test/widgetsSpec.js')
| -rw-r--r-- | test/widgetsSpec.js | 169 | 
1 files changed, 105 insertions, 64 deletions
diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index c9665f1e..b365175d 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -20,74 +20,115 @@ describe("widget", function(){    describe("input", function(){ -    it('should input-text auto init and handle keyup/change events', function(){ -      compile('<input type="Text" name="name" value="Misko" ng-change="count = count + 1" ng-init="count=0"/>'); -      expect(scope.$get('name')).toEqual("Misko"); -      expect(scope.$get('count')).toEqual(0); - -      scope.$set('name', 'Adam'); -      scope.$eval(); -      expect(element.val()).toEqual("Adam"); - -      element.val('Shyam'); -      element.trigger('keyup'); -      expect(scope.$get('name')).toEqual('Shyam'); -      expect(scope.$get('count')).toEqual(1); - -      element.val('Kai'); -      element.trigger('change'); -      expect(scope.$get('name')).toEqual('Kai'); -      expect(scope.$get('count')).toEqual(2); -    }); - -    it("should process ng-format", function(){ -      compile('<input type="Text" name="list" value="a,b,c" ng-format="list"/>'); -      expect(scope.$get('list')).toEqual(['a', 'b', 'c']); - -      scope.$set('list', ['x', 'y', 'z']); -      scope.$eval(); -      expect(element.val()).toEqual("x, y, z"); - -      element.val('1, 2, 3'); -      element.trigger('keyup'); -      expect(scope.$get('list')).toEqual(['1', '2', '3']); -    }); - -    it("should process ng-format for booleans", function(){ -      compile('<input type="checkbox" name="name" value="true" ng-format="boolean"/>', function(){ -        scope.name = false; +    describe("text", function(){ +      it('should input-text auto init and handle keyup/change events', function(){ +        compile('<input type="Text" name="name" value="Misko" ng-change="count = count + 1" ng-init="count=0"/>'); +        expect(scope.$get('name')).toEqual("Misko"); +        expect(scope.$get('count')).toEqual(0); + +        scope.$set('name', 'Adam'); +        scope.$eval(); +        expect(element.val()).toEqual("Adam"); + +        element.val('Shyam'); +        element.trigger('keyup'); +        expect(scope.$get('name')).toEqual('Shyam'); +        expect(scope.$get('count')).toEqual(1); + +        element.val('Kai'); +        element.trigger('change'); +        expect(scope.$get('name')).toEqual('Kai'); +        expect(scope.$get('count')).toEqual(2);        }); -      expect(scope.name).toEqual(false); -      expect(scope.$element[0].checked).toEqual(false); -    }); -    it("should process ng-validate", function(){ -      compile('<input type="text" name="price" value="abc" ng-validate="number"/>'); -      expect(element.hasClass('ng-validation-error')).toBeTruthy(); -      expect(element.attr('ng-validation-error')).toEqual('Not a number'); - -      scope.$set('price', '123'); -      scope.$eval(); -      expect(element.hasClass('ng-validation-error')).toBeFalsy(); -      expect(element.attr('ng-validation-error')).toBeFalsy(); +      describe("ng-format", function(){ + +        it("should farmat text", function(){ +          compile('<input type="Text" name="list" value="a,b,c" ng-format="list"/>'); +          expect(scope.$get('list')).toEqual(['a', 'b', 'c']); + +          scope.$set('list', ['x', 'y', 'z']); +          scope.$eval(); +          expect(element.val()).toEqual("x, y, z"); + +          element.val('1, 2, 3'); +          element.trigger('keyup'); +          expect(scope.$get('list')).toEqual(['1', '2', '3']); +        }); + +        it("should format booleans", function(){ +          compile('<input type="checkbox" name="name" value="true" ng-format="boolean"/>', function(){ +            scope.name = false; +          }); +          expect(scope.name).toEqual(false); +          expect(scope.$element[0].checked).toEqual(false); +        }); + +        it("should come up blank if null", function(){ +          compile('<input type="text" name="age" ng-format="number"/>', function(){ +            scope.age = null; +          }); +          expect(scope.age).toBeNull(); +          expect(scope.$element[0].value).toEqual(''); +        }); + +        it("should show incorect text while number does not parse", function(){ +          compile('<input type="text" name="age" ng-format="number"/>'); +          scope.age = 123; +          scope.$eval(); +          scope.$element.val('123X'); +          scope.$element.trigger('change'); +          expect(scope.$element.val()).toEqual('123X'); +          expect(scope.age).toEqual(123); +          expect(scope.$element).toBeInvalid(); +        }); + +        it("should clober incorect text if model changes", function(){ +          compile('<input type="text" name="age" ng-format="number" value="123X"/>'); +          scope.age = 456; +          scope.$eval(); +          expect(scope.$element.val()).toEqual('456'); +        }); + +        it("should come up blank when no value specifiend", function(){ +          compile('<input type="text" name="age" ng-format="number"/>'); +          scope.$eval(); +          expect(scope.$element.val()).toEqual(''); +          expect(scope.age).toEqual(null); +        }); -      element.val('x'); -      element.trigger('keyup'); -      expect(element.hasClass('ng-validation-error')).toBeTruthy(); -      expect(element.attr('ng-validation-error')).toEqual('Not a number'); -    }); - -    it("should not call validator if undefinde/empty", function(){ -      var lastValue = "NOT_CALLED"; -      angularValidator.myValidator = function(value){lastValue = value;}; -      compile('<input type="text" name="url" ng-validate="myValidator"/>'); -      expect(lastValue).toEqual("NOT_CALLED"); - -      scope.url = 'http://server'; -      scope.$eval(); -      expect(lastValue).toEqual("http://server"); +      }); -      delete angularValidator.myValidator; +      describe("ng-validate", function(){ +        it("should process ng-validate", function(){ +          compile('<input type="text" name="price" value="abc" ng-validate="number"/>'); +          expect(element.hasClass('ng-validation-error')).toBeTruthy(); +          expect(element.attr('ng-validation-error')).toEqual('Not a number'); + +          scope.$set('price', '123'); +          scope.$eval(); +          expect(element.hasClass('ng-validation-error')).toBeFalsy(); +          expect(element.attr('ng-validation-error')).toBeFalsy(); + +          element.val('x'); +          element.trigger('keyup'); +          expect(element.hasClass('ng-validation-error')).toBeTruthy(); +          expect(element.attr('ng-validation-error')).toEqual('Not a number'); +        }); + +        it("should not call validator if undefinde/empty", function(){ +          var lastValue = "NOT_CALLED"; +          angularValidator.myValidator = function(value){lastValue = value;}; +          compile('<input type="text" name="url" ng-validate="myValidator"/>'); +          expect(lastValue).toEqual("NOT_CALLED"); + +          scope.url = 'http://server'; +          scope.$eval(); +          expect(lastValue).toEqual("http://server"); + +          delete angularValidator.myValidator; +        }); +      });      });      it("should ignore disabled widgets", function(){  | 
