Skip to content

Commit

Permalink
fix(nativeSelectValue): update selected value on change (#3154)
Browse files Browse the repository at this point in the history
* Fix nativeSelectValue not picking up selection

* Use props instead

* Add test for selected prop
  • Loading branch information
kevin940726 committed Sep 13, 2021
1 parent d0d1d59 commit 1ee88cb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/commons/text/form-control-value.js
Expand Up @@ -106,7 +106,7 @@ function nativeSelectValue(node) {
}

const options = querySelectorAll(vNode, 'option');
const selectedOptions = options.filter(option => option.hasAttr('selected'));
const selectedOptions = options.filter(option => option.props.selected);

// browser automatically selects the first option
if (!selectedOptions.length) {
Expand Down
6 changes: 4 additions & 2 deletions lib/core/base/virtual-node/virtual-node.js
Expand Up @@ -62,7 +62,8 @@ class VirtualNode extends AbstractVirtualNode {
id,
multiple,
nodeValue,
value
value,
selected
} = this.actualNode;

this._cache.props = {
Expand All @@ -72,7 +73,8 @@ class VirtualNode extends AbstractVirtualNode {
type: this._type,
multiple,
nodeValue,
value
value,
selected
};
}

Expand Down
14 changes: 14 additions & 0 deletions test/commons/text/form-control-value.js
Expand Up @@ -2,6 +2,7 @@ describe('text.formControlValue', function() {
var formControlValue = axe.commons.text.formControlValue;
var queryFixture = axe.testUtils.queryFixture;
var fixtureSetup = axe.testUtils.fixtureSetup;
var injectIntoFixture = axe.testUtils.injectIntoFixture;
var fixture = document.querySelector('#fixture');

function getNodeType(node) {
Expand Down Expand Up @@ -149,6 +150,19 @@ describe('text.formControlValue', function() {
assert.equal(nativeSelectValue(target), 'baz');
});

it('returns the selected option text after selection', function() {
injectIntoFixture(
'<select id="target">' +
' <option value="foo" selected>foo</option>' +
' <option value="bar">baz</option>' +
'</select>'
);
fixture.querySelector('#target').value = 'bar';
var rootNode = axe.setup(fixture);
var target = axe.utils.querySelectorAll(rootNode, '#target')[0];
assert.equal(nativeSelectValue(target), 'baz');
});

it('returns multiple options, space seperated', function() {
// Can't apply multiple "selected" props without setting "multiple"
var target = queryFixture(
Expand Down
10 changes: 10 additions & 0 deletions test/core/base/virtual-node/virtual-node.js
Expand Up @@ -39,6 +39,16 @@ describe('VirtualNode', function() {
assert.equal(vNode.props.type, 'text');
});

it('should reflect selected property', function() {
node = document.createElement('option');
var vNode = new VirtualNode(node);
assert.equal(vNode.props.selected, false);

node.selected = true;
vNode = new VirtualNode(node);
assert.equal(vNode.props.selected, true);
});

it('should lowercase type', function() {
var node = document.createElement('input');
node.setAttribute('type', 'COLOR');
Expand Down

0 comments on commit 1ee88cb

Please sign in to comment.