diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 44c6cf6c..f541aaef 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -997,6 +997,15 @@ describe('select', function() { it('should ensure that at least one option element has the "selected" attribute', function() { + function countSelected() { + var count = 0; + forEach(element.find('option'), function(option) { + count += option.getAttribute('selected') ? 1 : 0; + }); + return count; + } + + createSelect({ 'ng-model': 'selected', 'ng-options': 'item.id as item.name for item in values' @@ -1006,34 +1015,34 @@ describe('select', function() { scope.values = [{id: 10, name: 'A'}, {id: 20, name: 'B'}]; }); expect(element.val()).toEqual('?'); - expect(element.find('option').eq(0).attr('selected')).toEqual('selected'); + expect(countSelected()).toEqual(1); scope.$apply(function() { scope.selected = 10; }); // Here the ? option should disappear and the first real option should have selected attribute expect(element.val()).toEqual('0'); - expect(element.find('option').eq(0).attr('selected')).toEqual('selected'); + expect(countSelected()).toEqual(1); // Here the selected value is changed but we don't change the selected attribute scope.$apply(function() { scope.selected = 20; }); expect(element.val()).toEqual('1'); - expect(element.find('option').eq(0).attr('selected')).toEqual('selected'); + expect(countSelected()).toEqual(1); scope.$apply(function() { scope.values.push({id: 30, name: 'C'}); }); expect(element.val()).toEqual('1'); - expect(element.find('option').eq(0).attr('selected')).toEqual('selected'); + expect(countSelected()).toEqual(1); // Here the ? option should reappear and have selected attribute scope.$apply(function() { scope.selected = undefined; }); expect(element.val()).toEqual('?'); - expect(element.find('option').eq(0).attr('selected')).toEqual('selected'); + expect(countSelected()).toEqual(1); }); });