diff options
| author | Pawel Kozlowski | 2012-07-07 19:02:04 +0200 |
|---|---|---|
| committer | Igor Minar | 2012-11-26 16:44:34 +0100 |
| commit | 733a97adf87bf8f7ec6be22b37c4676cf7b5fc2b (patch) | |
| tree | 99422e0e402fe4b4340552f8f80a4feb3ac861f0 /test/ng/directive/formSpec.js | |
| parent | 96ed9ff59a454486c88bdf92ad9d28ab8864b85e (diff) | |
| download | angular.js-733a97adf87bf8f7ec6be22b37c4676cf7b5fc2b.tar.bz2 | |
feat(form): add ability to reset a form to pristine state
Retting a form to pristine state will cause all of the nested
form and form controls to be recursively reset as well.
Closes #856
Diffstat (limited to 'test/ng/directive/formSpec.js')
| -rw-r--r-- | test/ng/directive/formSpec.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/test/ng/directive/formSpec.js b/test/ng/directive/formSpec.js index 2fd55f60..9fe98570 100644 --- a/test/ng/directive/formSpec.js +++ b/test/ng/directive/formSpec.js @@ -430,4 +430,110 @@ describe('form', function() { expect(doc).toBeDirty(); }); }); + + + describe('$setPristine', function() { + + it('should reset pristine state of form and controls', function() { + + doc = $compile( + '<form name="testForm">' + + '<input ng-model="named1" name="foo">' + + '<input ng-model="named2" name="bar">' + + '</form>')(scope); + + scope.$digest(); + + var form = doc, + formCtrl = scope.testForm, + input1 = form.find('input').eq(0), + input1Ctrl = input1.controller('ngModel'), + input2 = form.find('input').eq(1), + input2Ctrl = input2.controller('ngModel'); + + input1Ctrl.$setViewValue('xx'); + input2Ctrl.$setViewValue('yy'); + scope.$apply(); + expect(form).toBeDirty(); + expect(input1).toBeDirty(); + expect(input2).toBeDirty(); + + formCtrl.$setPristine(); + expect(form).toBePristine(); + expect(formCtrl.$pristine).toBe(true); + expect(formCtrl.$dirty).toBe(false); + expect(input1).toBePristine(); + expect(input1Ctrl.$pristine).toBe(true); + expect(input1Ctrl.$dirty).toBe(false); + expect(input2).toBePristine(); + expect(input2Ctrl.$pristine).toBe(true); + expect(input2Ctrl.$dirty).toBe(false); + }); + + + it('should reset pristine state of anonymous form controls', function() { + + doc = $compile( + '<form name="testForm">' + + '<input ng-model="anonymous">' + + '</form>')(scope); + + scope.$digest(); + + var form = doc, + formCtrl = scope.testForm, + input = form.find('input').eq(0), + inputCtrl = input.controller('ngModel'); + + inputCtrl.$setViewValue('xx'); + scope.$apply(); + expect(form).toBeDirty(); + expect(input).toBeDirty(); + + formCtrl.$setPristine(); + expect(form).toBePristine(); + expect(formCtrl.$pristine).toBe(true); + expect(formCtrl.$dirty).toBe(false); + expect(input).toBePristine(); + expect(inputCtrl.$pristine).toBe(true); + expect(inputCtrl.$dirty).toBe(false); + }); + + + it('should reset pristine state of nested forms', function() { + + doc = $compile( + '<form name="testForm">' + + '<div ng-form>' + + '<input ng-model="named" name="foo">' + + '</div>' + + '</form>')(scope); + + scope.$digest(); + + var form = doc, + formCtrl = scope.testForm, + nestedForm = form.find('div'), + nestedFormCtrl = nestedForm.controller('form'), + nestedInput = form.find('input').eq(0), + nestedInputCtrl = nestedInput.controller('ngModel'); + + nestedInputCtrl.$setViewValue('xx'); + scope.$apply(); + expect(form).toBeDirty(); + expect(nestedForm).toBeDirty(); + expect(nestedInput).toBeDirty(); + + formCtrl.$setPristine(); + expect(form).toBePristine(); + expect(formCtrl.$pristine).toBe(true); + expect(formCtrl.$dirty).toBe(false); + expect(nestedForm).toBePristine(); + expect(nestedFormCtrl.$pristine).toBe(true); + expect(nestedFormCtrl.$dirty).toBe(false); + expect(nestedInput).toBePristine(); + expect(nestedInputCtrl.$pristine).toBe(true); + expect(nestedInputCtrl.$dirty).toBe(false); + }); + }); }); |
