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

Commit 22a9b1a

Browse files
Smeritypetebacondarwin
authored andcommitted
fix(ngScenario): select().option(val) should prefer exact value match
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
1 parent 7fef06f commit 22a9b1a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/ngScenario/dsl.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,12 @@ angular.scenario.dsl('select', function() {
295295
if (option.length) {
296296
select.val(value);
297297
} else {
298-
option = select.find('option:contains("' + value + '")');
298+
option = select.find('option').filter(function(){
299+
return _jQuery(this).text() === value;
300+
});
301+
if (!option.length) {
302+
option = select.find('option:contains("' + value + '")');
303+
}
299304
if (option.length) {
300305
select.val(option.val());
301306
} else {

test/ngScenario/dslSpec.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ describe("angular.scenario.dsl", function() {
227227
$root.dsl.select('test').option('A');
228228
expect(doc.find('[data-ng-model="test"]').val()).toEqual('A');
229229
});
230+
230231
it('should select single option using x-ng', function() {
231232
doc.append(
232233
'<select x-ng-model="test">' +
@@ -238,14 +239,25 @@ describe("angular.scenario.dsl", function() {
238239
expect(doc.find('[x-ng-model="test"]').val()).toEqual('A');
239240
});
240241

242+
it('should select option by exact name', function() {
243+
doc.append(
244+
'<select ng-model="test">' +
245+
' <option value=A>twenty one</option>' +
246+
' <option value=B selected>two</option>' +
247+
' <option value=C>thirty one</option>' +
248+
' <option value=D>one</option>' +
249+
'</select>'
250+
);
251+
$root.dsl.select('test').option('one');
252+
expect(doc.find('[ng-model="test"]').val()).toEqual('D');
253+
});
241254

242-
243-
244-
it('should select option by name', function() {
255+
it('should select option by name if no exact match and name contains value', function() {
245256
doc.append(
246257
'<select ng-model="test">' +
247-
' <option value=A>one</option>' +
258+
' <option value=A>twenty one</option>' +
248259
' <option value=B selected>two</option>' +
260+
' <option value=C>thirty one</option>' +
249261
'</select>'
250262
);
251263
$root.dsl.select('test').option('one');

0 commit comments

Comments
 (0)