Skip to content

Commit dae0b1c

Browse files
martynlingeddyerburgh
authored andcommitted
fix: wrapper.setSelected() to work on select with optgroups (#715)
1 parent 93b8d98 commit dae0b1c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Diff for: packages/test-utils/src/wrapper.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,13 @@ export default class Wrapper implements BaseWrapper {
642642
// $FlowIgnore
643643
el.selected = true
644644
// $FlowIgnore
645-
createWrapper(el.parentElement, this.options).trigger(event)
645+
if (el.parentElement.tagName === 'OPTGROUP') {
646+
// $FlowIgnore
647+
createWrapper(el.parentElement.parentElement, this.options).trigger(event)
648+
} else {
649+
// $FlowIgnore
650+
createWrapper(el.parentElement, this.options).trigger(event)
651+
}
646652
} else if (tag === 'SELECT') {
647653
throwError('wrapper.setSelected() cannot be called on select. Call it on one of its options')
648654
} else if (tag === 'INPUT' && type === 'checkbox') {

Diff for: test/resources/components/component-with-input.vue

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
<option value="selectB"></option>
1010
<option value="selectC"></option>
1111
</select>
12+
<select v-model="selectVal" class="with-optgroups">
13+
<optgroup label="Group1">
14+
<option value="selectA"></option>
15+
<option value="selectB"></option>
16+
</optgroup>
17+
<optgroup label="Group2">
18+
<option value="selectC"></option>
19+
</optgroup>
20+
</select>
1221
<label id="label-el"></label>
1322

1423
<span class="checkboxResult" v-if="checkboxVal">checkbox checked</span>

Diff for: test/specs/wrapper/setSelected.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ describeWithShallowAndMount('setSelected', (mountingMethod) => {
2222
expect(wrapper.text()).to.contain('selectA')
2323
})
2424

25+
it('updates dom with select v-model for select with optgroups', () => {
26+
const wrapper = mountingMethod(ComponentWithInput)
27+
const options = wrapper.find('select.with-optgroups').findAll('option')
28+
29+
options.at(1).setSelected()
30+
expect(wrapper.text()).to.contain('selectB')
31+
32+
options.at(0).setSelected()
33+
expect(wrapper.text()).to.contain('selectA')
34+
})
35+
2536
it('throws error if wrapper does not contain element', () => {
2637
const wrapper = mountingMethod({ render: (h) => h('div') })
2738
const div = wrapper.find('div')

0 commit comments

Comments
 (0)