This repository was 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.4k
fix(ngOptions): remove selected attribute from unselected options #14894
Merged
Merged
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -120,6 +120,47 @@ describe('ngOptions', function() { | |
return { pass: errors.length === 0, message: message }; | ||
} | ||
}; | ||
}, | ||
toBeMarkedAsSelected: function() { | ||
// Selected is special because the element property and attribute reflect each other's state. | ||
// IE9 will wrongly report hasAttribute('selected') === true when the property is | ||
// undefined or null, and the dev tools show that no attribute is set | ||
return { | ||
compare: function(actual) { | ||
var errors = []; | ||
if (actual.selected === null || typeof actual.selected === 'undefined' || actual.selected === false) { | ||
errors.push('Expected option property "selected" to be truthy'); | ||
} | ||
|
||
if (msie !== 9 && actual.hasAttribute('selected') === false) { | ||
errors.push('Expected option to have attribute "selected"'); | ||
} | ||
|
||
var result = { | ||
pass: errors.length === 0, | ||
message: errors.join('\n') | ||
}; | ||
|
||
return result; | ||
}, | ||
negativeCompare: function(actual) { | ||
var errors = []; | ||
if (actual.selected) { | ||
errors.push('Expected option property "selected" to be falsy'); | ||
} | ||
|
||
if (msie !== 9 && actual.hasAttribute('selected')) { | ||
errors.push('Expected option not to have attribute "selected"'); | ||
} | ||
|
||
var result = { | ||
pass: errors.length === 0, | ||
message: errors.join('\n') | ||
}; | ||
|
||
return result; | ||
} | ||
}; | ||
} | ||
}); | ||
}); | ||
|
@@ -744,6 +785,41 @@ describe('ngOptions', function() { | |
}); | ||
|
||
|
||
it('should remove the "selected" attribute from the previous option when the model changes', function() { | ||
scope.values = [{id: 10, label: 'ten'}, {id:20, label: 'twenty'}]; | ||
|
||
createSelect({ | ||
'ng-model': 'selected', | ||
'ng-options': 'item.label for item in values' | ||
}, true); | ||
|
||
var options = element.find('option'); | ||
expect(options[0]).toBeMarkedAsSelected(); | ||
expect(options[1]).not.toBeMarkedAsSelected(); | ||
expect(options[2]).not.toBeMarkedAsSelected(); | ||
|
||
scope.selected = scope.values[0]; | ||
scope.$digest(); | ||
|
||
expect(options[0]).not.toBeMarkedAsSelected(); | ||
expect(options[1]).toBeMarkedAsSelected(); | ||
expect(options[2]).not.toBeMarkedAsSelected(); | ||
|
||
scope.selected = scope.values[1]; | ||
scope.$digest(); | ||
|
||
expect(options[0]).not.toBeMarkedAsSelected(); | ||
expect(options[1]).not.toBeMarkedAsSelected(); | ||
expect(options[2]).toBeMarkedAsSelected(); | ||
|
||
scope.selected = 'no match'; | ||
scope.$digest(); | ||
|
||
expect(options[0]).toBeMarkedAsSelected(); | ||
expect(options[1]).not.toBeMarkedAsSelected(); | ||
expect(options[2]).not.toBeMarkedAsSelected(); | ||
}); | ||
|
||
describe('disableWhen expression', function() { | ||
|
||
describe('on single select', function() { | ||
|
@@ -1395,6 +1471,26 @@ describe('ngOptions', function() { | |
}); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should remove the "selected" attribute when the model changes', function() { | ||
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. when the model changes --> when the view changes (?) 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. Technically, the problem appears when you update the model (which then updates the view) |
||
createSelect({ | ||
'ng-model': 'selected', | ||
'ng-options': 'item.label for item in arr track by item.id' | ||
}); | ||
|
||
var options = element.find('option'); | ||
browserTrigger(options[2], 'click'); | ||
|
||
expect(scope.selected).toEqual(scope.arr[1]); | ||
|
||
scope.selected = {}; | ||
scope.$digest(); | ||
|
||
expect(options[0]).toBeMarkedAsSelected(); | ||
expect(options[2]).not.toBeMarkedAsSelected(); | ||
expect(options[2]).not.toBeMarkedAsSelected(); | ||
}); | ||
|
||
}); | ||
|
||
|
||
|
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.
I think is missing the "track by id", sorry if I wrong
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.