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

Commit

Permalink
fix(ngScenario): select().option(val) should prefer exact value match
Browse files Browse the repository at this point in the history
With select(...).option(val) it previously would select the first node
which contains the value, even if an exact match was available.
This fix prefers exact matches if available, otherwise it reverts
to the previous 'contains' behaviour for backwards compatibility.

Closes #2856
  • Loading branch information
Smerity authored and petebacondarwin committed Jul 12, 2013
1 parent 7fef06f commit 22a9b1a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/ngScenario/dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,12 @@ angular.scenario.dsl('select', function() {
if (option.length) {
select.val(value);
} else {
option = select.find('option:contains("' + value + '")');
option = select.find('option').filter(function(){
return _jQuery(this).text() === value;
});
if (!option.length) {
option = select.find('option:contains("' + value + '")');
}
if (option.length) {
select.val(option.val());
} else {
Expand Down
20 changes: 16 additions & 4 deletions test/ngScenario/dslSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ describe("angular.scenario.dsl", function() {
$root.dsl.select('test').option('A');
expect(doc.find('[data-ng-model="test"]').val()).toEqual('A');
});

it('should select single option using x-ng', function() {
doc.append(
'<select x-ng-model="test">' +
Expand All @@ -238,14 +239,25 @@ describe("angular.scenario.dsl", function() {
expect(doc.find('[x-ng-model="test"]').val()).toEqual('A');
});

it('should select option by exact name', function() {
doc.append(
'<select ng-model="test">' +
' <option value=A>twenty one</option>' +
' <option value=B selected>two</option>' +
' <option value=C>thirty one</option>' +
' <option value=D>one</option>' +
'</select>'
);
$root.dsl.select('test').option('one');
expect(doc.find('[ng-model="test"]').val()).toEqual('D');
});



it('should select option by name', function() {
it('should select option by name if no exact match and name contains value', function() {
doc.append(
'<select ng-model="test">' +
' <option value=A>one</option>' +
' <option value=A>twenty one</option>' +
' <option value=B selected>two</option>' +
' <option value=C>thirty one</option>' +
'</select>'
);
$root.dsl.select('test').option('one');
Expand Down

0 comments on commit 22a9b1a

Please sign in to comment.