Skip to content

Commit

Permalink
fix: wrapper.setSelected() to work on select with optgroups (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
martynling authored and eddyerburgh committed Jun 14, 2018
1 parent 93b8d98 commit dae0b1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,13 @@ export default class Wrapper implements BaseWrapper {
// $FlowIgnore
el.selected = true
// $FlowIgnore
createWrapper(el.parentElement, this.options).trigger(event)
if (el.parentElement.tagName === 'OPTGROUP') {
// $FlowIgnore
createWrapper(el.parentElement.parentElement, this.options).trigger(event)
} else {
// $FlowIgnore
createWrapper(el.parentElement, this.options).trigger(event)
}
} else if (tag === 'SELECT') {
throwError('wrapper.setSelected() cannot be called on select. Call it on one of its options')
} else if (tag === 'INPUT' && type === 'checkbox') {
Expand Down
9 changes: 9 additions & 0 deletions test/resources/components/component-with-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
<option value="selectB"></option>
<option value="selectC"></option>
</select>
<select v-model="selectVal" class="with-optgroups">
<optgroup label="Group1">
<option value="selectA"></option>
<option value="selectB"></option>
</optgroup>
<optgroup label="Group2">
<option value="selectC"></option>
</optgroup>
</select>
<label id="label-el"></label>

<span class="checkboxResult" v-if="checkboxVal">checkbox checked</span>
Expand Down
11 changes: 11 additions & 0 deletions test/specs/wrapper/setSelected.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ describeWithShallowAndMount('setSelected', (mountingMethod) => {
expect(wrapper.text()).to.contain('selectA')
})

it('updates dom with select v-model for select with optgroups', () => {
const wrapper = mountingMethod(ComponentWithInput)
const options = wrapper.find('select.with-optgroups').findAll('option')

options.at(1).setSelected()
expect(wrapper.text()).to.contain('selectB')

options.at(0).setSelected()
expect(wrapper.text()).to.contain('selectA')
})

it('throws error if wrapper does not contain element', () => {
const wrapper = mountingMethod({ render: (h) => h('div') })
const div = wrapper.find('div')
Expand Down

0 comments on commit dae0b1c

Please sign in to comment.