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

Commit d799025

Browse files
committed
go full custom matcher
1 parent fd29c12 commit d799025

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

src/ng/directive/ngOptions.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -472,17 +472,10 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
472472

473473
// Make sure to remove the selected attribute from the previously selected option
474474
// Otherwise, screen readers might get confused
475-
if (selectedOption) {
476-
selectedOption.element.removeAttribute('selected');
477-
// jqLite(selectedOption.element).prop('selected', false); // IE9
478-
// jqLite(selectedOption.element).removeAttr('selected'); // IE9
479-
}
475+
if (selectedOption) selectedOption.element.removeAttribute('selected');
480476

481477
if (option) {
482-
if (providedEmptyOption) {
483-
emptyOption.removeAttr('selected');
484-
// emptyOption.prop('selected', false); // IE9
485-
}
478+
if (providedEmptyOption) emptyOption.removeAttr('selected');
486479
// Don't update the option when it is already selected.
487480
// For example, the browser will select the first option by default. In that case,
488481
// most properties are set automatically - except the `selected` attribute, which we

test/ng/directive/ngOptionsSpec.js

+49-18
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,40 @@ describe('ngOptions', function() {
120120
return { pass: errors.length === 0, message: message };
121121
}
122122
};
123+
},
124+
toBeSelected: function() {
125+
// Selected is special because the element property and attribute reflect each other's state.
126+
// IE9 will wrongly report hasAttribute('selected') === true when the property is
127+
// undefined or null, and the dev tools show that no attribute is set
128+
return {
129+
compare: function(actual) {
130+
var errors = [];
131+
if (actual.selected === null || typeof actual.selected === 'undefined' || actual.selected === false) {
132+
errors.push('Expected option property "selected" to be truthy');
133+
}
134+
135+
if (msie !== 9 && actual.hasAttribute('selected') === false) {
136+
errors.push('Expected option to have attribute "selected"');
137+
}
138+
139+
var message = function() {
140+
return errors.join('\n');
141+
};
142+
143+
var result = {};
144+
145+
result.pass = errors.length === 0;
146+
147+
if (result.pass) {
148+
result.message = 'Expected option property "selected" to be falsy' +
149+
(msie !== 9 ? ' and option not to have attribute "selected"' : '');
150+
} else {
151+
result.message = message;
152+
}
153+
154+
return result;
155+
}
156+
};
123157
}
124158
});
125159
});
@@ -753,33 +787,31 @@ describe('ngOptions', function() {
753787
}, true);
754788

755789
var options = element.find('option');
756-
expect(options[0].getAttribute('selected')).toBe('selected');
757-
expect(options[1].hasAttribute('selected')).toBe(false);
758-
expect(options[2].hasAttribute('selected')).toBe(false);
790+
expect(options[0]).toBeSelected();
791+
expect(options[1]).not.toBeSelected();
792+
expect(options[2]).not.toBeSelected();
759793

760794
scope.selected = scope.values[0];
761795
scope.$digest();
762796

763-
expect(options[0].hasAttribute('selected')).toBe(false);
764-
expect(options[1].getAttribute('selected')).toBe('selected');
765-
expect(options[2].hasAttribute('selected')).toBe(false);
766-
767-
return;
797+
expect(options[0]).not.toBeSelected();
798+
expect(options[1]).toBeSelected();
799+
expect(options[2]).not.toBeSelected();
768800

769801
scope.selected = scope.values[1];
770802
scope.$digest();
771803

772-
expect(options[0].hasAttribute('selected')).toBe(false);
773-
expect(options[1].hasAttribute('selected')).toBe(false);
774-
expect(options[2].getAttribute('selected')).toBe('selected');
804+
expect(options[0]).not.toBeSelected();
805+
expect(options[1]).not.toBeSelected();
806+
expect(options[2]).toBeSelected();
775807

776808
scope.selected = 'no match';
777809
scope.$digest();
778810

779811
expect(options[0].selected).toBe(true);
780-
expect(options[0].getAttribute('selected')).toBe('selected');
781-
expect(options[1].hasAttribute('selected')).toBe(false);
782-
expect(options[2].hasAttribute('selected')).toBe(false);
812+
expect(options[0]).toBeSelected();
813+
expect(options[1]).not.toBeSelected();
814+
expect(options[2]).not.toBeSelected();
783815
});
784816

785817
describe('disableWhen expression', function() {
@@ -1448,10 +1480,9 @@ describe('ngOptions', function() {
14481480
scope.selected = {};
14491481
scope.$digest();
14501482

1451-
expect(options[0].selected).toBe(true);
1452-
expect(options[0].getAttribute('selected')).toBe('selected');
1453-
expect(options[2].selected).toBe(false);
1454-
expect(options[2].hasAttribute('selected')).toBe(false);
1483+
expect(options[0]).toBeSelected();
1484+
expect(options[2]).not.toBeSelected();
1485+
expect(options[2]).not.toBeSelected();
14551486
});
14561487

14571488
});

0 commit comments

Comments
 (0)