From 733a97adf87bf8f7ec6be22b37c4676cf7b5fc2b Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sat, 7 Jul 2012 19:02:04 +0200 Subject: 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 --- test/ng/directive/formSpec.js | 106 +++++++++++++++++++++++++++++++++++++++++ test/ng/directive/inputSpec.js | 12 +++++ 2 files changed, 118 insertions(+) (limited to 'test') 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( + '
' + + '' + + '' + + '
')(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( + '
' + + '' + + '
')(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( + '
' + + '
' + + '' + + '
' + + '
')(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); + }); + }); }); diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 01669b18..4dcb79a3 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -117,6 +117,18 @@ describe('NgModelController', function() { }); }); + describe('setPristine', function() { + + it('should set control to its pristine state', function() { + ctrl.$setViewValue('edit'); + expect(ctrl.$dirty).toBe(true); + expect(ctrl.$pristine).toBe(false); + + ctrl.$setPristine(); + expect(ctrl.$dirty).toBe(false); + expect(ctrl.$pristine).toBe(true); + }); + }); describe('view -> model', function() { -- cgit v1.2.3