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

Commit a4f768f

Browse files
committed
fix tests in ie9, introduce custom matcher
IE9 will report option.hasAttribute('selected') === true if the option property has been unset and even if the dev tools show that the attribute is not set. I've added a custom matcher that accounts for this behavior. In all other browsers, property and attribute should always be in the same state.
1 parent 1bd55d9 commit a4f768f

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

test/ng/directive/ngOptionsSpec.js

+49-16
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+
toBeMarkedAsSelected: 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,31 +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]).toBeMarkedAsSelected();
791+
expect(options[1]).not.toBeMarkedAsSelected();
792+
expect(options[2]).not.toBeMarkedAsSelected();
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);
797+
expect(options[0]).not.toBeMarkedAsSelected();
798+
expect(options[1]).toBeMarkedAsSelected();
799+
expect(options[2]).not.toBeMarkedAsSelected();
766800

767801
scope.selected = scope.values[1];
768802
scope.$digest();
769803

770-
expect(options[0].hasAttribute('selected')).toBe(false);
771-
expect(options[1].hasAttribute('selected')).toBe(false);
772-
expect(options[2].getAttribute('selected')).toBe('selected');
804+
expect(options[0]).not.toBeMarkedAsSelected();
805+
expect(options[1]).not.toBeMarkedAsSelected();
806+
expect(options[2]).toBeMarkedAsSelected();
773807

774808
scope.selected = 'no match';
775809
scope.$digest();
776810

777811
expect(options[0].selected).toBe(true);
778-
expect(options[0].getAttribute('selected')).toBe('selected');
779-
expect(options[1].hasAttribute('selected')).toBe(false);
780-
expect(options[2].hasAttribute('selected')).toBe(false);
812+
expect(options[0]).toBeMarkedAsSelected();
813+
expect(options[1]).not.toBeMarkedAsSelected();
814+
expect(options[2]).not.toBeMarkedAsSelected();
781815
});
782816

783817
describe('disableWhen expression', function() {
@@ -1446,10 +1480,9 @@ describe('ngOptions', function() {
14461480
scope.selected = {};
14471481
scope.$digest();
14481482

1449-
expect(options[0].selected).toBe(true);
1450-
expect(options[0].getAttribute('selected')).toBe('selected');
1451-
expect(options[2].selected).toBe(false);
1452-
expect(options[2].hasAttribute('selected')).toBe(false);
1483+
expect(options[0]).toBeMarkedAsSelected();
1484+
expect(options[2]).not.toBeMarkedAsSelected();
1485+
expect(options[2]).not.toBeMarkedAsSelected();
14531486
});
14541487

14551488
});

0 commit comments

Comments
 (0)