Skip to content

allow array for multiselect .setValue() method #1554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/api/wrapper/setValue.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,10 @@ textInput.setValue('some value')

const select = wrapper.find('select')
select.setValue('option value')

// requires <select multiple>
const multiselect = wrapper.find('select')
multiselect.setValue(['value1', 'value3'])
```

- **Note:**
4 changes: 4 additions & 0 deletions docs/ja/api/wrapper/setValue.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,10 @@ textInput.setValue('some value')

const select = wrapper.find('select')
select.setValue('option value')

// requires <select multiple>
const multiselect = wrapper.find('select')
multiselect.setValue(['value1', 'value3'])
```

- **注:**
4 changes: 4 additions & 0 deletions docs/ru/api/wrapper/setValue.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,10 @@ textInput.setValue('some value')

const select = wrapper.find('select')
select.setValue('option value')

// требует <select multiple>
const multiselect = wrapper.find('select')
multiselect.setValue(['value1', 'value3'])
```

- **Примечание:**
4 changes: 4 additions & 0 deletions docs/zh/api/wrapper/setValue.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,10 @@ textInput.setValue('some value')

const select = wrapper.find('select')
select.setValue('option value')

// requires <select multiple>
const multiselect = wrapper.find('select')
multiselect.setValue(['value1', 'value3'])
```

- **注意:**
33 changes: 18 additions & 15 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
@@ -744,27 +744,30 @@ export default class Wrapper implements BaseWrapper {
`wrapper.setValue() cannot be called on a <input type="radio" /> ` +
`element. Use wrapper.setChecked() instead`
)
} else if (
tagName === 'INPUT' ||
tagName === 'TEXTAREA' ||
tagName === 'SELECT'
) {
} else if (tagName === 'SELECT') {
if (Array.isArray(value)) {
// $FlowIgnore
const options = this.element.options
for (let i = 0; i < options.length; i++) {
const option = options[i]
option.selected = value.indexOf(option.value) >= 0
}
} else {
// $FlowIgnore
this.element.value = value
}

this.trigger('change')
return nextTick()
} else if (tagName === 'INPUT' || tagName === 'TEXTAREA') {
// $FlowIgnore
this.element.value = value

if (tagName === 'SELECT') {
this.trigger('change')
} else {
this.trigger('input')
}
this.trigger('input')

// for v-model.lazy, we need to trigger a change event, too.
// $FlowIgnore
if (
(tagName === 'INPUT' || tagName === 'TEXTAREA') &&
this.element._vModifiers &&
this.element._vModifiers.lazy
) {
if (this.element._vModifiers && this.element._vModifiers.lazy) {
this.trigger('change')
}
return nextTick()
7 changes: 7 additions & 0 deletions test/resources/components/component-with-input.vue
Original file line number Diff line number Diff line change
@@ -20,6 +20,11 @@
<option value="selectB"></option>
<option value="selectC"></option>
</select>
<select v-model="multiselectVal" class="multiselect" multiple>
<option value="selectA"></option>
<option value="selectB"></option>
<option value="selectC"></option>
</select>
<select v-model="selectVal" class="with-optgroups">
<optgroup label="Group1">
<option value="selectA"></option>
@@ -35,6 +40,7 @@
<span class="counter">{{ counter }}</span>
{{ textVal }}
{{ selectVal }}
{{ JSON.stringify(multiselectVal) }}
{{ radioVal }}
<input id="lazy" type="text" v-model.lazy="lazy" />
{{ lazy }}
@@ -52,6 +58,7 @@ export default {
textareaVal: undefined,
radioVal: undefined,
selectVal: undefined,
multiselectVal: [],
counter: 0
}
},
31 changes: 31 additions & 0 deletions test/specs/wrapper/setValue.spec.js
Original file line number Diff line number Diff line change
@@ -65,6 +65,37 @@ describeWithShallowAndMount('setValue', mountingMethod => {
expect(wrapper.text()).to.contain('selectB')
})

it('sets element of multiselect value', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select.multiselect')
select.setValue(['selectA', 'selectC'])

const selectedOptions = Array.from(select.element.selectedOptions).map(
o => o.value
)
expect(selectedOptions).to.deep.equal(['selectA', 'selectC'])
})

it('overrides elements of multiselect', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select.multiselect')
select.setValue(['selectA', 'selectC'])
select.setValue(['selectB'])

const selectedOptions = Array.from(select.element.selectedOptions).map(
o => o.value
)
expect(selectedOptions).to.deep.equal(['selectB'])
})

it('updates dom with multiselect v-model when array', async () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select.multiselect')
await select.setValue(['selectA', 'selectC'])

expect(wrapper.text()).to.contain('["selectA","selectC"]')
})

it('throws error if element is option', () => {
const message =
'wrapper.setValue() cannot be called on an <option> element. Use wrapper.setSelected() instead'