diff options
| author | Yves Brissaud | 2013-12-10 21:49:31 +0100 |
|---|---|---|
| committer | Matias Niemelä | 2014-02-28 01:01:34 -0500 |
| commit | 33443966c8e8cac85a863bb181d4a4aff00baab4 (patch) | |
| tree | 2d811beee0b11ef997e084226343c7bc552207c7 /test/ng/directive/inputSpec.js | |
| parent | 8794a173f9c175df2343245e71ee9a137f5bc66a (diff) | |
| download | angular.js-33443966c8e8cac85a863bb181d4a4aff00baab4.tar.bz2 | |
feat($animate): animate dirty, pristine, valid, invalid for form/fields
Add css animations when form or field status change to/from dirty,
pristine, valid or invalid. This works like animation system present
with ngClass, ngShow, etc.
Closes #5378
Diffstat (limited to 'test/ng/directive/inputSpec.js')
| -rw-r--r-- | test/ng/directive/inputSpec.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index b9f737ac..e3e50e02 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -1482,3 +1482,101 @@ describe('input', function() { }); }); }); + +describe('NgModel animations', function() { + beforeEach(module('ngAnimateMock')); + + function findElementAnimations(element, queue) { + var node = element[0]; + var animations = []; + for(var i = 0; i < queue.length; i++) { + var animation = queue[i]; + if(animation.element[0] == node) { + animations.push(animation); + } + } + return animations; + }; + + function assertValidAnimation(animation, event, className) { + expect(animation.event).toBe(event); + expect(animation.args[1]).toBe(className); + } + + var doc, input, scope, model; + beforeEach(inject(function($rootScope, $compile, $rootElement, $animate) { + scope = $rootScope.$new(); + doc = jqLite('<form name="myForm">' + + ' <input type="text" ng-model="input" name="myInput" />' + + '</form>'); + $rootElement.append(doc); + $compile(doc)(scope); + $animate.queue = []; + + input = doc.find('input'); + model = scope.myForm.myInput; + })); + + afterEach(function() { + dealoc(input); + }); + + it('should trigger an animation when invalid', inject(function($animate) { + model.$setValidity('required', false); + + var animations = findElementAnimations(input, $animate.queue); + assertValidAnimation(animations[0], 'removeClass', 'ng-valid'); + assertValidAnimation(animations[1], 'addClass', 'ng-invalid'); + assertValidAnimation(animations[2], 'removeClass', 'ng-valid-required'); + assertValidAnimation(animations[3], 'addClass', 'ng-invalid-required'); + })); + + it('should trigger an animation when valid', inject(function($animate) { + model.$setValidity('required', false); + + $animate.queue = []; + + model.$setValidity('required', true); + + var animations = findElementAnimations(input, $animate.queue); + assertValidAnimation(animations[0], 'removeClass', 'ng-invalid'); + assertValidAnimation(animations[1], 'addClass', 'ng-valid'); + assertValidAnimation(animations[2], 'removeClass', 'ng-invalid-required'); + assertValidAnimation(animations[3], 'addClass', 'ng-valid-required'); + })); + + it('should trigger an animation when dirty', inject(function($animate) { + model.$setViewValue('some dirty value'); + + var animations = findElementAnimations(input, $animate.queue); + assertValidAnimation(animations[0], 'removeClass', 'ng-pristine'); + assertValidAnimation(animations[1], 'addClass', 'ng-dirty'); + })); + + it('should trigger an animation when pristine', inject(function($animate) { + model.$setPristine(); + + var animations = findElementAnimations(input, $animate.queue); + assertValidAnimation(animations[0], 'removeClass', 'ng-dirty'); + assertValidAnimation(animations[1], 'addClass', 'ng-pristine'); + })); + + it('should trigger custom errors as addClass/removeClass when invalid/valid', inject(function($animate) { + model.$setValidity('custom-error', false); + + var animations = findElementAnimations(input, $animate.queue); + assertValidAnimation(animations[0], 'removeClass', 'ng-valid'); + assertValidAnimation(animations[1], 'addClass', 'ng-invalid'); + assertValidAnimation(animations[2], 'removeClass', 'ng-valid-custom-error'); + assertValidAnimation(animations[3], 'addClass', 'ng-invalid-custom-error'); + + $animate.queue = []; + model.$setValidity('custom-error', true); + + animations = findElementAnimations(input, $animate.queue); + assertValidAnimation(animations[0], 'removeClass', 'ng-invalid'); + assertValidAnimation(animations[1], 'addClass', 'ng-valid'); + assertValidAnimation(animations[2], 'removeClass', 'ng-invalid-custom-error'); + assertValidAnimation(animations[3], 'addClass', 'ng-valid-custom-error'); + })); +}); |
