Skip to content

Commit 2e6de7b

Browse files
VictorCazanaveeddyerburgh
authored andcommitted
feat: use setValue() on select element (#837)
1 parent ebf3f4f commit 2e6de7b

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

Diff for: .github/CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ The default test script will do the following: lint with ESLint -> type check wi
8282

8383
- **`create-instance`**: private package that creates an instance and applies mounting options.
8484

85-
- **`shared`**: private package that contains utilities used by the other packzges.
85+
- **`shared`**: private package that contains utilities used by the other packages.
8686

8787
- **`scripts`**: contains build-related scripts and configuration files. In most cases you don't need to touch them.
8888

Diff for: docs/api/wrapper/setValue.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## setValue(value)
22

3-
Sets value of a text-control input element and updates `v-model` bound data.
3+
Sets value of a text-control input or select element and updates `v-model` bound data.
44

55
- **Arguments:**
66
- `{any} value`
@@ -12,15 +12,26 @@ import { mount } from '@vue/test-utils'
1212
import Foo from './Foo.vue'
1313

1414
const wrapper = mount(Foo)
15+
1516
const input = wrapper.find('input[type="text"]')
1617
input.setValue('some value')
18+
19+
const select = wrapper.find('select')
20+
select.setValue('option value')
1721
```
1822

1923
- **Note:**
2024

21-
`textInput.setValue(value)` is an alias of the following code.
25+
- `textInput.setValue(value)` is an alias of the following code.
2226

23-
```js
24-
textInput.element.value = value
25-
textInput.trigger('input')
26-
```
27+
```js
28+
textInput.element.value = value
29+
textInput.trigger('input')
30+
```
31+
32+
- `select.setValue(value)` is an alias of the following code.
33+
34+
```js
35+
select.element.value = value
36+
select.trigger('change')
37+
```

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,12 @@ export default class Wrapper implements BaseWrapper {
697697
const type = this.attributes().type
698698

699699
if (tagName === 'SELECT') {
700+
// $FlowIgnore
701+
this.element.value = value
702+
this.trigger('change')
703+
} else if (tagName === 'OPTION') {
700704
throwError(
701-
`wrapper.setValue() cannot be called on a <select> ` +
705+
`wrapper.setValue() cannot be called on an <option> ` +
702706
`element. Use wrapper.setSelected() instead`
703707
)
704708
} else if (tagName === 'INPUT' && type === 'checkbox') {

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,34 @@ describeWithShallowAndMount('setValue', mountingMethod => {
1818
expect(textarea.element.value).to.equal('foo')
1919
})
2020

21-
it('updates dom with v-model', () => {
21+
it('updates dom with input v-model', () => {
2222
const wrapper = mountingMethod(ComponentWithInput)
2323
const input = wrapper.find('input[type="text"]')
24-
2524
input.setValue('input text awesome binding')
2625

2726
expect(wrapper.text()).to.contain('input text awesome binding')
2827
})
2928

30-
it('throws error if element is select', () => {
29+
it('sets element of select value', () => {
30+
const wrapper = mountingMethod(ComponentWithInput)
31+
const select = wrapper.find('select')
32+
select.setValue('selectB')
33+
34+
expect(select.element.value).to.equal('selectB')
35+
})
36+
37+
it('updates dom with select v-model', () => {
38+
const wrapper = mountingMethod(ComponentWithInput)
39+
const select = wrapper.find('select')
40+
select.setValue('selectB')
41+
42+
expect(wrapper.text()).to.contain('selectB')
43+
})
44+
45+
it('throws error if element is option', () => {
3146
const message =
32-
'wrapper.setValue() cannot be called on a <select> element. Use wrapper.setSelected() instead'
33-
shouldThrowErrorOnElement('select', message)
47+
'wrapper.setValue() cannot be called on an <option> element. Use wrapper.setSelected() instead'
48+
shouldThrowErrorOnElement('option', message)
3449
})
3550

3651
it('throws error if element is radio', () => {

0 commit comments

Comments
 (0)