Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(select): make ctrl.hasOption method consistent #9262

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
id: option.id,
selected: option.selected
});
selectCtrl.addOption(option.label, element);
if (lastElement) {
lastElement.after(element);
} else {
Expand All @@ -622,7 +623,9 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
// remove any excessive OPTIONs in a group
index++; // increment since the existingOptions[0] is parent element not OPTION
while(existingOptions.length > index) {
existingOptions.pop().element.remove();
option = existingOptions.pop();
selectCtrl.removeOption(option.label);
option.element.remove();
}
}
// remove any excessive OPTGROUPs from select
Expand Down
66 changes: 66 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,39 @@ describe('select', function() {
expect(element).toEqualSelect(['? string:r2d2 ?']);
expect(scope.robot).toBe('r2d2');
});

describe('selectController.hasOption', function() {
it('should return true for options added via ngOptions', function() {
scope.robots = [
{key: 1, value: 'c3p0'},
{key: 2, value: 'r2d2'}
];
scope.robot = 'r2d2';

compile('<select ng-model="robot" ' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also have a version of this test for select-multiple? I don't see why the patch wouldn't work there, but it would be good to verify it

'ng-options="item.key as item.value for item in robots">' +
'</select>');

var selectCtrl = element.data().$selectController;

expect(selectCtrl.hasOption('c3p0')).toBe(true);
expect(selectCtrl.hasOption('r2d2')).toBe(true);

scope.$apply(function() {
scope.robots.pop();
});

expect(selectCtrl.hasOption('c3p0')).toBe(true);
expect(selectCtrl.hasOption('r2d2')).toBe(false);

scope.$apply(function() {
scope.robots.push({key: 2, value: 'r2d2'});
});

expect(selectCtrl.hasOption('c3p0')).toBe(true);
expect(selectCtrl.hasOption('r2d2')).toBe(true);
});
});
});
});
});
Expand Down Expand Up @@ -481,6 +514,39 @@ describe('select', function() {
expect(element).toBeValid();
expect(element).toBeDirty();
});

describe('selectController.hasOption', function() {
it('should return true for options added via ngOptions', function() {
scope.robots = [
{key: 1, value: 'c3p0'},
{key: 2, value: 'r2d2'}
];
scope.robot = 'r2d2';

compile('<select ng-model="robot" multiple ' +
'ng-options="item.key as item.value for item in robots">' +
'</select>');

var selectCtrl = element.data().$selectController;

expect(selectCtrl.hasOption('c3p0')).toBe(true);
expect(selectCtrl.hasOption('r2d2')).toBe(true);

scope.$apply(function() {
scope.robots.pop();
});

expect(selectCtrl.hasOption('c3p0')).toBe(true);
expect(selectCtrl.hasOption('r2d2')).toBe(false);

scope.$apply(function() {
scope.robots.push({key: 2, value: 'r2d2'});
});

expect(selectCtrl.hasOption('c3p0')).toBe(true);
expect(selectCtrl.hasOption('r2d2')).toBe(true);
});
});
});


Expand Down