aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/ng/directive/formSpec.js106
-rw-r--r--test/ng/directive/inputSpec.js12
2 files changed, 118 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);
+ });
+ });
});
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() {