This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(form): make ngForm $pristine after nested control.$setPristine() (counter version) #13773
Open
linoleum-js
wants to merge
2
commits into
angular:master
Choose a base branch
from
linoleum-js:ng-form-set-pristine
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -714,9 +714,7 @@ describe('form', function() { | |
expect(form.$error.maxlength[0].$name).toBe('childform'); | ||
|
||
inputController.$setPristine(); | ||
expect(form.$dirty).toBe(true); | ||
|
||
form.$setPristine(); | ||
expect(form.$dirty).toBe(false); | ||
|
||
// remove child form | ||
form.$removeControl(childformController); | ||
|
@@ -1043,6 +1041,208 @@ describe('form', function() { | |
expect(nestedInputCtrl.$pristine).toBe(true); | ||
expect(nestedInputCtrl.$dirty).toBe(false); | ||
}); | ||
|
||
|
||
it('should be idempotent on freshly created form', function() { | ||
doc = $compile( | ||
'<form name="form">' + | ||
'<input ng-model="inputModel" name="input">' + | ||
'</form>')(scope); | ||
|
||
var form = doc, | ||
inputCtrl = doc.find('input').eq(0).controller('ngModel'); | ||
|
||
expect(form).toBePristine(); | ||
|
||
// many times | ||
inputCtrl.$setPristine(); | ||
inputCtrl.$setPristine(); | ||
scope.$apply(); | ||
expect(form).toBePristine(); | ||
|
||
inputCtrl.$setDirty(); | ||
scope.$apply(); | ||
expect(form).toBeDirty(); | ||
|
||
// many times | ||
inputCtrl.$setDirty(); | ||
inputCtrl.$setDirty(); | ||
scope.$apply(); | ||
expect(form).toBeDirty(); | ||
|
||
inputCtrl.$setPristine(); | ||
scope.$apply(); | ||
expect(form).toBePristine(); | ||
}); | ||
|
||
it('should propagate pristine-ness to the parent form', function() { | ||
doc = $compile( | ||
'<form name="parentForm">' + | ||
'<div ng-form name="childForm"></div>' + | ||
'</form>')(scope); | ||
|
||
var parentForm = doc, | ||
childForm = parentForm.find('div').eq(0), | ||
childFormCtrl = scope.childForm; | ||
|
||
childFormCtrl.$setDirty(); | ||
scope.$apply(); | ||
expect(parentForm).toBeDirty(); | ||
|
||
childFormCtrl.$setPristine(); | ||
scope.$apply(); | ||
expect(childForm).toBePristine(); | ||
expect(parentForm).toBePristine(); | ||
}); | ||
|
||
it('should be pristine if all the nested controls are pristine', function() { | ||
doc = $compile( | ||
'<form name="form">' + | ||
'<div ng-form name="childForm">' + | ||
'<input ng-model="inputModel1" name="input1">' + | ||
'<input ng-model="inputModel2" name="input2">' + | ||
'</div>' + | ||
'</form>')(scope); | ||
|
||
var form = doc, | ||
childForm = form.find('div').eq(0), | ||
input1 = form.find('input').eq(0), | ||
input2 = form.find('input').eq(1), | ||
inputCtrl1 = input1.controller('ngModel'), | ||
inputCtrl2 = input2.controller('ngModel'); | ||
|
||
inputCtrl1.$setDirty(); | ||
inputCtrl1.$setDirty(); | ||
scope.$apply(); | ||
expect(form).toBeDirty(); | ||
expect(childForm).toBeDirty(); | ||
|
||
inputCtrl2.$setDirty(); | ||
inputCtrl2.$setDirty(); | ||
scope.$apply(); | ||
expect(form).toBeDirty(); | ||
expect(childForm).toBeDirty(); | ||
|
||
inputCtrl1.$setPristine(); | ||
scope.$apply(); | ||
expect(form).toBeDirty(); | ||
expect(childForm).toBeDirty(); | ||
|
||
inputCtrl2.$setPristine(); | ||
scope.$apply(); | ||
expect(form).toBePristine(); | ||
expect(childForm).toBePristine(); | ||
}); | ||
|
||
it('should be pristine if all the nested forms are pristine', function() { | ||
doc = $compile( | ||
'<form name="outerForm1">' + | ||
'<div ng-form name="outerForm2">' + | ||
'<div ng-form name="childForm1"></div>' + | ||
'<div ng-form name="childForm2"></div>' + | ||
'</div>' + | ||
'</form>')(scope); | ||
|
||
var outerForm1 = doc, | ||
outerForm2 = doc.find('div').eq(0), | ||
childFormCtrl1 = scope.childForm1, | ||
childFormCtrl2 = scope.childForm2; | ||
|
||
childFormCtrl1.$setDirty(); | ||
scope.$apply(); | ||
expect(outerForm1).toBeDirty(); | ||
expect(outerForm2).toBeDirty(); | ||
childFormCtrl2.$setDirty(); | ||
scope.$apply(); | ||
expect(outerForm1).toBeDirty(); | ||
expect(outerForm2).toBeDirty(); | ||
|
||
childFormCtrl1.$setPristine(); | ||
scope.$apply(); | ||
expect(outerForm1).toBeDirty(); | ||
expect(outerForm2).toBeDirty(); | ||
|
||
childFormCtrl2.$setPristine(); | ||
scope.$apply(); | ||
expect(outerForm1).toBePristine(); | ||
expect(outerForm2).toBePristine(); | ||
}); | ||
|
||
it('should properly handle added/removed controls and be idempotent', function() { | ||
|
||
var test = function(input, inputCtrl) { | ||
doc = $compile( | ||
'<form name="outerForm">' + | ||
'<div ng-form name="innerForm"></div>' + | ||
'</form>')(scope); | ||
|
||
var outerForm = doc, | ||
innerForm = doc.find('div').eq(0), | ||
innerFormCtrl = innerForm.controller('form'); | ||
|
||
inputCtrl.$setDirty(); | ||
inputCtrl.$setDirty(); | ||
|
||
// just add control does not change form pristine-ness | ||
innerFormCtrl.$addControl(inputCtrl); | ||
scope.$apply(); | ||
expect(innerForm).toBePristine(); | ||
expect(outerForm).toBePristine(); | ||
|
||
// change after adding | ||
inputCtrl.$setDirty(); | ||
inputCtrl.$setDirty(); | ||
scope.$apply(); | ||
expect(innerForm).toBeDirty(); | ||
|
||
innerFormCtrl.$removeControl(inputCtrl); | ||
|
||
// removed control does not affect | ||
inputCtrl.$setPristine(); | ||
scope.$apply(); | ||
expect(innerForm).toBeDirty(); | ||
expect(outerForm).toBeDirty(); | ||
|
||
innerFormCtrl.$addControl(inputCtrl); | ||
scope.$apply(); | ||
expect(innerForm).toBeDirty(); | ||
expect(outerForm).toBeDirty(); | ||
|
||
// single $setPristine after multiple $setDirty | ||
inputCtrl.$setPristine(); | ||
scope.$apply(); | ||
expect(innerForm).toBePristine(); | ||
expect(outerForm).toBePristine(); | ||
|
||
// many times | ||
innerFormCtrl.$removeControl(inputCtrl); | ||
inputCtrl.$setPristine(); | ||
inputCtrl.$setPristine(); | ||
innerFormCtrl.$addControl(inputCtrl); | ||
scope.$apply(); | ||
expect(innerForm).toBePristine(); | ||
expect(outerForm).toBePristine(); | ||
|
||
// single setDirty afterm multiple setPristine | ||
inputCtrl.$setDirty(); | ||
scope.$apply(); | ||
expect(innerForm).toBeDirty(); | ||
expect(outerForm).toBeDirty(); | ||
}; | ||
|
||
var input1 = $compile('<input ng-model="inputModel" name="input">')(scope), | ||
inputCtrl1 = input1.controller('ngModel'), | ||
|
||
input2 = $compile('<div ng-form name="input"></div>')(scope), | ||
inputCtrl2 = input2.controller('form'); | ||
|
||
// test for input | ||
test(input1, inputCtrl1); | ||
dealoc(doc); | ||
|
||
// test for ng-form | ||
test(input2, inputCtrl2); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add a test to make sure a change propagates from a control through it's parent form to the outer-most form. |
||
}); | ||
|
||
describe('$setUntouched', function() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking: The above lines can be "summarized" as
var isPristine = controls.every(function(control) { return control.$pristine; });