Skip to content

feat: use setValue() on select element #837

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 7 commits into from
Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ The default test script will do the following: lint with ESLint -> type check wi

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

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

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

Expand Down
23 changes: 17 additions & 6 deletions docs/api/wrapper/setValue.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## setValue(value)

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

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

const wrapper = mount(Foo)

const input = wrapper.find('input[type="text"]')
input.setValue('some value')

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

- **Note:**

`textInput.setValue(value)` is an alias of the following code.
- `textInput.setValue(value)` is an alias of the following code.

```js
textInput.element.value = value
textInput.trigger('input')
```
```js
textInput.element.value = value
textInput.trigger('input')
```

- `select.setValue(value)` is an alias of the following code.

```js
select.element.value = value
select.trigger('change')
```
6 changes: 5 additions & 1 deletion packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,12 @@ export default class Wrapper implements BaseWrapper {
const type = this.attributes().type

if (tagName === 'SELECT') {
// $FlowIgnore
this.element.value = value
this.trigger('change')
} else if (tagName === 'OPTION') {
throwError(
`wrapper.setValue() cannot be called on a <select> ` +
`wrapper.setValue() cannot be called on a <option> ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change a to ancannot be called on an <option>

`element. Use wrapper.setSelected() instead`
)
} else if (tagName === 'INPUT' && type === 'checkbox') {
Expand Down
25 changes: 20 additions & 5 deletions test/specs/wrapper/setValue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,34 @@ describeWithShallowAndMount('setValue', mountingMethod => {
expect(textarea.element.value).to.equal('foo')
})

it('updates dom with v-model', () => {
it('updates dom with input v-model', () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input[type="text"]')

input.setValue('input text awesome binding')

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

it('throws error if element is select', () => {
it('sets element of select value', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select')
select.setValue('selectB')

expect(select.element.value).to.equal('selectB')
})

it('updates dom with select v-model', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select')
select.setValue('selectB')

expect(wrapper.text()).to.contain('selectB')
})

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

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